[Tutorial] Improve your freqtrade strategy with machine learning
In the previous article, we’ve made our first personalized strategy on freqtrade. Now it’s time to see if the strategy is profitable and if it’s possible to improve it through hyperopt and machine learning.
- Basic knowledge in Python.
- A good understanding of how freqtrade works (see documentation).
Before starting to optimize our strategy, we need to see if it would have been profitable by the past. The process to do this with freqtrade is as follows :
- Give a pairlist to freqtrade
- Download data from this pairlist
Place yourself in the freqtrade main folder and modify the previously created
config.json file. It’s in this file that we will give the pairlist on wich the bot will work as well as the timeframe.
For this example, I’ve chosen a one hour timeframe and selected about fifty pairs. The more pair you have in your list, the more details you’ll have on the backtest and data as well to improve your strategy.
Let’s download the data of these 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.
We’re now ready for our first backtest !
freqtrade backtesting -s MyStrategy --timerange=20210601-20210630
Here are the bot’s results for June of 2021 :
The strategy is loosing money during June. 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.
Let’s see how to automate the research for optimization of our stop loss and ROI table thanks to hyperopt included in freqtrade.
What is Hyperopt ?
Hyperopt is a freqtrade’s tool that will allow us to automate the research for optimal parameters for our strategy. It is applicable on technical indicators variables but also on sell, buy and stop loss variables. It’s a very powerful tool when it’s mastered and it’s really worth your time. Hyperopt will rely on 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. Don’t hesitate to rent a VPS to do this.
Freqtrade’s Hyperopt example
We’re ready, let’s optimize our stop loss and our ROI with the following command :
freqtrade hyperopt --hyperopt-loss SharpeHyperOptLossDaily --spaces roi stoploss --strategy MyStrategy -e 100
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 now done, we can see that it transformed our losing strategy into a winning one (since 2021-01-01) 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 during the month of June.
Here’s the result, our strategy is still losing but we passed from -25.55% to -11.96% !
You now have a great example of how powerful hyperopt is, all you have to do is refine your strategy automatically. Happy Trading !