data feed is fed one candle too early
-
I have a simple algo that has two input feeds: 5m and 1h.
I read the data from csv and add them the standard way:data5m, data1h = BacktestDataProvider.load(data_type) # Add the Data Feed to Cerebro cerebro.adddata(data5m) cerebro.adddata(data1h)
The problem is, that the hourly data are fed too early.
The next method:def next(self): self.log( f"\nsimulation datetime {self.datetime.datetime(0)}\n" f"5m feed datetime {self.datas[0].datetime.datetime(0)}\n" f"1h feed datetime {self.datas[1].datetime.datetime(0)}\n" )
and the output:
simulation datetime 2021-02-22 22:30:00 5m feed datetime 2021-02-22 22:30:00 1h feed datetime 2021-02-22 22:00:00
If the datetime in simulation is 22:30. It is not possible to receive 5m candle for time 22:30 or 1h candle for time 22:00, since (in the 1h case) you need all the data across interval 22:00 - 23:00.
I guess, that it would work fine with just one datafeed, since the orders are executed at the open price of the next candle. However, in this case I make decisions based on the 1h candle and execute them on the next 5m candle, which is way before I get the 1h candle in reality.
Any ideas how to solve this?
-
@kuky I believe BT assumes the timestamp of a bar is for the end of that time interval. Thus at the simulator time of 22:30, you are seeing the 5m bar that ended at 22:30 (i.e. 22:25-22:30) and the 1h bar that ended at 22:00 (which is the last complete 1h bar at this point).
-
@davidavr I think your right. The resample has this information:
rightedge (default: True)
Use the right edge of the time boundaries to set the time.
If False and compressing to 5 seconds the time of a resampled bar for seconds between hh:mm:00 and hh:mm:04 will be hh:mm:00 (the starting boundary
If True the used boundary for the time will be hh:mm:05 (the ending boundary)
-
Thanks guys. I will need to change my dataset then.
-
@kuky You can resample your data to the other edge. Keep timeframe and compression the same and just resample to rightedge = true. At least I think this would work.