This tip explains how to build market neutral pairs trading models in NeuroShell. We will describe the following, each with a sample chart:
1. How to build trading strategies (with protective stops) that go long on one stock at the same time they go short on another in the same chart
2. How to use neural networks to build a linear combination of one stock to compute a proxy price, thus creating a co-integrated pair for pairs trading
3. How to optimize a pairs trading strategy in a chart
The methods in this article are somewhat complicated, and may take a while to learn and understand. However, we strongly suggest that you understand the concepts here before you try to utilize our sample charts. There is a link to download these charts at the end of this article, but screen shots of two of them are included in the article.
Overview
There are many ways that pairs of instruments (usually equities) can be “pairs traded” in a market neutral fashion, but the purpose of this article is not to explain the various theories and their validity, but only to show the mechanics of trading a general method in NeuroShell. In all of our examples we make the assumption that the stocks we used did not experience any splits during the backtesting periods.
How to build trading strategies with stock pairs
In the general pairs trading method, you pick two stocks with prices that closely follow one another over time, and in fact cross frequently. Consider the stocks WYE and GSK as displayed in the following screenshot:
Since mid 2002 the two stocks seem to follow one another closely, and would be good candidates for pairs trading. For example, look at mid 2003, when the difference between the stock prices was high. In pairs trading, one might have sold WYE short at that time and taken a long position in GSK in equal dollar amounts. Before the end of the year, the prices would have converged and one would have a nice profit. Other nice opportunities occurred through the years.
The question becomes how to make these market-neutral opposite trades in NeuroShell when both stocks are in the same chart? Usually NeuroShell applies the same rules in the same direction to both chart pages (stocks). The key is to let NeuroShell distinguish between the two chart pages, and know which one it is processing, so it can determine whether to go long or short. The way to do that is to create two indicators, which we call:
WYE Close
GSK Close
Let’s un-hide some things in the chart:
GSK is loaded and the GSK Close indicator is 1 (true). The WYE Close indicator is 0 (false). This tells NeuroShell it is working on the GSK chart page. The way we built these indicators is to compare the last 10 close values in the chart to the last 10 Glaxo and Wyeth closes that we loaded as Other Instrument Data.
Next we need to decide which one goes long and which short. As you can see from the difference between the closes (Diff = GSK – WYE) in the chart, when Diff >4, say, we should go long WYE and short GSK. We should do the opposite when Diff < -4. Our long entry rule does that, except that we use crossovers above and below 4 and -4 instead of > and
The exit rules are the same for both long and short. Everybody exits when the absolute value of the spread is much smaller than 4 (profitable exit on convergence), or when the spread is much larger than 4 (protective stop on divergence).
Here are the rules we used:
In order to be as market neutral as we can, we buy and short equal dollar amounts.
How to use neural networks to build a linear combination of one stock to compute a proxy price
The stock prices of the pair do not have to actually cross to create a tradable pair, but it will be harder to decide when to trade unless you can transform one stock so that it becomes co-integrated with other. This means one stock computed as a linear combination of the other, such that the stocks follow one another in a stable fashion and cross prices. This is pretty easy to do in NeuroShell using the Prediction Wizard. Take a look at this chart:
Here we want to build a pair with BMY as one of the members. Neither WYE nor GSK are at the right price level to form a tradable pair with BMY. So we built a Prediction to compute an appropriate partner for BMY. There are two inputs, WYE and GSK. The output to be predicted is BMY. If the Prediction Wizard is set to zero hidden neurons, it will make a linear model, which is what we want. The following settings are also necessary:
1. Predict 0 bars ahead
2. Use no trading positions
3. Objective function is minimize mean squared error
4. Maximum of 1 input used
You can see on the top subgraph that the linear model made by the prediction Wizard is nicely co-integrated with BMY, and the Wizard picked WYE for the computation. So we will be pairing BMY with WYE.
Caution – to assure a good hedge, make sure that the prediction is correlated (moves in the same direction) as the input (WYE in this case). Sometimes the best fit prediction is an inverse of the input, which destroys the hedge.
However, the difference computation will be the difference between BMY and the predicted BMY made from WYE. In the next screenshot we show the model made the same way we made the WYE – GSK model:
How to optimize a pairs trading strategy in a chart
The next question is “How do I figure out the correct spreads for entry, exit, and protective stop?” The NeuroShell optimizer is the obvious answer. All we should have to do is find the best spread values as we optimize the trading strategy. This is pretty straight forward, except for four things:
1. We must link the parameters in the long entry rule with the corresponding parameters in the short entry rule, so that they optimize to the same value. When linking the parameters in the entry rule, you will need different link names for the negative spreads and positive spreads.
2. When setting parameter ranges, make sure you do not optimize the lookbacks for computing the sums which are compared to see which stock we are dealing with. Set the range of each of those to 10 to 10. (The final chart in the series which shows optimization of the trading strategy used 5 to 5, but 10 is better)
3. We must link the similar parameters in the exit rules as well
4. We must optimize across all chart pages so that parameter values in one stock match the corresponding ones in the other stock
Additional Notes
After we built our charts we realized that instead of re-naming our identifying indicators “GSK Close” and “WYE close”, it would have been more clarifying to call them “GSK chart page” and “WYE chart page”.
If you want the exact formula for the linear transformation, you can use ChaosHunter to find it.
When we used the neural net to find the non-linear combination, we told the optimizer we wanted no more than 1 input. If you are OK with a non-linear combination of more than one stock, you can set the max number of inputs to 2, or 3, or whatever. Setting the hidden neurons to more than 0 allows on-linear combinations, but then might be difficult to decide which stocks need to be shorted and which not.
Further efforts
Although the methods in this article work fine, they are a little tedious. Our Advanced Indicator Set 3 add-on contains indicators to make the work a little easier.
Click here to download the three charts we used in this article. Data is saved in these charts.