Together with our worldwide community of traders, there is a growing group of programmers who are extending StrategyQuant with custom snippets and indicators. We would like to support your effort, …
Building mean-reversion strategies using templates and limit orders
In this simple tutorial we present you a simple approach to setup a builder template that allows you to generate mean-reversion strategies easily. StrategyQuantX can explore millions of possible combinations of a mean-reversion system that can be easily found using its excelent templating system.
What is a mean-reversion strategy
The concept is really simple. Imagine the market price does not move too much for some time and either trends slowly or is choppy and then suddenly a quick sell-off occurs. In such situation traders tend to perceive this situation as an opportunity to buy with a discount and ‘fade’ this market action expecting the market price to return back to its average (mean).
This approach is commonly used across all markets but surprisingly works well with stocks and stock index futures naturally. In our example we try to test the idea using US index futures data for an e-mini contract but as mentioned the concept can be used in any market.
An example It can be clearly seen on the following picture. The market moved slowly higher and then suddenly went sharply down. Once we see the movement we could assume this ‘extreme’ to be temporary and try to buy into the price weakness expecting the market price to go back up.
How to build a mean reversion system in SQ AlgoWizard
To understand following information we assume you are already familiar with ‘SQ templates and randomization’. If not please skip reading this article for a moment and check our article on Strategy templates.
We could move the idea little further and for a situation where market drops we also assume the drop could continue a little lower before it bounces back to the average thus giving us an additional price discount for the long entry. This is a great opportunity to test a buy limit order how it can perform in this setup and whether it can provide some profit opportunity. So let’s define some basic rules and entry conditions.
It is quite easy to setup a template for this. To keep everything as simple as possible we build the strategy for the long side only while we want StrategyQuantX to build conditions using all available (and enabled) building blocks. We do not want to limit the builder here since we do not know yet what conditions for placing limit order we are going to use. This can be easily achieved using a template with entry conditions as follows:
- The last bar size (from High to Low price) needs to be larger than ATR value (average true range) multiplied by a fix number (for example High – Low > 1.5 x ATR)
- We also have a bearish bar for a valid signal (Close < Open)
- We add a ‘Random condition 1’ so the builder can replace it by any possible entry and the best exit condition (one or more) such as indicators, signals, price action conditions etc. This can serve the purpose of for example ‘filtering’ signals into a trend direction etc.
- Finally we want to trigger the buy entry only if there is no existing long position (we are FLAT)
Note the green-colored items that represent blocks that will be randomly generated later by the builder. We want the builder to optimize it for us
That’s it for the entry signal. Now let’s have a look onto the long entry order tab how to setup the buy limit order correctly using the randomization.
Notice the highlighted part where we set the limit price for the buy limit order. You can see we subtract ATR value again multiplied by a fix value from the last bar close. The fix value will be generated later randomly by SQX in the range from 0.5 to 2 using step = 0.1 (0.5, 0.6, 0.7, …, 1.9, 2). We also want to have the order expiration and stop-loss always enabled in the template so we enable this option as well.
Building mean reversion strategies using StrategyQuantX templates
Now the template is basically complete and we can setup a simple build configuration to test how well this can perform.
To test the idea we will be using the data of E-mini S&P 500 index futures contract. You can easily switch to the micro version of this contract (symbol name ET in StrategyQuant Data Manager) with the point value 10 times smaller compared to the mini version.
We use the simplest build config possible since we want StrategyQuantX to generate just a quick prime sample of strategies to see whether this trading approach can provide profitable systems generally. Let’s setup only the essentials:
We also want to see the actual trades that a strategy creates on a price chart so we enable the ‘Store chart data’ option
And finally we want some basic performance metrics set so only profitable strategies are stored into the databank.
When the random generation mode is used you should get first strategies almost immediately after you start the build. Let’s check the best strategy in the first sample whether all works as expected.
We can also see the pseudo code shows a correct strategy logic according to the template setting we used.
And we can check individual trades executions on a chart directly in StrategyQuantX.
Using this basic template you can explore huge amount of possible strategies across forex, stocks and commodities and across all possible timeframes. We do not recommend to test using small timeframes. While the templates provides good results on intraday charts you can also test using daily or weekly timeframes and apply the concept to a broader portfolio of markets.