FRENESDE

Multiple data feeds

Each call to cerebro.adddata() adds a feed. In the strategy, self.datas lists them in the order they were added: self.data (or self.data0) is the first, self.data1 the second.
You can name a feed when adding it: cerebro.adddata(indice, name='INDICE'), then retrieve it with self.getdatabyname('INDICE'). It is clearer and safer than a number that changes if the order of adding changes.
Backtrader synchronises feeds by date: on each call to next(), the [0] values refer to the same moment. If a feed has no bar that day (a holiday on its exchange), it keeps the values of its last bar.
A common use: a market filter. You only buy the asset if the index is above its 200 average. To trade several assets, you name the target feed in the order: self.buy(data=d), and read the position with self.getposition(d).

Examples

Key terms

Feed
Price data source added to Cerebro
self.datas
List of all feeds, in the order they were added
getdatabyname
Method that returns a feed from its name
Market filter
Condition on an index that allows or blocks trades
Take the mission on Doji Academy

← Back to the index