Indicator to plot gradient MA - A little help...

 

I am trying to create an indicator to plot the change in gradient of the NonLagMA indicator as a Histogram. What I have done works fine when you first place it on a chart but gets messed up as soon as a new bar appears. Could someone please take a look at the code and show me where I am going wrong? Note: I had to remove some code from the init() function as the post was too long. Nothing that should have any effect on the problem I am having though.


#property indicator_separate_window
#property indicator_buffers 5

#property indicator_color1  LimeGreen
#property indicator_color2  Red
#property indicator_color3  Maroon
#property indicator_color4  DarkGreen 
#property indicator_width1  4
#property indicator_width2  4 
#property indicator_width3  4 
#property indicator_width4  4
#property indicator_width5  1  

//*****************************************************************************
// External Options                                                       
//*****************************************************************************
extern int     TimeFrame      = 0;
extern int     Price          = 4;//Apply to Price(0-Close;1-Open;2-High;3-Low;4-Median price;5-Typical price;6-Weighted Close) 
extern int     Length         = 13;
extern double  A_Factor       = 1;  //Amplitude factor 
extern int     S_Factor       = 0;  //Smoothing Factor
extern double  G_Factor       = 1;  //Geometric factor 
extern double  PctFilter      = 0;
//----+ +---------------------------------------------------------------------------+

//*****************************************************************************
// Program Global Variables                                                     
//*****************************************************************************
double ind_buffer1[], ind_buffer1s[], ind_buffer2[], ind_buffer2s[]; 
double MainBuffer[];

string TF, sPrice, short_name;
//+==================================================================+
//| Custom Expert initialization function                            |
//+==================================================================+
int init() {

   short_name = StringConcatenate("NonLagMA_Gradient_Hist"," (",TF,")","(",sPrice,")","(",Length,")");
   IndicatorShortName(short_name);
   
   SetIndexBuffer(0,ind_buffer1);
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexLabel(0,"Lime");

   SetIndexBuffer(1,ind_buffer1s);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexLabel(1,"Red");

   SetIndexBuffer(2,ind_buffer2);
   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexLabel(2,"Maroon");

   SetIndexBuffer(3,ind_buffer2s);
   SetIndexStyle(3,DRAW_HISTOGRAM);
   SetIndexLabel(3,"DarkGreen");
   
   SetIndexBuffer(4,MainBuffer);
   
   SetIndexEmptyValue(0, 0);
   SetIndexEmptyValue(1, 0);
   SetIndexEmptyValue(2, 0);
   SetIndexEmptyValue(3, 0);
   
   return (0);
}//end init()

int deinit() {
   return (0);
}//end deinit()

int start() {

   datetime TimeArray[];
   int      i,shift, counted_bars=IndicatorCounted(),limit;
   double   price;         
   double MAValue1,MAValue2;
   double Value0,Value1,Value2,Gradient01,Gradient12;
   
   //--- the appropriate limit will depend on the timeframe selected
   if(TimeFrame==0)         TimeFrame = Period(); 
   if ( counted_bars > 0 )  limit=Bars-counted_bars+TimeFrame/Period();
   if ( counted_bars < 0 )  return(0);
   if ( counted_bars ==0 )  limit=Bars-TimeFrame/Period(); 
   if ( counted_bars < 1 ) {  //--- initialize buffers to zero
      for(i=0;i<Bars-1;i++) {//for(i=1;i<5*Length;i++)
         ind_buffer1[i]=0;//IndicatorValue;  
         ind_buffer1s[i]=0;//IndicatorValue;
         ind_buffer2[i]=0;//IndicatorValue;  
         ind_buffer2s[i]=0;//IndicatorValue;    
      }//end for(i=0;i<Bars-1;i++)
   }
   
   ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame);
   /*
   extern int     Price          = 5;  //Apply to Price(0-Close;1-Open;2-High;3-Low;4-Median price;5-Typical price;6-Weighted Close) 
   extern int     Length         =14;  //Period of NonLagMA
   extern int     Displace       = 0;  //DispLace or Shift 
   extern double  A_Factor       = 1;  //Amplitude factor 
   extern int     S_Factor       = 0;  //Smoothing Factor
   extern double  G_Factor       = 1;  //Geometric factor 
   extern double  PctFilter      = 0;  //Dynamic filter in decimal
   extern int     Color          = 1;  //Switch of Color mode (1-color)  
   extern int     ColorBarBack   = 1;  //Bar back for color mode
   extern double  Deviation      = 0;  //Up/down deviation        
   extern int     AlertMode      = 0;  //Sound Alert switch (0-off,1-on) 
   extern int     WarningMode    = 0;  //Sound Warning switch(0-off,1-on) 
   extern int     CountBars      = 0;  //Maximum Number of Bars(0-all Bars)
   */ 
   //--- load the MA values into buffer
   for(shift=0,int y=0;shift<limit;shift++) {
      if (Time[shift]<TimeArray[y]) {
         y++;
      }   
      MainBuffer[shift] = iCustom(NULL,TimeFrame,"NonLagMA_v7.9",Price,Length,0,A_Factor,S_Factor,G_Factor,PctFilter,1,1,0,0,0,0,y);
      //Print(MainBuffer[shift]);
   }//end for(shift=0,int y=0;shift<limit;shift++) {
   
   for (int bar=limit-1;bar>=0;bar--) {
   
      Value0 = MainBuffer[bar];//these values are always positive
      Value1 = MainBuffer[bar+1];
      Value2 = MainBuffer[bar+2];
      Gradient01 = MathAbs(Value0 - Value1);
      Gradient12 = MathAbs(Value1 - Value2);
      
      //if the gradient has increased
      if(Gradient01>Gradient12) {
         //if the current MA value is greater than the previous, i.e. the gradient is positive
         if(Value0 > Value1) {
            ind_buffer1[bar]=Gradient01;
            ind_buffer1s[bar]=0.0;             
            ind_buffer2[bar] =0.0;
            ind_buffer2s[bar]=0.0;
         } 
         //else if it is lower       
         else {
            ind_buffer1[bar] =0.0;
            ind_buffer1s[bar]=Gradient01;           
            ind_buffer2[bar] =0.0; 
            ind_buffer2s[bar]=0.0;
         }             
      }//end if(Value0>Value1)
      //else if the gradient has decreased
      else {
         //if the current MA value is less than the previous, i.e. the gradient is negative
         if(Value0 < Value1) {
            ind_buffer1[bar] =0.0;
            ind_buffer1s[bar]=0.0;
            ind_buffer2[bar] =Gradient01;
            ind_buffer2s[bar]=0.0;
         }
         //else if the gradient has decreased but the MA is still higher (i.e. it is slowing during an upward move)  
         else {
            ind_buffer1[bar] =0.0;
            ind_buffer1s[bar]=0.0;
            ind_buffer2[bar] =0.0;
            ind_buffer2s[bar]=Gradient01;
         }           
      }//end else
   }//end for (int bar = 0; bar < limit; bar++) {

   return (0);
}//end start
Reason: