Cerebro forloop reuse?
-
I'm trying to reuse cerebro inside a for loop, something like this:
for item in x: cerebro = bt.Cerebro() strategy = SmaCross(item[0],item[1]) cerebro.addstrategy(strategy) data0 = bt.feeds.YahooFinanceData(dataname='MSFT', fromdate=datetime(2018, 1, 1), todate=datetime(2020, 12, 31)) cerebro.adddata(data0) cerebro.run()
However im arriving at a strange error:
_obj._id = cerebro._next_stid() AttributeError: 'NoneType' object has no attribute '_next_stid'
Which is fine, since only one strategy gets passed (before we're supposedly creating a new obj of cerebro) - it shouldnt have an id for a next strategy. I've read up on the reuse, and im not sure the Strategy Factory approach i read in the other thread will help, as the strategy inputs need to be dynamic and not predefined like in the example. Meaning, the inputs for the SMA need to change after every iteration of the loop.
Any help with this? Loving the lib so far :)
-
@appypollyloggies said in Cerebro forloop reuse?:
strategy = SmaCross(item[0],item[1])
I think your problem lies here. You are instantiating the strategy class. The strategy class is passed into cerebro as a class and with it the kwargs. Something like this.
for item in x: cerebro = bt.Cerebro() kwargs = dict( param1 = item[0], param2 = item[1], ) cerebro.addstrategy(SmaCross, **kwargs) data0 = bt.feeds.YahooFinanceData(dataname='MSFT', fromdate=datetime(2018, 1, 1), todate=datetime(2020, 12, 31)) cerebro.adddata(data0) cerebro.run()
-
@run-out Amazing, thank you!