Indicator not refreshing values - EMA Predictive

 

Hey guys!

I found an indicator online EMAPredictive3 by Matthew "Dr Chaos" Kennel.

The only problem with it is that it doesn't really update. I have to click refresh after every tick, otherwise it will look overlapped (as in the picture attached).

I really tried to do something with RefreshRates() as somebody in another topic suggested, w/o luck. 

Can anyone point me in the right direction or even fix it? I would highly appreciate it!!!


#property copyright "Matthew (Dr Chaos) Kennel"
#property link      "ftp://lyapunov.ucsd.edu/pub/nonlinear" // my academic software site.  No finance.

#property indicator_chart_window
#property indicator_buffers 1

#property indicator_color1 Red
//---- input parameters
extern double       LongPeriod=25.0;  // note that for an EMA you can have floating point numbers. 
extern double       ShortPeriod=8.0;
extern double       ExtraTimeForward=1.0; // how much to further extrapolate location. 

//---- buffers
//---- indicator buffers
double ExtBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexBuffer(0,ExtBuffer);
 //---- drawing settings
   SetIndexStyle(0,DRAW_LINE);
//---- initialization done
   return(0);
  }


int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();
   double ma1,ma3;
   double p1,p3; 
   double t1,t3,t;

//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- main loop
   p1 = 2.0/(LongPeriod+1.0);
   p3 = 2.0/(ShortPeriod+1.0); 
   
   t1 = (LongPeriod-1.0)/2.0;
   t3 = (ShortPeriod-1.0)/2.0;
   t = ShortPeriod + ExtraTimeForward;
   
   ma1 = Close[limit-1];
   ma3 = ma1;
   for(int i=limit-1; i>= 0; i--) {
      //---- ma_shift set to 0 because SetIndexShift called above
      double val = Close[i];
      double slope1, predict;

         
      ma1 = p1*val + (1.0-p1)*ma1;
      ma3 = p3*val + (1.0-p3)*ma3;
      
      slope1 = (ma3-ma1)/(t1-t3);
   
      predict = ma3 + slope1*t;
   
      ExtBuffer[i]=predict;  
  }
//---- done
   return(0);
}
//+------------------------------------------------------------------+
 

If I hit refresh on the chart, this is how it looks.

 
Remus Vargas:

If I hit refresh on the chart, this is how it looks.

//limit=Bars-counted_bars;
limit=1000;

It happens because the initial values of ma1 and ma3 are updated every time.

 
Naguisa Unada:

It happens because the initial values of ma1 and ma3 are updated every time.

Thank you very much!!! It seems to be working now. You are a lifesaver!! . But can you please explain me a little what does that limit mean? I really want to learn, not to make these noob mistakes again. :)
 

"limit" means the loop count of "for-loop" (ie, the number of bars used for calculation).

if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;

In the original program, the "limit" is (limit = Bars) at startup because (counted_bars == 0).
But since (limit = Bars - counted_bars), limit is 1 or 0 after the second time.
Therefore, in the following line, ma1 and ma3 are updated every time, it affects the result.

ma1 = Close[limit-1];
ma3 = ma1;

Actually, it is correct to set (limit = Bars - 1), but (limit = 1000) is all right because load is increased.

 
Naguisa Unada:

"limit" means the loop count of "for-loop" (ie, the number of bars used for calculation).

In the original program, the "limit" is (limit = Bars) at startup because (counted_bars == 0).
But since (limit = Bars - counted_bars), limit is 1 or 0 after the second time.
Therefore, in the following line, ma1 and ma3 are updated every time, it affects the result.

Actually, it is correct to set (limit = Bars - 1), but (limit = 1000) is all right because load is increased.

Got it now! Thanks a lot Naguisa! Learned something new today. :)
Reason: