MiniXC / simple-back

A simple daily python backtester that works out of the box.
Mozilla Public License 2.0
59 stars 12 forks source link

current close price replaced by na but not dropped #30

Closed fightthepower closed 4 years ago

fightthepower commented 4 years ago

This example has taken from the quickstart tutorial

Code

builder = (
   BacktesterBuilder()
   .name('JNUG 20-Day Crossover')
   .balance(10_000)
   .calendar('NYSE')
   .compare(['JNUG']) # strategies to compare with
   #.live_progress() # show a progress bar
)

bt = builder.no_live_plot().build()
for day, event, b in bt['2019-1-1':'2020-1-1']:
    if event == 'open':
        jnug_ma = b.prices['JNUG',-5:]['close']
        print("-"*50)
        print("Current Date: " , day.strftime("%Y-%m-%d"),"\n\n")
        print("Current Close: ",b.price('JNUG'),"\n\n")
        print("Previous Close: \n",b.prices['JNUG',-1:]['close'],"\n")

Close prices

>>> b.prices['JNUG']['close']
2013-10-03    37749.738281
2013-10-04    33773.062500
2013-10-07    33648.792969
2013-10-08    29758.150391
2013-10-09    28438.962891
                  ...     
2019-12-24      766.299927
2019-12-26      813.159302
2019-12-27      778.961914
2019-12-30      835.891113
2019-12-31      832.202148

Expected

--------------------------------------------------
Current Date:  2019-12-27 

Current Close:  807.5760402517996 

Previous Close: 
 2019-12-26    813.159302
Name: close, dtype: float64 

--------------------------------------------------
Current Date:  2019-12-30 

Current Close:  791.2251285490009 

Previous Close: 
2019-12-27      778.961914
Name: close, dtype: float64 

--------------------------------------------------
Current Date:  2019-12-31 

Current Close:  867.695590073621 

Previous Close: 
 2019-12-30    835.891113
Name: close, dtype: float64 

What I am getting

--------------------------------------------------
Current Date:  2019-12-27 

Current Close:  807.5760402517996 

Previous Close: 
 2019-12-26    813.159302
2019-12-27           NaN
Name: close, dtype: float64 

--------------------------------------------------
Current Date:  2019-12-30 

Current Close:  791.2251285490009 

Previous Close: 
 2019-12-30   NaN
Name: close, dtype: float64 

--------------------------------------------------
Current Date:  2019-12-31 

Current Close:  867.695590073621 

Previous Close: 
 2019-12-30    835.891113
2019-12-31           NaN
Name: close, dtype: float64 

Is this supposed to do this way?

MiniXC commented 4 years ago

This is because you are asking for the current close price at the market open. It would be easy to have a time leak by accident if close prices were available at the open. If you want to use future data in your backtests (which I recommend against, unless it is for evaluating your predictions) you can instantiate a new instance of YahooFinanceProvider.

Edit: Because you are only logging at market open, what you call "Current Close" should actually be "Current Open". Edit2: Actually "Current Close" confused me as well and now I see what the actual issue is ^^ I will add a dropna at the relevant place which will fix this. Will be included in 0.6.1