Photo by Markus Winkler on Unsplash.com
Strategy optimization doesn’t have to be hard and you don’t even have to code it yourself. Python backtesting libraries like backtrader, zipline or backtesting.py come with a built-in optimization engine that finds the optimal combination of strategy parameter values.
If you would like to learn how to optimize your trading strategy using backtesting.py this post is for you!
This story is solely for general information purposes, and should not be relied upon for trading recommendations or financial advice. Source code and information is provided for educational purposes only, and should not be relied upon to make an investment decision. Please review my full cautionary guidance before continuing.
Optimization Options
Backtesting.py offers two optimization options: Randomized Grid Search and the scikit-optimize package.
Grid Search randomly searches through the combinations of specified parameters in order to optimize profit.
The second optimization option using the scikit-optimize package uses forests of decision trees. The library uses a tree based regression model to model the expensive backtest function. The model is then improved by sequentially evaluating the backtest function at the next best point. This process allows the package to find an optimized result with as few evaluations as possible.
In this tutorial we are going to use the Grid Search optimization.
Here the backtesting.py documentation on optimization.
The Strategy
For the purpose of this tutorial we are going to use a sample strategy that let’s us optimize a couple of technical indicator parameters.
We will use the following technical indicators:
Exponential Moving Average (EMA)
The EMA is a moving average that places more weight on the most recent prices. As a consequence, the indicator is closer aligned with the actual price.
For a detailed explanation how the EMA is calculated, check out Investopia.
Relative Strength Index (RSI)
The RSI is a momentum indicator that seeks to identify overbought and underbought conditions of an asset by calculating the average price changes during a lookback period.
Check out the detailed calculation on Investopedia here.
Strategy Rules
Long Entry:
EMA 9 crosses above EMA 13
RSI Period 14 crosses above 30
Long Exit:
Take profit at 3 percent
stop loss of 1 percent
Later in this tutorial we are going to optimize the EMA periods, the RSI periods and the lower and upper RSI threshold.
How long do optimizations take to run?
Strategy optimizations may take a long time to run - from minutes and hours or even days. The estimated time depends on a variety of factors. Here are the most significant ones:
Your computing power (e.g. number of CPUs, CPU speed, RAM size, hard disk speed)
The amount and time interval of your data
The complexity of the computing steps that need to be performed during execution of the strategy.
The number of variables you want to optimize
The size of the ranges and step size for each variable
The number of max retries for the optimization.
If you are planning to run very complex optimizations, I suggest you look into backtesting tools like MetaTrader, which allow you to buy distributed computing power for running optimizations.
What is Overfitting?
Overfitting is a condition that occurs when the strategy parameters or a model has been optimized to the point that is only generates optimal results for a given data set. However, when applying the optimized strategy to another data set, the results are quite poor.
To discuss in detail how to prevent overfitting would extend past the scope of this tutorial. I suggest for your to be aware of it, read up on it and test your optimized strategy against different data sets, market conditions, and time periods.