Simple MetaTrader MQL4 helper functions

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.

Tags: , ,

Leave a Reply

Do NOT fill this !