Trading help, book reviews, risk management, and interviews.

Disclaimer

The contents of this blog do not constitute investment advice. Trading CFDs and Forex can be very risky and you can lose much more than your initial deposit. Seek professional advice before making any trades.

Implementing an assert in MetaTrader MQL4

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");
      return;
      }

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.


Reader Feedback

2 Responses to “Implementing an assert in MetaTrader MQL4”

  1. Mike T says:

    Thanx for this – i can see this mql assert technique saving me a whole load of time. Debugging Metatrader is always a real pain! – Mike T

  2. Philippe says:

    Hi,
    You can still do a “division by 0”. That’s what I do…

    void ASSERT(bool condition, string message)
    {
    if (!condition)
    {
    double zero=0.;
    double terminate = 5./zero;
    }
    }

Leave a Reply

Do NOT fill this !