Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Is there anyway to save down the Sharp-Ratios and Other Parameters from BackTesting.Optimize #881

Unanswered
kiann00 asked this question in Q&A
Discussion options

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)

image

You must be logged in to vote

Replies: 1 comment 4 replies

Comment options

You could save and/or examine every instance of stats that you get into your maximize_func() ...

You must be logged in to vote
4 replies
Comment options

sorry, @kernc , but where would I put the code to save down the stats within the bt.optimize?

Comment options

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.

Comment options

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. 😅

Comment options

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /