Forward dates returning values?
-
The following code shouldn't be returning forward values but it is.
def __init__(self): self.open = self.datas[0].open self.close = self.datas[0].close def next(self): date = self.data.datetime.date() if date > datetime.date(2006, 12, 1): return print( date, self.datas[0].close[0], self.close[3], self.datas[0].open[0], self.open[3], )
Output:
2006-01-02 3604.33 3650.24 3578.73 3652.19 2006-01-03 3614.34 3666.99 3604.08 3650.54 2006-01-04 3652.46 3671.78 3615.23 3667.1 2006-01-05 3650.24 3644.94 3652.19 3671.23 2006-01-06 3666.99 3668.61 3650.54 3645.73 2006-01-09 3671.78 3670.2 3667.1 3667.16 2006-01-10 3644.94 3629.25 3671.23 3670.27 2006-01-11 3668.61 3644.41 3645.73 3628.73 2006-01-12 3670.2 3610.07 3667.16 3639.57
@vladisld Do I need another coffee? What's going on here?
Full Code:
import datetime import backtrader as bt import pandas as pd from extension.data import IQMinute from dateutil import relativedelta from datetime import date class Strategy(bt.Strategy): def log(self, txt, dt=None): """ Logging function fot this strategy""" dt = dt or self.data.datetime[0] if isinstance(dt, float): dt = bt.num2date(dt) print("%s, %s" % (dt.date(), txt)) def __init__(self): self.open = self.datas[0].open self.close = self.datas[0].close def next(self): date = self.data.datetime.date() if date > datetime.date(2006, 12, 1): return print( date, self.datas[0].close[0], self.close[3], self.datas[0].open[0], self.open[3], ) if __name__ == "__main__": cerebro = bt.Cerebro() data = bt.feeds.GenericCSVData( dataname="data/2006-day-001.txt", dtformat=("%Y-%m-%d"), timeframe=bt.TimeFrame.Days, compression=1, ) cerebro.adddata(data) cerebro.addstrategy(Strategy) cerebro.run()
-
@run-out said in Forward dates returning values?:
if date > datetime.date(2006, 12, 1):
returnI don't see the problem other than probably mixup of months and days. The code prints on each
next
until Dec'1 2006 - and the output confirms that.The print seems to be strange - but just probably because it prints in US locale ( %Y-%d-%m ) and not (%Y-%m-%d)
-
@vladisld Hey Vlad,
I'm using the conditional this just to stop the error at the end of the dataset, and this is not the important part..
if date > datetime.date(2006, 12, 1): return
The important part is that I getting the values three days ahead of time when I should get an error from backtrader.
self.datas[0].close[0], self.close[3],
Note that the close column is reading ahead 3 days. 3650.24 in the third column is the close value 3 bars ahead in the second column. We are looking into the future. Should backtrader kick up an error?
2006-01-02 3604.33 3650.24 3578.73 3652.19 2006-01-03 3614.34 3666.99 3604.08 3650.54 2006-01-04 3652.46 3671.78 3615.23 3667.1 2006-01-05 3650.24 3644.94 3652.19 3671.23
-
@run-out said in Forward dates returning values?:
We are looking into the future. Should backtrader kick up an error?
Why should backtrader kick up an error ? Referencing the future values are technically possible (it's questionable logically though)
-
@vladisld I guess i haven't done that in so long I came to believe it wasn't possible.