How to pass a data-specific parameter for each data in a multidata strategy
-
Hey all,
I searched for a while but couldnt find anyone trying to do this exactly, so quick question for you:I run through SPY in an instance of cerebro to create a list of signals for trading 0DTE options which includes: contract type, strike, entry time.
on a single data, this can be done by passing in the entry time as a parameter, but i get stuck on a multidata setup.
When I load all of the datasets to multidata cerebro instance, I'd ideally like to pass the entry time as a parameter "specific to each data", but I can only see how to pass a "global parameter".
Does anyone have a way to pass a data-specific parameter?
My table of signals from the first run on SPY looks like:
index, dataname entry_time
0 SPY220225P00432000 10:46:00
1 SPY220225P00435000 11:19:00
2 SPY220225P00436000 11:55:00The entry signal in the options trades looks like this:
for i, d in enumerate(self.datas): dt, tm, dn = self.datetime.date(tz=pytz.timezone('US/Eastern')), self.datetime.time(tz=pytz.timezone('US/Eastern')), d._name if tm == self.p.entry_time: # <-- this parameter is global. #do some trade stuff
I remain keen to run the options trades as a multidata strategy rather than looping through each one, and if i wanted to load all of the data along with SPY, i would be loading a lot of options data that wont be used.
Thanks!!
-
@stevenm100 I created options capability in a library called Lumibot. It's open source and you can load and trade options in backtesting and live with Interactive Brokers. Here's an example strategy.
Let me know if you have any questions.
-
@run-out
Thanks, that looks interesting.just generally speaking for backtrader , is there a way to pass a separate parameter in for each data loaded? For instance, can a strategy's parameter actually be a dict or a list? (i'll try this next).
One other related question, if opting for the "load all the data" method, is there a way to only turn on plotting for the companion data that gets traded (like setting the plot = True/False in the notify trade method)? (i'll also try this out soon).
I havent found either on the community pages yet :)
-
FYI folks,
It works to pass a dictionary as a parameter to the strategy with the caveat that the dataname cannot be an integer. I got around this by passing as an str()spy_dict = spy_orders.to_dict('dict') for n in spy_orders.index: print(n) dataframe = pdr.get_data_yahoo(spy_dict['dataname'][n], period='1d', interval='1m', prepost=False) data = bt.feeds.PandasData(dataname=dataframe, tz=pytz.timezone('US/Eastern')) cerebro.adddata(data, name=str(n)) # <--- LOOK HERE name = str(n)
Then when trying to reference it within the strategy you need to convert back to int() like this:
if tm == self.p.entry_time['entry_time'][int(d._name)]: #<--LOOK HERE int(d._name) #do some trading stuff
Cheers.
-
oops missed a bit of the code:
cerebro = bt.Cerebro() cerebro.addstrategy(spy_0DTE_strat_OPTIONS, entry_time = spy_dict)