The right way to access multiple timeframes data?
-
Hi all,
I got a simple issue when using multiple timeframes data:
data1 = bt.feeds.PandasData(dataname=data_df, timeframe=bt.TimeFrame.Minutes, compression=1) cerebro.adddata(data1, name='M1') cerebro.resampledata(data1, name='M5', timeframe=bt.TimeFrame.Minutes, compression=5)
But in next(self) function of strategy I could not access the second data. It print if I used only one data. When add 2 data, the next function is missed (no print at all).
def next(self): print(self.datas[0].close[0]) print(self.datas[1].close[0])
So what is the right way to access multiple timeframes data in strategy class? I tried many search result here, but no way to access them.
-
@quỳnh-h-nguyễn From the information you have given, that should be working. Please try to add your entire code next time to assist us in answering your question.
Here is a sample of code to show you how to add data and print.
import backtrader as bt 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, txt)) def print_signal(self, dline=0): self.log( f"{self.datas[dline]._name}, " f"o {self.datas[dline].open[0]:7.2f} " f"h {self.datas[dline].high[0]:7.2f} " f"l {self.datas[dline].low[0]:7.2f} " f"c {self.datas[dline].close[0]:7.2f} " f"v {self.datas[dline].volume[0]:7.0f} " ) def next(self): self.print_signal(dline=0) self.print_signal(dline=1) if __name__ == "__main__": cerebro = bt.Cerebro() data = bt.feeds.GenericCSVData( dataname="data/dev.csv", dtformat=("%Y-%m-%d %H:%M:%S"), timeframe=bt.TimeFrame.Minutes, compression=1, date=0, high=1, low=2, open=3, close=4, volume=6, ) cerebro.adddata(data, name='m1') cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=15, name="m15") cerebro.addstrategy(Strategy) # Execute cerebro.run()
-
Wow, thanks so much @run-out !
I found the problem in data sample. Because the sample is not big enough for a indicator parameter. I used a indicator HullMovingAverage with a big period, so with the small timeframe it is normal, but in the large timeframe it need a bigger size of input data.
That's why cerebro plots running result, but not print log.
P/S: I will add entire code next question. Thanks for your advice!