Download MetaTrader 5

Correlation EA not opening pairs on both sides of trade

To add comments, please log in or register



This topic does not represent financial investing or trading advice -- it is merely ideas I have been looking at and wanting to put into a functional EA. 


Main idea

Pairs that are highly correlated (always move in same direction) sometimes go slightly out of correlation, but long term will get back in correlation.

When this happens, the idea is to sell the higher priced pair, and buy the lower priced pair, creating a crude hedge. Over time the correlation mismatch closes up, and the EA will take profit.


So far

Pick 2 correlating pairs

Calculate long term simple moving average (I currently use 52 weeks) .

Calculate the current price as a percentage of the long term SMA.

Calculate the percentage difference between the previous time period and the current time period to get a percentage change.

If large enough percentage difference between the pairs, Sell the higher priced pair, Buy the lower price pair. (current using 1 microlot)

Check every 3 seconds for a profit on the account. If at profit level, close all positions.

With the attached EA code, I have a number of problems:

Sometime multiple positions are opened on one side (say buy side) but none on the sell side. I cant figure what is wrong with the logic. A bit tricky to test this historically, as MT4 only allows you to test with one currency pair. I have run this against a demo account.

Any assistance or advice appreciated.


Sergey Golubev
Sergey Golubev  

As I am not a coder so I can provide a standard reply only sorry :


  • Usually people who can't code don't receive free help on this forum, though it could happen if you are lucky, be patient.
  • If you show your attempts and describe well your problem, you will most probably receive an answer from the community.
  • If you don't want to learn to code, nothing bad, you can either look at the Codebase if something free already exists, or in the Market for paid products (sometimes free also).
  • Finally, you also have the option to hire a programmer in the Freelance section.

Good luck.

I just hope someone will help.

Ernst Van Der Merwe
Ernst Van Der Merwe  

The problem is probably here:

   for(Count=TotalOrders-2; Count<TotalOrders;Count++)
To add comments, please log in or register