Custom Indicator for MA slope is calculating wrong

 

Hi guys, I don´t know what else to do now, so I ask for a bit of help. I´ve been struggling for several days now. I´m rewriting a MQL4 indicator that I created into MQL5. I just want the MA slope, and I normalize it with the ATR, so then I can aggregate this indicator values with the same indicator in other pairs.

The logic for each calculation point is basically is (MA[i] - MA[i-1]) / ATR[i], so bear in mind that I need one value more in the MA buffer than in the ATR buffer.

Finally I don´t have any more "out of range errors", but I don´t know what I´m doing wrong to get this odd calculations. Let me copy an screenshot, and you will see that when the MA is going down the Indicator is still positive but it should be below zero.

Wrong calculations

I´ll copy the whole indicator below. I suspect that it is something in the way an indicator values is read (from X to 0) and how the CopyBuffer array is read (from 0 to X) but I do travel the buffer from 0

//+------------------------------------------------------------------+
//|                                     AMG Slope Diff Histogram.mq5 |
//+------------------------------------------------------------------+
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_level1  0

input int     MaPeriod          = 10;

int ma_handle = 0;
int atr_handle = 0;

double values[];
double ma_values[];
double atr_values[];

int OnInit()
{
   ma_handle = iMA(Symbol(), PERIOD_CURRENT, MaPeriod, 0, MODE_SMA, PRICE_CLOSE);
   if(ma_handle==INVALID_HANDLE)
   {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(PERIOD_CURRENT),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
   }
   
   atr_handle = iATR(Symbol(), PERIOD_CURRENT, MaPeriod);
   if(atr_handle==INVALID_HANDLE)
   {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iATR indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(PERIOD_CURRENT),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
   }

   SetIndexBuffer(0, values, INDICATOR_DATA);
   PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_LINE);
   PlotIndexSetInteger(0, PLOT_LINE_STYLE, STYLE_SOLID);
   PlotIndexSetInteger(0, PLOT_LINE_COLOR,clrBlack);  

   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
   // Release the MA indicator handle
   IndicatorRelease(ma_handle);
   IndicatorRelease(atr_handle);
}

int OnCalculate(const int rates_total, const int prev_calculated, const int begin, const double &price[])
{
   if(rates_total<MaPeriod+1) return(0);

   int first,bar;
   
   if(prev_calculated==0)      // checking for the first start of the indicator calculation
   {
      first=MaPeriod;    // starting number for calculation of all bars
   }
   else
   {
      first=prev_calculated; // starting number for calculation of new bars
   }
   
   if (CopyBuffer(atr_handle, 0, first -1, rates_total - first + 1, atr_values) < 0) 
   {
      Print("CopyBufferAtr error =",GetLastError());
      return(0);
   }
   if (CopyBuffer(ma_handle, 0, first -1, rates_total - first + 1, ma_values) < 0) 
   {
      Print("CopyBufferSMA error =",GetLastError());
      return(0);
   }
   
   for(bar = first + 1; bar < rates_total; bar++)
   {
      int buffer_index = bar - first;
   
      double atr = atr_values[buffer_index];
      double diff = ma_values[buffer_index] - ma_values[buffer_index - 1];
      
      // I do this double step because there is an issue that I havent identified where
      // the result of the operation is a massive number
      double temp = diff / atr;  
      values[bar] = temp > 1 ? 1 : temp < -1 ? -1 : temp;
   }
   
   return(rates_total);
}

Any help is massively appreciated.

 
fonx:

Hi guys, I don´t know what else to do now, so I ask for a bit of help. I´ve been struggling for several days now. I´m rewriting a MQL4 indicator that I created into MQL5. I just want the MA slope, and I normalize it with the ATR, so then I can aggregate this indicator values with the same indicator in other pairs.

The logic for each calculation point is basically is (MA[i] - MA[i-1]) / ATR[i], so bear in mind that I need one value more in the MA buffer than in the ATR buffer.

Finally I don´t have any more "out of range errors", but I don´t know what I´m doing wrong to get this odd calculations. Let me copy an screenshot, and you will see that when the MA is going down the Indicator is still positive but it should be below zero.


I´ll copy the whole indicator below. I suspect that it is something in the way an indicator values is read (from X to 0) and how the CopyBuffer array is read (from 0 to X) but I do travel the buffer from 0

Any help is massively appreciated.

I´ve found the issue. Nothing like posting in a forum to have the aha moment. values[bar] is wrong.