Posts Tagged ‘expert advisor’

Implementing an assert in MetaTrader MQL4

Wednesday, November 25th, 2009

MQL4 is a very useful programming language for creating expert advisors to automatically trade FOREX. However the language does have many omissions which you will soon notice if you are used to programming a more complete language such as Java or C++.

One of the big omissions is that there is no built in way to do an ‘assert’ when running an expert advisor. If some kind of exceptional condition occurs that would make any further running of the expert meaningless there is nothing you can do as you can’t exit the current run programmatically.

This can waste a lot of time while you wait for the expert to complete. Something could have gone wrong in the depths of your code and you might not even realise it until the expert has completed – if at all!

On one website I did see the suggestion to implement an MQL4 assert by printing out the word ‘ASSERT’ along with your debugging message and then searching for it in the logs after the run has complete.

This is very unsatisfactory as it requires you to manually search for the tag, and you will only know if the assert has happened if you actually look for it.

My solution won’t stop the expert from running immediately (there is no way to do this in MQL4). But what it will do is to stop any further parts of your code from being executed by stopping the start() function from doing anything. This will mean that if an assert happens your expert will finish executing very quickly and the ‘assert’ message will be near the top of the log – so you don’t have to hunt for it.

Here are the code snippets you need.

// This goes near the top of your .mq4 file
bool iASSERT = false;
// Assert if the condition is true
void ASSERT_IF(bool condition, string message, string function)
   if (condition)
      ASSERT(message, function);
// Unconditional assert
void ASSERT(string message, string function)
   Alert("*** ASSERT: ", message, ", function=" + function + " ***");
   iASSERT = true;
// Rest of your code goes here…
// You add one line to the beginning of your start function. The rest of your
// start function is unaffected.
void start()
   if (iASSERT) return;
   // rest of your start function

How do you use these asserts? The first example is using the unconditional assert. Your expert will stop executing when the next tick is passed to the expert (via the start function).

if (type == StopTypeFast || type == StopTypeSlow)
      ASSERT("Stop type not valid here: " + type, "UpdateStops");

The next example is using the condition assert. The ‘assert’ only fires (and the expert execution is halted) if fractionalPips is bigger than 1.

int ToIntPips(double fractionalPips)
   ASSERT_IF(fractionalPips>1, "fractionalPips>1", "ToIntPips");
   return (fractionalPips*10000);

You can see from the code that the ASSERT function is not strictly necessary as both behaviour can be implemented in terms of ASSERT_IF. However having both versions gives you more flexibility in how your code works.