Indicator not updating with new candles

 

Hello everyone,


I found this MT5 indicator:

//+------------------------------------------------------------------+
//|                                                 Volume_Delta.mq5 |
//+------------------------------------------------------------------+
#property copyright "avoitenko"
#property link      "https://www.mql5.com/en/users/avoitenko"
#property version   "1.00"
#property indicator_separate_window
//---
#property indicator_buffers 5
#property indicator_plots 3
//---
#property indicator_type1  DRAW_HISTOGRAM
#property indicator_color1 clrDodgerBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
#property indicator_label1 "Ask Volumes"
//---
#property indicator_type2  DRAW_HISTOGRAM
#property indicator_color2 clrRed
#property indicator_style2 STYLE_SOLID
#property indicator_width2 2
#property indicator_label2 "Bid Volumes"
//---
#property indicator_type3  DRAW_HISTOGRAM
#property indicator_color3 clrLime
#property indicator_style3 STYLE_SOLID
#property indicator_width3 5
#property indicator_label3 "Delta Volumes"
//+------------------------------------------------------------------+
input bool InpShowBidAsk=false;//Show Ask & Bid Volume
input bool InpShowDelta=true;//Show Delta Volume
input uint InpMaxHistory=500;//Max Bars
//---
double ShortBuffer[];
double LongBuffer[];
double DeltaBuffer[];
double AskVolume[];
double BidVolume[];
bool forex;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   forex=SymbolInfoInteger(_Symbol,SYMBOL_TRADE_CALC_MODE)==SYMBOL_CALC_MODE_FOREX;
   SetIndexBuffer(0,LongBuffer);
   SetIndexBuffer(1,ShortBuffer);
   SetIndexBuffer(2,DeltaBuffer);
   SetIndexBuffer(3,AskVolume,INDICATOR_CALCULATIONS);
   SetIndexBuffer(4,BidVolume,INDICATOR_CALCULATIONS);
//---
   ArraySetAsSeries(ShortBuffer,true);
   ArraySetAsSeries(LongBuffer,true);
   ArraySetAsSeries(DeltaBuffer,true);
   ArraySetAsSeries(AskVolume,true);
   ArraySetAsSeries(BidVolume,true);
//---
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0);
   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0);
//---
   string short_name="Volume Delta ";
   if(InpShowBidAsk)
     {
      if(InpShowDelta)
         short_name+="(Ask & Bid & Delta)";
      else
         short_name+="(Ask & Bid)";
     }
   else
   if(InpShowDelta)
      short_name+="(Delta)";

   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
   IndicatorSetInteger(INDICATOR_DIGITS,0);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
   ArraySetAsSeries(time,true);

//---
   MqlTick tick;
   if(!SymbolInfoTick(_Symbol,tick))
      return(rates_total);

//---
   int limit;
   if(prev_calculated==0)
     {
      ArrayInitialize(LongBuffer,0);
      ArrayInitialize(ShortBuffer,0);
      ArrayInitialize(DeltaBuffer,0);
      ArrayInitialize(AskVolume,0);
      ArrayInitialize(BidVolume,0);

      //---
      limit=rates_total-1;
      if(InpMaxHistory>0)
         limit=fmin(limit,(int)InpMaxHistory);

      //---
      MqlTick ticks[];
      ArraySetAsSeries(ticks,true);

      //---calc history bars
      //datetime futures_start_time = SymbolInfoInteger(_Symbol,SYMBOL_START_TIME);
      //Print(time[limit]," ",futures_start_time);
      int result=CopyTicksRange(_Symbol,ticks,COPY_TICKS_TRADE,time[limit]*1000,TimeTradeServer()*1000);
      if(result>0)
        {
         int index=0;
         for(int i=0; i<result-1; i++)
           {
            datetime tick_time=(datetime)(ticks[i].time_msc/1000);

            while(tick_time<time[index])
               if(index<limit)
                  index++;

            /*double middle=NormalizeDouble((ticks[i].ask+ticks[i].bid)/2,_Digits);
            if(ticks[i].last>=middle)
               AskVolume[index]+=(int)ticks[i].volume;
            else
               BidVolume[index]+=(int)ticks[i].volume;
            */
            if((ticks[i].flags&TICK_FLAG_BUY)==TICK_FLAG_BUY)
              {
               AskVolume[index]+=(int)ticks[i].volume;
              }
            if((ticks[i].flags&TICK_FLAG_SELL)==TICK_FLAG_SELL)
              {
               BidVolume[index]+=(int)ticks[i].volume;
              }
           }
        }
     }
   else
     {
      limit=rates_total-prev_calculated;
      if(rates_total-prev_calculated>0)
        {
         LongBuffer[0]=0;
         ShortBuffer[0]=0;
         DeltaBuffer[0]=0;
         AskVolume[0]=0;
         BidVolume[0]=0;
        }
     }
/*
//--- current bar
   double middle=NormalizeDouble((tick.ask+tick.bid)/2,_Digits);
   if(tick.last>=middle)
     {
      if(tick.flags==TICK_FLAG_BUY)
        {AskVolume[0]+=(int)tick.volume;}
     }
   else
     {
      if(tick.flags==TICK_FLAG_SELL)
        {BidVolume[0]+=(int)tick.volume;}
     }

//--- for forex symbols
   if(forex)
     {
      for(int i=limit;i>=0;i--)
        {
         LongBuffer[i]=BidVolume[i];
         ShortBuffer[i]=0;
         DeltaBuffer[i]=0;
        }
      return(rates_total);
     }
*/
//--- futures
   for(int i=limit;i>=0;i--)
     {
      if(InpShowBidAsk)
        {
         LongBuffer[i]=AskVolume[i];
         ShortBuffer[i]=-BidVolume[i];
         if(InpShowDelta)
            DeltaBuffer[i]=AskVolume[i]-BidVolume[i];
         else
            DeltaBuffer[i]=0;
        }
      else
        {
         if(InpShowDelta)
           {
            double delta=AskVolume[i]-BidVolume[i];
            if(delta>0)
              {
               LongBuffer[i]=delta;
               ShortBuffer[i]=0;
              }
            else
              {
               LongBuffer[i]=0;
               ShortBuffer[i]=delta;
              }
           }
         DeltaBuffer[i]=0;
        }
     }
//--- done
   return(rates_total);
  }
//+------------------------------------------------------------------+


However it has an issue, it doesn't update the values in the data window with new candles. I have a feeling this has to do with the for loop in OnCalculate() and I tried replacing it with

int from2=MathMax(0,prev_calculated-1);

for(int i=from2;i<rates_total;i++)

but it didn't fix the problem. I'm unsure what to do to amend this and therefore I would immensely appreciate guidance and help!


Many thanks,

RappNoyd.

Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Running MQL5 Program Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Running MQL5 Program Properties
  • www.mql5.com
Running MQL5 Program Properties - Environment State - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5