Improve freqtrade strategy with parameter optimization
This revision is from 2024/11/23 15:41. You can Restore it.
Before optimizing our strategy, we need to see if it would have been profitable as a baseline.
Work in config.json, give it enough pairs and a timeframe. Perhaps a one hour timeframe and about fifty pairs. The more pairs in the list, the more data and the supposedly the better the optmization.
To perform a baseline backtest, download the data for the pairs with the following command :
freqtrade download-data -t 1h --timerange=20210101-
-t : define the timeframe for the data. (5m,1h,4h,1d).
--timerange= : Download data from 2021/01/01 until today.
Run the backtest
freqtrade backtesting -s MyStrategy --timerange=20210601-20210630
Review where the strategy is losing money. The first sell reason is because of the stop loss, maybe too close, maybe too far, Our ROI table can also be optimized to take more profits.
Optimize the stop loss and ROI table using hyperopt included in freqtrade.
What is Hyperopt?
Hyperopt is freqtrade’s tool to search and automate optimal parameters for our strategy. Applicable on technical indicators variables but also on sell, buy and stop loss variables. Hyperopt will use the data we have downloaded before to find the best parameters, this is why it’s important to have a lot of data.
Be aware that hyperopt takes time and it’s resource hungry.
Freqtrade’s Hyperopt example
Quickly optimize the spaces roi, stoploss and trailing without changing anything in the strategy. Optimize the stop loss and our ROI with the following command:
- Have a working strategy at hand.
freqtrade hyperopt --hyperopt-loss SharpeHyperOptLossDaily --spaces roi stoploss trailing --strategy MyWorkingStrategy --config config.json -e 100
freqtrade hyperopt --hyperopt-loss SharpeHyperOptLossDaily --spaces roi stoploss --strategy MyStrategy -e 100
freqtrade hyperopt --hyperopt-loss SortinoHyperOptLoss -s MyStrategy --spaces buy roi stoploss trailing -e 1000 -i 1h --timerange 20210422-20220908
Sortino ratio is less affected by upward fluctuations and focuses more on minimizing downside risk when compared to the Sharpe ratio, which considers overall volatility. Loss-functions, freqtrade has a number of loss functions built in as seen at https://www.freqtrade.io/en/latest/hyperopt/
Hyperopt has different calculation methods depending on your strategy, modes dedicated to scalping, Sharpe ratio, Sortino ration. Be sure to chose the right one according to your strategy.
-e defines how many evaluations hyperopt will do, the more you have, the more the results will be precise. Beyond 500-1000 there will usually no longer be any changes in the results.
Hyperopt work is finished, we can see that it transformed our losing strategy into a winning one simply by modifying our ROI table and our Stop Loss. Now we just have to modify our strategy with the values provided by hyperopt and do a new backtest to see if our strategy has been improved.
Source: https://blog.botcrypto.io/en/2021/07/08/tutorial-improve-your-freqtrade-strategy-with-hyperopt/
Valid switches are:
- all: optimize everything
- buy: just search for a new buy strategy
- sell: just search for a new sell strategy
- roi: just optimize the minimal profit table for your strategy
- stoploss: search for the best stoploss value
- trailing: search for the best trailing stop values
- trades: search for the best max open trades values
- protection: search for the best protection parameters (read the protections section on how to properly define these)
- default: all except trailing and protection
- space-separated list of any of the above values for example --spaces roi stoploss
Choose a hyperopt result other than the one displayed.
freqtrade hyperopt-show --config user_data/config.json --hyperopt-filename strategy_x_2024-05-09_21-41-37.fthypt -n 37
freqtrade hyperopt --hyperopt-loss SharpeHyperOptLoss --spaces all --strategy strategy -c user_data/config.json -e 500 --job-workers 1 -v
freqtrade hyperopt --hyperopt-loss SharpeHyperOptLossDaily --spaces buy sell roi --strategy SMA --config user_data/backtest_config.json -e 100 --job-workers 4
Here's a simple explanation of each loss function:
ShortTradeDurHyperOptLoss (Default Legacy Freqtrade Hyperoptimization Loss Function):
What it does: Focuses on avoiding short-term losses and ensuring that trades last long enough to be profitable.
Best for: Strategies that want to minimize quick losses and focus on longer trades.
OnlyProfitHyperOptLoss:
What it does: Considers only the total profit made from trades, without worrying about other factors like risk or trade duration.
Best for: Strategies that prioritize profit above all else, regardless of other risks or considerations.
SharpeHyperOptLoss:
What it does: Optimizes the Sharpe Ratio, which balances profit against overall risk (volatility).
Best for: Strategies that want to maximize returns for each unit of risk taken.
SharpeHyperOptLossDaily:
What it does: Similar to SharpeHyperOptLoss, but calculates the Sharpe Ratio based on daily returns.
Best for: Strategies that want to optimize daily performance while considering risk.
SortinoHyperOptLoss:
What it does: Optimizes the Sortino Ratio, which focuses on returns relative to downside risk (losses).
Best for: Strategies that want to maximize returns while minimizing losses.
SortinoHyperOptLossDaily:
What it does: Similar to SortinoHyperOptLoss, but calculates the Sortino Ratio based on daily returns.
Best for: Strategies that want to optimize daily performance while minimizing losses.
MaxDrawDownHyperOptLoss:
What it does: Minimizes the maximum drawdown, which is the largest loss from a peak to a trough.
Best for: Strategies that want to preserve capital and avoid large losses.
MaxDrawDownRelativeHyperOptLoss:
What it does: Minimizes both the maximum absolute drawdown and the relative drawdown (drawdown compared to overall profit).
Best for: Strategies that want to balance between avoiding large losses and maintaining profitability.
CalmarHyperOptLoss:
What it does: Optimizes the Calmar Ratio, which considers the annualized return relative to the maximum drawdown.
Best for: Strategies that want to achieve good returns with minimal risk of large losses.
ProfitDrawDownHyperOptLoss:
What it does: Balances maximizing profit and minimizing drawdown. You can adjust how strict it is on drawdown through a variable.
Best for: Strategies that want a good balance between profit and risk, with flexibility in how much drawdown is acceptable.
MultiMetricHyperOptLoss:
What it does: Optimizes for multiple metrics like profit, drawdown, profit factor, expectancy ratio, and win rate. It also penalizes strategies with too few trades.
Best for: Strategies that want a well-rounded performance, considering multiple aspects of trading and ensuring adequate trade frequency.