Improve freqtrade strategy with parameter optimization

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:

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

  

📝 📜 ⏱️  ⬆️