-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Class variables - how to update them with optimized values and hold different values for different assets? #929
-
Background
For my strategy, I have approx. 10 class variables/parameters which I optimize.
I go through these one by one via optimize (there would be too many combos doing altogether), and after each one I update the class variable I just optimized with the optimized value so when testing the next variable, it incorporates the updated values for the first variable... and so on until I have done them all. I might run through again. Not perfect, but seemed to get me where I needed to get to.
This was fine with one asset (symbol), but I now want to execute over 5 to 20 assets, and furthermore, automate in order to hook into a live trading script. I am fairly new to coding, so I stick to what I know, which is using "for" loops, iterating over the symbols, and creating dictionaries for the optmized values which I can use to define a live trading strategy.
I am fine to create a dictionary of backtests for each asset applying each of the asset-specific dataframes as follows:
def get_backtests (df, symbols):
bt_dict = {}
for symbol in symbols:
backtest = Backtest(df[symbol], Live_Strategy, cash=100, commission=0.00066,
exclusive_orders=True, trade_on_close = True, margin=0.066667)
bt_dict[symbol] = backtest
return bt_dict
I can then optimize class variables/parameters for each asset and return a dictionary of the optimized values by asset using:
def get_optimized_indicator_1 (symbols):
optimized_indicator_1_dict = {}
for symbol in symbols:
stats, heatmap = bt_dict[symbol].optimize(....
heatmap = heatmap.reset_index(name='Equity')
col = "Equity"
max_x = heatmap.loc[heatmap[col].idxmax()]
optimized_indicator_1_dict[symbol] = max_x['indicator_1']
return optimized_indicator_1_dict
However, the issue is as things stand is that the "Live_Strategy" class section will still contain the original variable/parameter values, not the optimized ASSET-specific values. I don't know how to make the variables/parameters (indicator_1 through to 10) below dynamic:
class Live_Strategy (Strategy):
#define class parameters in order to run via bt.optimize
#how can these be updated with optimized, asset-specific values? Can change dataframe for each backtest,
#but class is always the same - i.e. Live_Strategy
indicator_1 = 100
indicator_2 = 200
...
indicator_10 = 1000
def init(self):
self.ind_1 = self.I(lambda ind_1: ind_1, self.data.ind_1.s)
self.ind_2 = self.I(lambda ind_2: ind_2, self.data.ind_2.s)
self.ind_3 = self.I(lambda ind_3: ind_3, self.data.ind_3.s)
def next(self):
# some trading strategy using indicators 1 to 10
Question Info
I cannot find a way of iterating over the class variables/parameters in "Live Strategy" to use the dictionaries I create for each optimized, asset-specific value. I can sort of work around this using only the optimizer to generate the dictionaries of values for each parameter for each asset - I do this by applying a range to test for ONLY the variable to be optimized, and essentially include the other 9 variables in the optimizer using their dictionary values with effectively no range to test, I will then move to the next indicator, and do the same... but this is time consuming and cumbersome - see below for illustration in case I'm hard to follow!:
def get_optimized_indicator_1 (symbols):
get_optimized_indicator_1 = {}
for symbol in symbols:
stats, heatmap = bt_dict[symbol].optimize(
indicator_1 = np.arange(1, 10, 1).tolist(),
indicator_2 = np.arange(optimized_indicator_2_dict[symbol],optimized_indicator_2_dict[symbol] +1, 1).tolist(),
indicator_3 = np.arange(optimized_indicator_3_dict[symbol],optimized_indicator_3_dict[symbol] +1, 1).tolist(),
etc.........
)
maximize='Return [%]',
method="grid",return_heatmap=True,
)
heatmap = heatmap.reset_index(name='Equity')
col = "Equity"
max_x = heatmap.loc[heatmap[col].idxmax()]
optimized_indicator_1_dict[symbol] = max_x['indicator_1']
return optimized_indicator_1_dict
The Actual Question!
So, for 20 assets, aside from creating 20 x classes of "Live Strategies" where I can assign asset-specific dictionaries for each class variable meaning they'll always pick up optimized values once those formulas have run, is there another, simpler way of incorporating the optmized values/parameters into each backtest for each asset, please?
Thanks for your patience if you have read until the end!
Beta Was this translation helpful? Give feedback.