Hello, I've been working with backtrader for a couple of weeks now (great work by the way!), and I have some difficulties that I haven't been able to figure out on my own so far.
I am attempting to use backtrader to filter and select a few securities to trade from a large universe of about 200 etfs. Ideally, this code would be able to handle an arbitrarily large universe. That would be easy enough to do externally if I wanted to just stick with the original selections, but I want it to be able to rebalance at some given frequency (monthly perhaps) where it would go back and check the universe again for better candidates.
So far as I've found, there is not a convenient way to remove datastreams once they've been added and the strategy has been initialized. So, the only way that I have been able to do this is to pass the entire universe into self.datas via cerebro, and then let the strategy do all the magic. This seems like a really inefficient way to do it to me since I'm only using about 6 of those data streams at any given time. I've probably missed a glaringly simple way to do this. I am really horrible at programming to begin with, but I did try to figure this out and googled until my eyes bled.
If you're interested, this is how I am building self.datas:
data = {}
for sector in updDict.values():
for i, ticker in enumerate(sector):
data[i] = bt.feeds.YahooFinanceCSVData(dataname='./data/'+ticker+
'.csv', reverse = True, nullvalue=0, plot = False, **kwargs)
cerebro.adddata(data[i], name=ticker)
Right now updDict is a dictionary with 6 sectors as the keys and then a list of etfs for each key's value. In the strategy, I rank the etfs for each sector and pick the top one to trade, leaving me with 6 etfs.
Aside from the obvious inefficiency of having so many data feeds, plotting becomes impossible because cerebro wants to plot every data feed. I'm not really sure plotting a set of potentially rotating data streams would be any easier, though.
This turned into a bit of a book, so I will leave it at that for now. I would appreciate any suggestions.