Different results in strategy when using multiple timeframes
-
Hi,
I have written a simple strategy and I have two timeframes, 1Min and a resampled 5Min. When I define two indicators based on the 5Min data, the result of the backtest differs from when I comment those lines.
This is the strategy code:class Strategy(bt.Strategy): def __init__(self): self.dataclose = self.datas[0].close self.BB1small = bt.ind.BollingerBands(self.datas[0], period=16, devfactor=1.2) self.BB1large = bt.ind.BollingerBands(self.datas[0], period=89, devfactor=2.3) self.HMA1 = bt.ind.HullMovingAverage(self.datas[0], period=45) self.close1 = self.datas[0].close #************This is the part that creates the problem************** self.BB5small = bt.ind.BollingerBands(self.datas[1], period=16, devfactor=1.2) self.BB5large = bt.ind.BollingerBands(self.datas[1], period=89, devfactor=2.3) self.HMA5 = bt.ind.HullMovingAverage(self.datas[1], period=45) #******************************************************************** def next(self): if self.close1[0] > self.BB1large.lines.top[0] and not self.position: self.buy(size=.1) elif self.close1[0] < self.BB1large.lines.bot[0]: self.close()
-
@farzadex its not quite clear what you mean with different results.
you require more data if you resample from 1 min to 5 min. so the trading time range from your data will contain less trades. that does not mean that results are different but just the time range of the data you use will be shorter.
-
@dasch
If I don't write this part:self.BB5small = bt.ind.BollingerBands(self.datas[1], period=16, devfactor=1.2) self.BB5large = bt.ind.BollingerBands(self.datas[1], period=89, devfactor=2.3) self.HMA5 = bt.ind.HullMovingAverage(self.datas[1], period=45)
the final portfolio value of backtest is 9976.
but if I write it, however its data is not used in thenext
method, it affects the result and changes to 10042. -
@farzadex yes, that is expected then, you don't take some trades, since there is no data available yet on the 5min indicators.
if you disable these indicators, backtrader will not need to wait until these indicators are filled.
if you enable them, you will get into
next
later. just log you data time to see the difference. -
for example:
self.BB5large = bt.ind.BollingerBands(self.datas[1], period=89
period=89 on 5min -> 89*5 = 445 -> data will need at least 445minutes 1min data to fill the BB5large.
-
@dasch Thanks. You're right. but how can I overcome this? because later I need to compare the values of indicators of various timeframes. what is the right way to do that?
by the way, I used 1-min BTCUSD data for 1 month -
@farzadex not really sure what you mean with overcoming this. The indicators need to wait until they have enough data, nothing to overcome here.
If you use one strategy with different timeframes, just add all datas, indicators you want to analyze and then log the values or compare them like you want.
-
@dasch
I'm new to backtrader and I still don't know why there is such difference between results. what you say is absolutely correct, but 445 minutes is less than a day and I don't think many trades could have happened in first 445 minutes. -
why do you think, that the diff cannot come from the 445 minutes? its a diff of 66 dollars.
best way to find out is creating some logs, writing some analyzer, compare both results and look which trades were made.
with the limited amount of information provided this seems the best reason why you get different results.
-
@dasch
I logged the data and the first 3 trades don't happen when 5-min indicator is used and that's the difference between results.
Thanks for your help.