Cointegrated Pairs Trading Robot
IMPORTANT NOTE: This robot trades two or more symbols at the go and as such it can not 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.
Cointegrated Pairs Trading Robot is an expert advisor executable on any PC running Metatrader 4. A linear regression is applied on two instruments’ (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. It is recommended that there be $50 for every 0.01 lots (standard). Now given that this robot opens a pair of orders in each of its entry phase, the minimum deposit will be between $150 and $200. Withdrawals should be made with care not to violate the funds-to-lot relationship as stated above. This model has a sufficiently low risk and good returns in a long run. 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.
2. OpMode selects operation mode from four (values 01, 02, 03 & 04).
3. Magic & magik are magic numbers (should never be the same) used to uniquely identify trades opened by this robot. As such, every magic number should be a uniquely chosen positive integer.
4. MaxRatio limits the slope relating the two traded instruments.
5. CurLevel is the current level of residual process relative to Standard deviation.
6. LimLevel is the maximum level you allow the residual process to reach. This level acts as a stop loss level since everything is closed at this level.
7. AlertsOn allows you to either see or hide Alerts during the robot operation.
8. ModeDemo is the switching Boolean parameter. When true the model will work on Demo accounts but if false, it will work on live accounts.
9. Lots is the nominal lot size. Minimum usually 0.01.
10. Scale is a scaling factor used to modify the Standard deviation when necessary (i.e. When standard deviation is less that spread of the two instruments the it has to be scaled up to avoid all profits being taken up by spread).
11. Multiplier works in a similar spirit to Scale but rather than being multiplicative on Standard deviation, it is additive to it. That is, it multiplies the total spread and add it to Standard deviation to bias/threshold it.
12. pVThreshold is the threshold for p-Value (in percentage) above which the cointegration is considered weak and trading stops.
13. RSThreshold is the threshold for R^2 goodness of fit measure below which trading will not happen.
14. CCThreshold is the threshold for correlation coefficient measure below which no trading will happen.
15. MarginLevel sets the minimum Margin level below which trading stops.
16. Symbols are two instruments to be tested for cointegration and traded if the test succeeds.
17. Username and Password already provided by default.
18. TFrame is the time frame for the price data used.
19. Update is the time frame for updating the cointegration model.
20. Parallelism allows EAs to run in parallel.With smaller model sizes the standard deviation tends to become small and if it is so small that it is on the order of the instrument spread then the system will result in losses due to symbol spread. As shown in the last figures above, model size reduction is only sensible when the symbol spread is very close to zero or exactly zero. For safety stick with higher model sizes (>500) and higher timeframes (>= 1Hour).