Monday, October 26, 2009

My first MQL5 Expert Advisor

Update 12/1/2010: this forward test now replaced with a Virtual Order Manager equivalent coexisting with the Support_Resistance EA.  See statement here and live screenshot here.  All links below are still valid.

This EA is a Moving Average Cross system using the Fractal Adaptive Moving Average.  It is almost certainly not a profitable EA - I've just used a simple system like this as an exercise in learning MT5.  Give it to me to code in MT4 and I'd have it done in literally 15 minutes, but this has taken me on an off about a week to code.

I decided to write an EA that is triggered using an indicator to really understand how things work.  The FraMa Cross indicator is here, and includes arrows showing long and short entries.  The default MA periods are 6 and 12.

There are various ways to code an always-in-the-market reversing EA such as this in MT5.  I simply reverse the position by sending an order for double the lots in the opposite direction, which illustrates one of the  largest differences between MT4 and MT5.

Files needed for this EA to compile and run:
FraMa cross EA V1_2.mq5 - the Expert Advisor itself. Store in MQL5/Experts
FraMa cross.mq5 - the indicator.  Store in MQL5/Indicators
EnumToStr.mqh - contains lookup tables for a couple of enums.  Store in MQL5/Include
SimpleLog.mqh - logging utitlity that I used to debug.  Store in MQL5/Include

Here is the statement of a live forward test, which is running on a EURUSD H1 chart with FastMA 6 and SlowMA 12.

[update] and here's a screenshot updated every 10 minutes.  You may need to use refresh on your browser to see updates (the picture to the left is an example only and doesn't update)

[update 2] I have uploaded a new version of the EA. It suddenly stopped working properly because I had made an error by not initialising a double in OpenLots().  A recent MT5 update must have stopped automatically initialising doubles.

[update 3]  The EA hasn't been working for a while - must have been a new build which broke it for some reason.  So I've recompiled after re-writing some of the code, such as OpenLots() (which doesn't need to loop through all positions as I'd orinally written it), and introducing a more versatile function called AdjustPosition().  The code is updated as Frama Cross EA V1_2.mq5


  1. Hello Paul,
    I have wrote about your indicator and ea: FraMa Cross Indicator and EA for MT5

    Greetings, Peter M.

  2. Thanks miranon. You will see that I have been referring people to your blog as well. For those following MetaTrader 5, miranon's blog at is a well kept and up to date resource.

  3. bro,Paul there many error when compile it ?

  4. What errors do you get? Need to make sure you have all 4 files stored in the locations I listed. The files that need compiling are the Frama Cross EA and the FraMa cross indicator.

  5. Forward test is looking good so far :D

  6. Thanks skajake, but don't get your hopes up. I doubt that this is a profitable EA in the long run - I just thought it would be worthwhile to forward test a reasonably sound system idea and witness how MT5 handles it. I will run it though the strategy tester when it becomes available, and perhaps some optimisation may make it truly profitable.

    Note that the forward test sometimes misses trades because I have to keep recompiling to keep up with the very frequent MT5 updates.

  7. hello

    I tried to convert mql4 to mql5 as per sugested procedure and always getting message"File not found..." Where should I place my .mql4 file in order that script can find it?

  8. Hi Anon, I didn't write the MQL4 to MQL5 converter and haven't tried it, but you would probably need to copy the MQL4 file into the experts\files folder. MT4 will only read files in that folder or its sub-folders.

  9. thanks to share this EA

    have some errors while compiling (release 324)

    'LogFile' undeclared identifier
    'LOG' fonction is not defined

    as i'm not a coder, i dont know what to do, the 2 mqh files are into /include

  10. I was looking at this blog with good information and guidance to become successful forex trader and to get the best from the forex market. Its helps currency exchange companies and valutahandel users.

  11. Thanks Forex. Due to lots happening in the rest of my life, I haven't added anything to it for quite a long time. I'll start again soon.

  12. Great discussion... what's the best timeframe for your EA?

  13. My first MQL5 Expert Advisor is an nice article.This post shows some news about the EA5 .

  14. I would like to recommend that you stick with the #1 Forex broker.