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
THETA-X (fictional market) : The dotted line is the index, the candles are the traded asset. While the index rises, the filter lets buy signals on the asset through; when it turns, the strategy stays out, even if the asset still looks strong.
February–March 2020: a market filter put to the test : Between 19 February and 23 March 2020, the S&P 500 lost about 34%. An “index above its 200-day average” filter triggered late, once the fall was under way: it does not avoid the whole drop, but it blocks new purchases for the rest of the slide.
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