Drawing not updated

 
Hi all
I've written a Zigzag-RSI indicator combining RSI values to standard Zigzag indicator. It works pretty well, except that drawing of RSI values does not update on chart (i.e. indicator window). I want it to be updated in each tick as it is for standard RSI indicator.

Here is the code: 
//+------------------------------------------------------------------+
//|                                                    ZigZagRSI.mq4 |
//|                   Copyright 2006-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright "2006-2014, MetaQuotes Software Corp."
#property link      "http://www.mql4.com"
#property strict

#property indicator_separate_window
#property indicator_maximum 100
#property indicator_minimum 0
#property indicator_level1     30.0
#property indicator_level2     70.0
#property indicator_levelcolor clrSilver
#property indicator_levelstyle STYLE_DOT
#property indicator_buffers 2
#property indicator_color1  Gold
#property indicator_color2  Red
//---- indicator parameters
input int InpDepth=12;     // Depth
input int InpDeviation=5;  // Deviation
input int InpBackstep=3;   // Backstep
input int InpRSIPeriod=8;   // RSI Period
//---- indicator buffers
double ExtZigzagBuffer[];
double ExtHighBuffer[];
double ExtLowBuffer[];
double RSIBuffer[];
//--- globals
int    ExtLevel=3; // recounting's depth of extremums
double outVal =101.0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(InpBackstep>=InpDepth)
     {
      Print("Backstep cannot be greater or equal to Depth");
      return(INIT_FAILED);
     }
//--- 2 additional buffers
   IndicatorBuffers(4);
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_SECTION);
//---- indicator buffers
   SetIndexBuffer(0,RSIBuffer);
   SetIndexBuffer(1,ExtZigzagBuffer);
   SetIndexBuffer(2,ExtHighBuffer);
   SetIndexBuffer(3,ExtLowBuffer);

   SetIndexEmptyValue(0,outVal);
   SetIndexEmptyValue(1,outVal);
//---- indicator short name
   IndicatorShortName("ZigZagRSI("+string(InpDepth)+","+string(InpDeviation)+","+string(InpBackstep)+";"+InpRSIPeriod+")");
   
//---- initialization done
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
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,counterZ,whatlookfor=0;
   int    back,pos,lasthighpos=0,lastlowpos=0;
   double extremum;
   double curlow=outVal,curhigh=outVal,lasthigh=outVal,lastlow=outVal;
//--- check for history and inputs
   if(rates_total<InpDepth || InpBackstep>=InpDepth)
      return(0);
   
   //ArraySetAsSeries(RSIBuffer,false);   
//--- first calculations
   if(prev_calculated==0)
     {
      limit=InitializeAll();
      for(i=0; i<rates_total; i++)
        {
         RSIBuffer[i]=iRSI(NULL,0,InpRSIPeriod,PRICE_CLOSE,i);
        }
     }
   else
     {
      for(i=prev_calculated; i<rates_total; i++)
        {
         RSIBuffer[i]=iRSI(NULL,0,InpRSIPeriod,PRICE_CLOSE,i);
        }
      //--- find first extremum in the depth ExtLevel or 100 last bars
      i=counterZ=0;
      while(counterZ<ExtLevel && i<100)
        {
         if(ExtZigzagBuffer[i]!=outVal)
            counterZ++;
         i++;
        }
      //--- no extremum found - recounting all from begin
      if(counterZ==0)
         limit=InitializeAll();
      else
        {
         //--- set start position to found extremum position
         limit=i-1;
         //--- what kind of extremum?
         if(ExtLowBuffer[i]!=outVal)
           {
            //--- low extremum
            curlow=ExtLowBuffer[i];
            //--- will look for the next high extremum
            whatlookfor=1;
           }
         else
           {
            //--- high extremum
            curhigh=ExtHighBuffer[i];
            //--- will look for the next low extremum
            whatlookfor=-1;
           }
         //--- clear the rest data
         for(i=limit-1; i>=0; i--)
           {
            ExtZigzagBuffer[i]=outVal;
            ExtLowBuffer[i]=outVal;
            ExtHighBuffer[i]=outVal;
           }
        }
     }
//--- main loop
   for(i=limit; i>=0; i--)
     {
      //--- find lowest low in depth of bars
      //int index1 = ArrayMinimum(RSIBuffer,InpDepth,i);
      extremum=RSIBuffer[ArrayMinimum(RSIBuffer,InpDepth,i)]; //low[iLowest(NULL,0,MODE_LOW,InpDepth,i)];
      //--- this lowest has been found previously
      if(extremum==lastlow)
         extremum=outVal;
      else
        {
         //--- new last low
         lastlow=extremum;
         //--- discard extremum if current low is too high
         if(RSIBuffer[i]-extremum>InpDeviation*Point)
            extremum=outVal;
         else
           {
            //--- clear previous extremums in backstep bars
            for(back=1; back<=InpBackstep; back++)
              {
               pos=i+back;
               if(ExtLowBuffer[pos]!=0 && ExtLowBuffer[pos]>extremum)
                  ExtLowBuffer[pos]=outVal;
              }
           }
        }
      //--- found extremum is current low
      if(RSIBuffer[i]==extremum)
         ExtLowBuffer[i]=extremum;
      else
         ExtLowBuffer[i]=outVal;
      //--- find highest high in depth of bars
      extremum=RSIBuffer[ArrayMaximum(RSIBuffer,InpDepth,i)]; //high[iHighest(NULL,0,MODE_HIGH,InpDepth,i)];
      //--- this highest has been found previously
      if(extremum==lasthigh)
         extremum=outVal;
      else
        {
         //--- new last high
         lasthigh=extremum;
         //--- discard extremum if current high is too low
         if(extremum-RSIBuffer[i]>InpDeviation*Point)
            extremum=outVal;
         else
           {
            //--- clear previous extremums in backstep bars
            for(back=1; back<=InpBackstep; back++)
              {
               pos=i+back;
               if(ExtHighBuffer[pos]!=0 && ExtHighBuffer[pos]<extremum)
                  ExtHighBuffer[pos]=outVal;
              }
           }
        }
      //--- found extremum is current high
      if(RSIBuffer[i]==extremum)
         ExtHighBuffer[i]=extremum;
      else
         ExtHighBuffer[i]=outVal;
     }
//--- final cutting
   if(whatlookfor==0)
     {
      lastlow=outVal;
      lasthigh=outVal;
     }
   else
     {
      lastlow=curlow;
      lasthigh=curhigh;
     }
   for(i=limit; i>=0; i--)
     {
      switch(whatlookfor)
        {
         case 0: // look for peak or lawn
            if(lastlow==outVal && lasthigh==outVal)
              {
               if(ExtHighBuffer[i]!=outVal)
                 {
                  lasthigh=RSIBuffer[i];
                  lasthighpos=i;
                  whatlookfor=-1;
                  ExtZigzagBuffer[i]=lasthigh;
                 }
               if(ExtLowBuffer[i]!=outVal)
                 {
                  lastlow=RSIBuffer[i];
                  lastlowpos=i;
                  whatlookfor=1;
                  ExtZigzagBuffer[i]=lastlow;
                 }
              }
            break;
         case 1: // look for peak
            if(ExtLowBuffer[i]!=outVal && ExtLowBuffer[i]<lastlow && ExtHighBuffer[i]==outVal)
              {
               ExtZigzagBuffer[lastlowpos]=outVal;
               lastlowpos=i;
               lastlow=ExtLowBuffer[i];
               ExtZigzagBuffer[i]=lastlow;
              }
            if(ExtHighBuffer[i]!=outVal && ExtLowBuffer[i]==outVal)
              {
               lasthigh=ExtHighBuffer[i];
               lasthighpos=i;
               ExtZigzagBuffer[i]=lasthigh;
               whatlookfor=-1;
              }
            break;
         case -1: // look for lawn
            if(ExtHighBuffer[i]!=outVal && ExtHighBuffer[i]>lasthigh && ExtLowBuffer[i]==outVal)
              {
               ExtZigzagBuffer[lasthighpos]=outVal;
               lasthighpos=i;
               lasthigh=ExtHighBuffer[i];
               ExtZigzagBuffer[i]=lasthigh;
              }
            if(ExtLowBuffer[i]!=outVal && ExtHighBuffer[i]==outVal)
              {
               lastlow=ExtLowBuffer[i];
               lastlowpos=i;
               ExtZigzagBuffer[i]=lastlow;
               whatlookfor=1;
              }
            break;
        }
     }
//--- done
   string tt;
   for(i=0; i<=100; i++)
     {

      if(ExtZigzagBuffer[i]!=outVal)
        {
         tt += i + "-" + ExtZigzagBuffer[i]+ "; ";
        }
     }
//Comment(tt);
   return(rates_total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int InitializeAll()
  {
   ArrayInitialize(ExtZigzagBuffer,outVal);
   ArrayInitialize(ExtHighBuffer,outVal);
   ArrayInitialize(ExtLowBuffer,outVal);
//--- first counting position
   return(Bars-InpDepth);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
 
srh124 I want it to be updated in each tick as it is for standard RSI indicator.
      for(i=prev_calculated; i<rates_total; i++)
        {
         RSIBuffer[i]=iRSI(NULL,0,InpRSIPeriod,PRICE_CLOSE,i);

You return rates_total, therefor on the next tick prev_calculated is rates_total. Your for loop runs while rates_total < rates_total. Why does not updating surprise you?
          How to do your lookbacks correctly #9#14 & #19

 
William Roeder #:

You return rates_total, therefor on the next tick prev_calculated is rates_total. Your for loop runs while rates_total < rates_total. Why does not updating surprise you?
          How to do your lookbacks correctly #9#14 & #19

Thanks a lot man for your hint.
I corrected my code as follows and it works:

for(i=0; i<=rates_total-prev_calculated; i++)
        {
         RSIBuffer[i]=iRSI(NULL,0,InpRSIPeriod,PRICE_CLOSE,i);
        }

Anyway, am i in correct way? I mean writing above code (which works fine) is logically (regarding MQL programming) correct? or i should do it another way?