Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get more than one statistic when optimize #1147

Open
edofe99 opened this issue May 27, 2024 · 4 comments
Open

Get more than one statistic when optimize #1147

edofe99 opened this issue May 27, 2024 · 4 comments

Comments

@edofe99
Copy link

edofe99 commented May 27, 2024

When I optimize a strategy I can get only a chosen statistic to see for every combinations, for example:

stats, heatmap = bt.optimize(dc_period = range(3,30,3),close_after = range(3,30,3),maximize = 'Return [%]',return_heatmap=True)

If I print(heatmap) I get:

dc_period  close_after
3          3             0.91800
           6             1.65825
           9             1.37100
           12            1.11650
           15            2.13875
                           ...  
27         15            0.46700
           18            1.14275
           21            0.91300
           24            1.07525
           27            1.50150
Name: Return [%], Length: 81, dtype: float64

So I see the two parameters and corresponding return of that combination, but say I also want to see winrate and average trade % of every combination? How can I do that?

Thank's.

@coder145
Copy link

To the best of my knowledge, there isn't a way it can be done directly in the library, but you could always rerun the parameters you are interested in to obtain your statistic of choice.

@chrisgft
Copy link

chrisgft commented Jun 13, 2024

You can run this yourself a different route and get the same results as well as the stats for every run.

from itertools import product

def run_backtest(params):
    ema_period, keltner_atr_multiplier = params
    bt = Backtest(df_training, Test, cash=CASH, commission=0.0005, margin=0.02, trade_on_close=True, hedging=True)
    stats = bt.run(
        ema_period=ema_period,
        keltner_atr_multiplier=keltner_atr_multiplier
    )
    return stats

# Define ranges for each parameter
ema_period_range = range(30, 50, 10)
keltner_atr_multiplier_range = range(3, 6, 1)

# Generate all possible combinations of parameters
params_list = list(product(ema_period_range, keltner_atr_multiplier_range))

# Use ThreadPoolExecutor to run backtests
with concurrent.futures.ThreadPoolExecutor() as executor:
    results = list(executor.map(run_backtest, params_list))

max_result = None
for result in results:
    if (max_result == None):
        max_result = result['Equity Final [$]']
    else:
        max_result = max(result['Equity Final [$]'], max_result)```

@s-kust
Copy link

s-kust commented Aug 16, 2024

This repo - https://github.com/s-kust/python-backtesting-template - may be helpful.

See Optimization of Strategy Parameters in readme.md and optimize_params.py script.

This solution is more flexible than the original bt.optimize and also provides you with an Excel file containing the results.

@mateusz-brodowicz
Copy link

mateusz-brodowicz commented Nov 25, 2024

I would suggest looking into Optuna (https://github.com/optuna/optuna). You can define custom objective functions, and you're not limited to scalar outputs. You can pass your backtest result inside the optimize function, and define the objective function as any combination of stats object's attributes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants