How to add multiple timeframes in CCXTStore? [Python, backtrader]

319 Views Asked by At

I'm trying to use the CCXTStore library to create a backtrader Strategy that uses multiple timeframes (1h and 5m). For that I need to figure out how to add additional datafeeds into Cerebro.

With CSV data it is easy, I can simply create two data objects and add them to Cerebro one-by-one with adddata method. However, this does not work with CCXTStore.

Is it possible to add multiple timeframes using CCXTStore and how?

The closest topic on this that I found is this from the backtrader community forum. I also created a new post there as well, but seems that this community does not have that many participants these days.

1

There are 1 best solutions below

1
On

To add additional datafeeds into Cerebro using CCXTStore, you can use the adddata method of the Cerebro object. This method takes a data parameter, which can be an instance of the CCXTStore class configured with the desired exchange and timeframes.

For example:

import backtrader as bt
from backtrader_ccxt.ccxtstore import CCXTStore

# create an instance of the CCXTStore class
store = CCXTStore(exchange='binance', timeframe=bt.TimeFrame.Minutes, compression=1)

# create an instance of the Cerebro class
cerebro = bt.Cerebro()

# add the datafeed to Cerebro
cerebro.adddata(store)

# run the strategy
cerebro.run()

To add multiple timeframes, you can simply create additional instances of the CCXTStore class with different timeframes and add them to the Cerebro object using the adddata method.

For example:

import backtrader as bt
from backtrader_ccxt.ccxtstore import CCXTStore

# create an instance of the CCXTStore class for the 1-minute timeframe
store1min = CCXTStore(exchange='binance', timeframe=bt.TimeFrame.Minutes, compression=1)

# create an instance of the CCXTStore class for the 5-minute timeframe
store5min = CCXTStore(exchange='binance', timeframe=bt.TimeFrame.Minutes, compression=5)

# create an instance of the CCXTStore class for the 1-hour timeframe
store1hour = CCXTStore(exchange='binance', timeframe=bt.TimeFrame.Hours, compression=1)

# create an instance of the Cerebro class
cerebro = bt.Cerebro()

# add the datafeeds to Cerebro
cerebro.adddata(store1min)
cerebro.adddata(store5min)
cerebro.adddata(store1hour)

# run the strategy
cerebro.run()