Posts Tagged ‘mql4’

MQL4 helper functions for calculating bar sizes

Sunday, February 21st, 2010

Here are some more MQL4 helper functions for MetaTrader. You can use them in your expert advisors and custom indicators. The three functions will allow you to quickly get the size of the upper and lower wicks of the candle stick, and also the size of the candle stick body. To use them you will also need some of the code I gave you in my simple MetaTrader MQL4 helper functions post.

If you use these functions in your code please give a reference to this blog post.

double UpperWickSize(int bar)
   {
   if (UpBar(bar))
      {
      return (High[bar]-Close[bar]);
      }
   if (DownBar(bar))
      {
      return (High[bar]-Open[bar]);
      }
   return (0);
   }
double LowerWickSize(int bar)
   {
   if (UpBar(bar))
      {
      return (Open[bar]-Low[bar]);
      }
   if (DownBar(bar))
      {
      return (Close[bar]-Low[bar]);
      }
   return (0);
   }
double BarBodySize(int bar)
  {
  if (UpBar(bar))
    {
    return (Close[bar]-Open[bar]);
    }
  if (DownBar(bar))
    {
    return (Open[bar]-Close[bar]);
    }
  return (0);
  }


Simple MetaTrader MQL4 helper functions

Saturday, January 30th, 2010

Here are some simple functions that you can use in your forex expert advisors and custom indicators written in MQL4 for MetaTrader.

If you split out commonly used functionality into separate functions with logical names (as I’ve done here) then you should find that your MQL4 code becomes more readable.

If you use these functions in your own code please reference this blog.

// returns the value of the top of the bar
double TopOfBar(int bar)
   {
   return (MathMax(Open[bar], Close[bar]));
   }
// returns the value of the bottom of the bar
double BottomOfBar(int bar)
   {
   return (MathMin(Open[bar], Close[bar]));
   }
// true if this is a down bar
bool DownBar(int bar)
  {
  return (Close[bar]<Open[bar]);
  }
// true if this is an up bar
bool UpBar(int bar)
  {
  return (Close[bar]>Open[bar]);
  }
// size of whole bar including the wicks
double HighLowSpread(int bar)
   {
   return (High[bar]-Low[bar]);
   }
// gives you the result of subtracting the lowest input value from the highest
double HighMinusLow(double value1, double value2)
   {
   double high = MathMax(value1, value2);
   double low = MathMin(value1, value2);
   return (high-low);
   }

Hope you find these useful.

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");
      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.