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

13 comments:

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

    Greetings, Peter M.

    ReplyDelete
  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 http://metatrader5.blogspot.com/ is a well kept and up to date resource.

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

    ReplyDelete
  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.

    ReplyDelete
  5. Forward test is looking good so far :D

    ReplyDelete
  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.

    ReplyDelete
  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?

    ReplyDelete
  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.

    ReplyDelete
  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

    ReplyDelete
  10. 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.

    ReplyDelete
  11. Great discussion... what's the best timeframe for your EA?
    ForexFox
    www.surfshop.it

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

    ReplyDelete