-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
-
Hi there,
I am still trying out the backtesting.optimize function (which is GREAT!!!). One think I would like to do, for analysis, though, is to see the various metrics that are pumped out as part of the backtesting run.
For example, I understand the below example shows the 'heatmap' which shows each of the input variables, and the output for the desired maximize function...
BUT, is there a way to save other metrics, such as 'Sharpe Ratio', 'Volatility' etc, that were calculated from a 'straight' run?
Thanks, gurus!
#############
s_time = time.time()
bt = Backtest(data_test, RsiOscillator, cash=10_000, commission=.002)
stats_skopt_2 = bt.run()
# bt.plot()
print('time for the initial strategy test is', time.time() - s_time)
def maximize_func(stats):
# return stats['Equity Final [$]'] / stats['Max. Drawdown [%]'] * stats['Expectancy [%]']
if stats['Max. Drawdown [%]'] == 0:
return stats['Equity Final [$]'] * stats['Expectancy [%]']
else:
return stats['Equity Final [$]'] * stats['Expectancy [%]'] / stats['Max. Drawdown [%]']
s_time = time.time()
# Note : for method = 'skopt', we only need interval end-points
# We can input constraints per this line if needed : constraint=lambda p: p.rsi_lower < p.rsi_upper,
stats_skopt_2, heatmap, optimize_result = bt.optimize(
upper_bound = [60, 70],
lower_bound = [15, 30],
rsi_window = [11, 14],
maximize= maximize_func,
method='skopt',
max_tries=100,
random_state=0,
return_heatmap=True,
return_optimization=True)
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 4 replies
-
You could save and/or examine every instance of stats
that you get into your maximize_func()
...
Beta Was this translation helpful? Give feedback.
All reactions
-
sorry, @kernc , but where would I put the code to save down the stats
within the bt.optimize?
Beta Was this translation helpful? Give feedback.
All reactions
-
Did you find an answer to this? I would be interested to know how to save every instance of stats and trades as my optimisations can take hours to complete.
Beta Was this translation helpful? Give feedback.
All reactions
-
I thought one can save all optimization stats somewhat like so:
all_results_stats = [] def maximize_func(stats): all_results_stats.append(stats) return stats['SQN'] # or similar bt = Backtest(...) bt.optimize(..., maximize=maximize_func) assert all_results_stats
but now I'm thinking this simple approach may not work with multiprocess optimization. 😅
Beta Was this translation helpful? Give feedback.
All reactions
-
@daveysab / @kernc ,
@kernc , thanks for the advice (code)..... I tried your sample, and unfortunately, it did not seem to save down anything. Perhaps I did something wrong (though I just cut-and-paste your suggestion). And yes, the trouble is, I am trying to have multi-process optimization...
@daveysab , unfortunately, I did not manage to debug/get the answer to this as of yet. If I manage to debug it, I will post it here, and give you a shout.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1