Download MetaTrader 5

TickIndicator - Array out of Range - Error if more ticks than bars - how to avoid this?

To add comments, please log in or register
Matthias Schneider
1636
Matthias Schneider 2014.05.14 19:12 

Hi,

is there any way to avoid the "Array out of range" error with tick inidcators which show up after some time? As far as I understand this error appears if I got more ticks than bars. The array size managed by the subsystem seems to be depending on the  "Max Bars in Chart" and the history of bars downloaded from the broker. But in every case if I leave this indicator running I will get of course more ticks than bars, it's just a matter of time.

Is ther any way to clean up older values or another solution for this code?

 

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1

#property indicator_label1  "Bid"
#property indicator_type1   DRAW_LINE
#property indicator_color1  Blue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

double BidBuffer[];
string name;

int OnInit()
{   
   SetIndexBuffer(0,BidBuffer,INDICATOR_DATA);  
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);

   return(INIT_SUCCEEDED);
}

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
{
   static int ticks=0;
   if(ticks==0)
      ArrayInitialize(BidBuffer,0);

   MqlTick last_tick;
   if(SymbolInfoTick(Symbol(),last_tick))
   {      
      BidBuffer[ticks]=last_tick.bid;
      
      int shift=rates_total-1-ticks;
      PlotIndexSetInteger(0,PLOT_SHIFT,shift);
      ticks++;  
   }

   return(rates_total);
}

Thanks. 

Matthias Schneider
1636
Matthias Schneider 2014.05.15 12:39  

Allright, I think I found it in the tick indicator article:

 

      if(ticks_stored>=rates_total)
      {
         // Removing the first tick_stored/2 quotes and shifting remaining quotes
         for(i=ticks_stored/2;i<ticks_stored;i++)
         {
            // Shifting the data to the beginning in the TicksBuffer[] array on tick_stored/2
            TicksBuffer[i-ticks_stored/2]=TicksBuffer[i];
         }
         // Changing the quotes counter
         ticks_stored-=ticks_stored/2;
      }
To add comments, please log in or register