Statistical Arbitrage Cointegrated Pairs Trading
- Experts
- Naleli Jubert Matjelo
- Version: 1.1
- Updated: 31 July 2024
- Activations: 10
This robot trades a pair of two symbols at the go and as such it cannot be tested with strategy tester in Metatrader 4. The reason being that Metatrader 4 strategy tester cannot synchronize price timing for more than one symbol at the time. To test this robot successfully, one has to run it in real-time on a demo account and monitor it for days.
Statistical Arbitrage Cointegrated Pairs Trading Robot is an expert advisor executable on any PC running Metatrader 4. A linear regression is applied on two symbols (i.e. Stocks or Forex symbols) historical prices and the cointegration test is performed on the residual from the regression. If the two instruments are cointegrated then the residual will be a stationary process (i.e. white noise) which is a lot more predictable than the individual instrument price which is often nonstationary (i.e. random walk) thus hard to predict. This robot is based on trading such a stationary residual process whereby it exploits the mean-reversion property of stationary processes. That is, when conditions allow, the robot will always bet on the residual crossing the mean from time to time.
In its usage, the user will need to register with one broker of choice. The required minimum deposit per traded pair depends very much on the set lot size and the measured standard deviation (shown as the right-most number on displayed Regression Model). For example, if the LotSize is set to 0.01 standard lots (which is 1000 units), LotRatio set to 2.00 and the measured standard deviation is 0.0070, then the recommended minimum deposit for trading just that pair alone will be 7.5*(2.00 + 1)*1000*$0.0070 = $158.00. When taking into account margin requirement and floating losses, it is always better to have more than the minimum required funds (i.e. $200 rather than $158 is a good choice for the example above). This model has a sufficiently low risk and good returns in a long run but it is not a get-rich quick scheme (it is more of an investment strategy and as such needs patience as a trade can a week or so before closing). However, the entry opportunities are rare (i.e. can go several days without any good entry for one chosen pair of instruments) so the user might compensate for this by trading several pairs to make up for the rareness of the entry & exit opportunities.
Input Parameters:
1. N is the data size for building linear regression model. By default, this is set to N = 9000. Lowering this value can affect the strength or accuracy of the robot.
2. Magic is the magic number used to uniquely mark/identify trades opened by this robot in the presence of other robots. By default, this is set to magic = 1000. As such, every instance of this robot (if you run multiple instances on the same MT4 app) should have unique magic number and the difference between any two chosen magic number should be atleast 6 to avoid overlaps that can confuses the robot. This is because when you set a magic number as 1000, the program internal creates others magic numbers from 1001 up to 1005. If you run another instance of this robot, you should not choose a magic number in the range from 1000 to 1005 otherwise the robot will end up being confused and close wrong trades due to overlapping magic numbers.
3. LotSize is the nominal lot size. By default, this is set to LotSize = 0.01. It can be changed depending on how much funds are available in the account.
4. LotRatio represents the lot size ratio (1.00 : LotRatio) of the two symbols formig a pair. This means for each complete transaction, a total of (LotRatio + 1)*LotSize lots will be used. As such LotRatio should not be set so high that one transaction ends up taking a large fraction of the funds. If a traded pair requires too much LotRatio, it is best to abandon that pair and look for others. By default LotRatio is set to 2.00. LotRatio should only be increased if the funds allow, otherwise change the traded pair to one which require smaller LotRatio.
5. Symbols are two instruments/symbols that form one traded pair. By default this is set to Symbols It is best to choose two symbols that are already known to be related (positive or negative comovement or correlation). If the chosen symbols do not show good evidence of relationship (either goodness of fit of less than 0% or correlation coefficient below 25% or cointegration p-Value measure of atleast 10%) the robot will not open any trade.
6. TimeFrame is the time frame for the price data used. By default this is set to 1 hour.