# Pairs Trading

**Pairs Trading** Expert Advisor automates the execution and position management of a very popular speculative investment strategy used in Wall Street called **pairs trading**.

**Pairs Trading** Expert Advisor is a powerful tool that will do all the **simultaneous calculations and position management** for you, open the combined pair position when the entry level is breached, take your profit upon reversion or stop your losses should the pair’s long-term relationship brakes, letting you focus on your statistical-business analysis and thus maximize your profits.

###
Input parameters

Lets assume that we have identified a long-term relationship between **Derwent London plc (DLN) **and **Great Portland Estates plc (GPOR)** for the period between 2015/01/01 and 2015/06/30. Moreover lets assume that after applying an econometric research analysis we have found that the long-term relationship between **Derwent** and **Great Portland** is given by the following equation:

Derwent=-576.41+4.964*Great Portland+error

Given the above equation, the **Pair’s Trading **price is:** Pair’s Spread = Derwent-(-576.41+4.964*Great Portland) = error** (Screenshot1)

The **error** term is a stationary time series, meaning that it fluctuates around its long-term mean, and our actual profit generation component. For the abovementioned example and based on our sample observations the statistical characteristics of the error are the following (Screenshot2):

**Mean: 0.189,Standard Deviation: 50.186,**** Skewness: 0.578, Kourtosis: 2.818**

**Stocka**: Is the symbol of the first pair component. In our example**DLN**for the Derwert London plc.

**Stockb**: Is the symbol of the second pair component. In example**GPOR**for the Great Portland Estates plc.

Before typing any pair symbol please make sure that the corresponding investment instruments are added to the navigator window. To add an instrument you should right click on navigator window, go to Symbols, select the desired instrument and then click on the show button.

**Lotsize**: Is the bet size in lots. For example if the input value is 1, then the combined position for the pair will consist of 1 lot Long/Short on**Stocka**and**1*****IntegratingCoef**Short/Long on**Stockb**.**Intercept:**Corresponds to the intercept of our long-term relationship. In our example the intercept is -576.41.

**Integrating Coefficient**: Is the co-integrating coefficient. In our example 4.964.

**Residual_Mean**: Theoretically the error is a random variable that has a normal distribution with mean 0 and standard deviation 1. In reality the error’s mean might be different than zero. Residual_Mean is our errors’ sample mean (**0.189**) and the price to which the pair’s spread will revert to.

**Threshold**: It is the distance, in absolute value, above and below the Residual_Mean (**0.189**), that if breached (*) then a position on the pair will be opened.

(*) In order for a position to be opened by Pairs Trading Expert Advisor the spread should be between the entry level and the stop loss level.

Residual_Mean+Threshold<=Pair's Spread<Residual_Mean+StopLoss Or Residual_Mean-Threshold>=Pair's Spread>Residual_Mean-StopLoss

**StopLoss**: It is the distance, in absolute value, above and below the Residual_Mean (**0.189**), that if breached then an already opened position will be closed.**Magicnoumbe**: Is the value that will be utilized by the**Pairs Trading**Expert Advisor in order to distinguish the opened positions on each pair. It is highly important to put a different**integer**magic number to different pairs. Additionally, if for some reason the window to which the Pairs Trading Expert advisor is applied gets closed, then after reopening the window and redeploying the EA the same magic number should be input, otherwise the opened positions**will be left unmanaged**.

Please note the following:

- Pairs Trading EA in its current version does not perform pre trade checks for fund availability.
- Up until now the MetaTrader4 strategy tester can be applied only on one instrument at a time. This means that Pairs trading EA unfortunately cannot be tested on Strategy Tester but only on demo accounts. Exactly for this reason I have placed a extremely low price for monthly rental so that Pairs Trading can be tested on a demo account.