Posts Tagged ‘source code’

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.