OCHL ratio indicator Moving Average error

 

Hi,

I'm was looking for an indicator, but can't seem to find one. So I created my own, but can't figure out how to get the moving average value correct.

//+------------------------------------------------------------------+
//|                                                   OCHL ratio.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+

#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 1
#property indicator_buffers 1

input int MAPeriod = 100;
input ENUM_MA_METHOD MAMethod = MODE_SMA;

double BufferMA[];

#define MAIndicator 0
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexStyle(MAIndicator, DRAW_LINE, STYLE_SOLID, 1, clrGreen);
   SetIndexBuffer(MAIndicator, BufferMA);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   if(rates_total <= MAPeriod)
     {
      return (0);
     }

   int limit = rates_total -prev_calculated;

   if(prev_calculated > 0)
     {
      limit++;
     }

   for(int i=0; i < limit; i++)
     {

      double highLowPips = high[i] - low[i];
      double openClosePips;
      if(open[i] > close[i])
        {
         openClosePips = open[i] - close[i];
        }
      else
        {
         openClosePips = close[i] - open[i];
        }
     
         double OCHLRatio = openClosePips / highLowPips;
       
      double OCHLRatioMa = iMA(Symbol(), Period(), MAPeriod, 0, MAMethod, OCHLRatio, i);


      Print("High: " + high[i] + ", Low: " + low[i] + ", Open: " + open[i] + ", Close: " + close[i] + ", highLowPips: " + highLowPips + ", openClosePips: " + openClosePips + ", OCHLRatio: " + OCHLRatio + ", OCHLRatioMa: " + OCHLRatioMa + ", Period: " + Period());
    
      BufferMA[i] = OCHLRatioMa;

     }
   return (rates_total);
  }
//+------------------------------------------------------------------+



If you take a look at the code then all values are correct until the return of iMA(). You can see the result for each day on the image. For somereason it is 0.98 on average. You can also see the green line on the indicator below.

Does anyone know why it is not returning the correct moving average? :)


Thank You,

Hendrik

 
  1. Your code
          double openClosePips;
    
                    
          if(open[i] > close[i])
            {
             openClosePips = open[i] - close[i];
            }
          else
            {
             openClosePips = close[i] - open[i];
            }
    Simplified
     double openClosePips = MathAbs(open[i] - close[i]);
  2. double OCHLRatioMa = iMA(Symbol(), Period(), MAPeriod, 0, MAMethod, OCHLRatio, i);
    The second from the last is a price enumeration — you pass a value. Bogus. Research iMAOnArray.
 

Thank you William.

After hassling with iMAOnArray I got it to work.


I have attached the indicator, if anyone need such a ratio and moving average.


This is how it looks like with 10 day MA.


Files:
Reason: