Indicator Values messed up

 

I did this small variation of the MACD indicator, where I only changed the moving average from Simple to Exponential, then added two buffers to make the indicator line change colors.

However, my indicator is bugged. After running for some time the values start getting messed up, and I get multiple values overlapping each other.

I guess the problem is that I process the bar zero again after it had been processed the first time, is that correct?

//+------------------------------------------------------------------+
//|                                                  Custom MACD.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"
#property description "Moving Averages Convergence/Divergence"
#property strict

#include <MovingAverages.mqh>

//--- indicator settings
#property  indicator_separate_window
#property  indicator_level1 0
#property  indicator_levelstyle 2
#property  indicator_levelwidth 1
#property  indicator_buffers 4
#property  indicator_color1  Lime
#property  indicator_color2  Red
#property  indicator_width1  2
#property  indicator_width2  2
//--- indicator parameters
input int InpFastEMA=13;   // Fast EMA Period
input int InpSlowEMA=25;   // Slow EMA Period
input int InpSignalSMA=5;  // Signal SMA Period
input ENUM_MA_METHOD Method = MODE_EMA;  // Method
input ENUM_APPLIED_PRICE Price = PRICE_CLOSE;  // Price
//--- indicator buffers
double    ExtLime[];
double    ExtRed[];
double    ExtMacdBuffer[];
double    ExtSignalBuffer[];
//--- right input parameters flag
bool      ExtParameters=false;
int linear_sum;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   IndicatorDigits(Digits+1);
//--- drawing settings
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,159);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,159);
   SetIndexStyle(2,DRAW_NONE);
   SetIndexStyle(3,DRAW_NONE);
   SetIndexDrawBegin(1,InpSignalSMA);
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtLime);
   SetIndexBuffer(1,ExtRed);
   SetIndexBuffer(2,ExtMacdBuffer);
   SetIndexBuffer(3,ExtSignalBuffer);
   
//--- name for DataWindow and indicator subwindow label
   IndicatorShortName("TAS Nav Clone("+IntegerToString(InpFastEMA)+","+IntegerToString(InpSlowEMA)+","+IntegerToString(InpSignalSMA)+")");
   SetIndexLabel(0,"Green");
   SetIndexLabel(1,"Red");
//--- check for input parameters
   if(InpFastEMA<=1 || InpSlowEMA<=1 || InpSignalSMA<=1 || InpFastEMA>=InpSlowEMA)
     {
      Print("Wrong input parameters");
      ExtParameters=false;
      return(INIT_FAILED);
     }
   else
      ExtParameters=true;
//--- initialization done
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
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[])
  {
   int i,limit;
//---
   if(rates_total<=InpSignalSMA || !ExtParameters)
      return(0);
//--- last counted bar will be recounted
   limit=rates_total-prev_calculated;
   if(prev_calculated>0)
      limit++;
//--- macd counted in the 1-st buffer
   for(i=0; i<limit; i++)
     {
      ExtMacdBuffer[i]=iMA(NULL,0,InpFastEMA,0,Method,Price,i+1)-
                    iMA(NULL,0,InpSlowEMA,0,Method,Price,i+1);
     }
//--- signal line counted in the 2-nd buffer
   ExponentialMAOnBuffer(rates_total,prev_calculated,0,InpSignalSMA,ExtMacdBuffer,ExtSignalBuffer);
   
   for(i=0; i<limit; i++)
     {
      double macd = ExtMacdBuffer[i];
      double macdma = ExtSignalBuffer[i];

      if(macd>macdma)
         ExtLime[i] = macd;
      else if(macd<macdma)
         ExtRed[i] = macd;
     }
   
//--- done
   return(rates_total);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
 
Naim El Hajj:

In future please post in the correct section.

I will move this to the MQL4 and MT4 section


 
Keith Watford:

In future please post in the correct section.

I will move this to the MQL4 and MT4 section


Sure sorry about that
 
Naim El Hajj: I guess the problem is that I process the bar zero again after it had been processed the first time, is that correct?

Yes. If you make it Lime, unmake it Red. EMPTY_VALUE

Reason: