Indicator not refreshing

 

I'm not able to figure out how to get the following indicator code to update. I've tried inserting RefreshRates() and inserted some suggested code around the "limit" line but to no avail. What needs to be changed in order to get this indicator to automatically update on the chart? Thanks. Bill

#property  copyright "Copyright © 2013, Bill Doerrfeld"

#property  indicator_separate_window
#property  indicator_buffers 1
#property  indicator_color1  Blue
#property  indicator_width1  2

//---- indicator parameters
extern int BarsBack=20;
extern double Threshold=0.5; 
//---- indicator buffers
double     PowerBuffer[];


int init()
  {
   SetIndexStyle(0,DRAW_HISTOGRAM);
   IndicatorDigits(Digits+1);
   SetIndexBuffer(0,PowerBuffer);

//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("Power("+BarsBack+")");
   SetIndexLabel(0,"Power");

   return(0);
  }

int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars/BarsBack;
   double Value;

   for(int i=0; i<limit; i++)
      PowerBuffer[i] = Value(i);

   return(0);
  }

// --------- Functions ---------

double BullValue(int i)
   {
   int Back = i + BarsBack;
   double r = 0;
   double v = 0;
   
   for(int j=i; j<Back; j++)
      {
      r = r + High[j] - Low[j];
      if(Open[j] < Close[j]) v=v+Close[j] - Open[j];
      }   
   return(-r/v);
   
   }


double BearValue(int i)
   {
   int Back = i + BarsBack;
   double r = 0;
   double v = 0;

   for(int j=i; j<Back; j++)
      {
      r = r + High[j] - Low[j];
      if(Open[j] > Close[j]) v=v+Close[j] - Open[j];
      }   
   return(-r/v);
   }

double Value(int i)
   {
   double v = BullValue(i) + BearValue(i);
   if(v < Threshold && v > -Threshold) return(0);
   else return(v);
   
   }
 
 

Possible divide by zero error?

double BullValue(int i)
   {
   int Back = i + BarsBack;
   double r = 0;
   double v = 0;
   
   for(int j=i; j<Back; j++)
      {
      r = r + High[j] - Low[j];
      if(Open[j] < Close[j]) v=v+Close[j] - Open[j];
      } 

 //MAYBE CHANGE THIS FROM  
 //  return(-r/v);
 // TO
    if(v==0 || r==0)
      return(0);
    else
      return(-r/v);
   
   }

Obviously, the same for the other function

 
ubzen: why are you doing this?
limit=Bars-counted_bars/BarsBack;
Exactly right. Do it right: https://www.mql5.com/en/forum/146942
 


Because I didn't know what else to try and came across someone's whacky solution using this technique at http://www.fxfisherman.com/forums/forex-metatrader/indicators/3690-trsi-indicator-issue.html.

 Resolved
I changed this one line:

limit=Bars-counted_bars - RSI_Period;

to this:

limit=Bars-counted_bars/RSI_Period;

and it now draws continually and the chart looks the same as the previous one bit doesn't stop, ran them side by side for a while...
 
billworld: Because I didn't know what else to try and came across someone's whacky solution using this technique at http://www.fxfisherman.com/forums/forex-metatrader/indicators/3690-trsi-indicator-issue.html.
Try going through the tutorials, it beats guessing. For Zero Divide, you only have to make sure that the equation|variable on the right side of the division does not equal zero. Example a/b. check to make sure b does_not equal 0 before performing the calculation.
 
GumRai:

Possible divide by zero error?

Obviously, the same for the other function


Thanks for this. It does appear to have been a zero divide issue. I made the suggested change and that solves the issue.
 
billworld:

Thanks for this. It does appear to have been a zero divide issue. I made the suggested change and that solves the issue.

I'm happy that it has helped, but you should still address the other issue mentioned by others as your indi is recalculating many more bars than is necessary.
 

Thanks GumRai. I'm still trying to understand the advice received regarding indicator bar counting (advice made via a link to a different thread). What is confusing is that step one of said advice (see below) suggests decrementing but step 2 says don't decrement and then points to a thread (which I've for the most part read) where experts debate whether or not to decrement.

With the exception of the "/BarsBack" code inserted at the end of "limit=Bars-counted_bars/BarsBack;" all I did was use as a basis for bar counting the code in the supplied MACD example.

Is that code flawed? If so, what specifically should be changed?

Also, I've inserted code to check for a new bar as follows:

int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   double Value;

   if(NewBar())
   {
   for(int i=0; i<limit; i++)
      PowerBuffer[i] = Value(i);
   }
   
   return(0);
  }

Custom function:

bool NewBar()
   {
      static datetime lastbar = 0;
      datetime curbar = iTime(Symbol(), Period(), 0);
      if(lastbar!=curbar)
      {
         lastbar=curbar;
         return (true);
      }
      else
      {
         return(false);
      }
   } 

Is that the best way to handle new bar checks within an indicator?

Thanks

Bill

---------

  1. Always count down so you're not repainting
  2. Don't decrement Contradictory information on IndicatorCounted() - MQL4 forum
  3. Don't look back past the last candle.

----------

 

Take look at the source code to some of the standard indicators you will see it is not neccessary to find the newbar yourself in order to make it update bar zero, indicators will continually update themselves thanks to the relationship between IndicatorCounted() and Bars and the way you write the loop.

Typical multi purpose indicator loop, I use this all the time. Automatically updates bar zero on every tick.

int counted_bars=IndicatorCounted();

int i=Bars-1-counted_bars;

while(i>=0)
{
 ExtMapBuffer[i] = (High[i] + Low[i])*0.5; //creates a median price line
 i--;
}

If you want to work on indicators a lot you need to fully understand why that works, so you know what to do when you need to limit where it starts counting from or to, there will be occasions when you need to count up instead of down, and even count up at the same time as counting down. You also must understand exactly what IndicatorCounted() does.

 
SDC:

Take look at the source code to some of the standard indicators you will see it is not neccessary to find the newbar yourself in order to make it update bar zero, indicators will continually update themselves thanks to the relationship between IndicatorCounted() and Bars and the way you write the loop.

Typical multi purpose indicator loop, I use this all the time. Automatically updates bar zero on every tick.

If you want to work on indicators a lot you need to fully understand why that works, so you know what to do when you need to limit where it starts counting from or to, there will be occasions when you need to count up instead of down, and even count up at the same time as counting down. You also must understand exactly what IndicatorCounted() does.


I don't want or need for bar 0 to update every tick, that's why I insert NewBar().
Reason: