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

Friday, October 16, 2009

My first MQL5 coding impressions

MQL5 is in beta test, and I've been playing around with it for a few hours to see what it's like.  It's great fun, and I can see the enormous potential for MetaTrader 5.

I know MetaTrader 4 backwards, and am also reasonably experienced in C++, but there is much in MQL5 that needs to be learnt.  The trading functions are completely different, and so is the coding of indicators.  It's not particularly useful to import MQL4 code into the MQL5 editor and attempt to convert it inline because the conversion needs to touch on the entire coding structure.  The search is on for the equivalents to anything that doesn't exist, like IsOptimization() or TimeToStr().  Other functions exist but need to be used differently, like StringConcatenate.

I guess one could write a whole lot of pseudo MT4 function calls such as this
bool IsTesting()
{
  return(MQL5_TESTING);
}

This may assist in converting MQL4 code, but it's probably simpler to re-write the simpler ones.

A startling change is that all inbuilt indicators return a handle, not a value. This handle is then used to copy results to an array. The reason for this is to make it easier and more efficient to code indicators of indicators.

Here's my first indicator.  The new concept in indicators is OnCalculate(), which is called for each new tick.  For some weird reason the arrays in OnCalculate are from left to right.  This indicator I wrote shows a fast and slow Fractal Moving Average (FRAMA) and places arrows at the open of each bar following an MA cross event.

I'm now working thorugh the complexities of which bar is which to use this indicator to control an Expert Advisor.

Here's a useful MQH include file which converts error codes and OrderSend results to strings.

Tuesday, October 13, 2009

The public beta for MetaTrader 5 has begun

You can download MetaTrader 5 here.  I recommend following the public beta thread directly on the mql4 forum.  At this stage, mql5.com is still inactive.

I have successfully downloaded MT5 and connected to the demo server.  Two points to assist:
  • The name field in the Open Account dialogue must be two words, implying first name and surname.
  • The compiled help file for MetaEditor is in Russian only at the moment.  fai on the mql4 forum has kindly translated it into English using Google Translate.  Open up this zip file and copy the contents as mql5.chm into .\MetaTrader 5\help.  You may need to be a member of the forum to do this.

Thursday, October 8, 2009

MetaTrader 5 public beta testing

At last - the public beta for MetaTrader 5 is due to start on 12th October!

I'm seriously thinking of starting up an MQL4 to MQL5 conversion and EA coding service.  In the last 12 months I've written MT4 EAs for free for quite a number of people.  I was hoping that the exercise would be worthwhile because I would be gaining valuable ideas in exchange, but in reality I haven't seen much new apart from a promising idea related to fixed range bars.

This new version of MetaTrader will generate an avalanche of coding requirements, so perhaps it's time to go commercial.  I should research how other service providers structure their charges, but one way would be to provide a compiled EX5 for a small cost, and charge say $100-$500 for the source code depending on complexity.  I'm looking forward to creating a set of module/objects which can easily be snapped into projects.