prev_calculated<0 ? is it possible?

 

T‌he piece of code from the document https://www.mql5.com/en/docs/series/copybuffer

//--- we can copy not all data 
   int to_copy; 
   if(prev_calculated>rates_total || prev_calculated<=0) to_copy=rates_total; 
   else 
     { 
      to_copy=rates_total-prev_calculated; 
      //--- last value is always copied 
      to_copy++; 
     } 

prev_calculated>rates_total and   prev_calculated<0 , I can not image these two cases.

I‌ think the minimum of prev_calculated is 0, and prev_calculated is not possible larger than rates_total.

Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
  • www.mql5.com
Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
 

Prev_calculated is the result of previous calculation invocation (see the docs). So you can easily return -1 and get it on the next tick. (Strinctly speeking, since the type of prev_calculated is int, good programming style requires you to check incoming value against all possible domain to prevent potential bugs)

A‌s for rates_total, this can be changed in either direction since MT is a client-server platform: normally it does only grow, but if history is somehow edited on server (to remove some incorrect data which is always possible in the real world), it should be processed on the client appropriately.

Documentation on MQL5: Custom Indicators
Documentation on MQL5: Custom Indicators
  • www.mql5.com
Custom Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
 
Stanislav Korotky:

Prev_calculated is the result of previous calculation invocation (see the docs). So you can easily return -1 and get it on the next tick. (Strinctly speeking, since the type of prev_calculated is int, good programming style requires you to check incoming value against all possible domain to prevent potential bugs)

A‌s for rates_total, this can be changed in either direction since MT is a client-server platform: normally it does only grow, but if history is somehow edited on server (to remove some incorrect data which is always possible in the real world), it should be processed on the client appropriately.


    Thank you.

    Prev_calculated is passed to OnCalculate() by the client terminal. It seems the value of prev_calculated depends on the return value of OnCalculate().

 ‌   Normally prev_calculated is equal to the return value, but if the return value is 0 or minus,or larger than rates_total ,then pre_calculate will be 0.

   

//+------------------------------------------------------------------+
//|                                                         test.mq5 |

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Label1
#property indicator_label1  "Label1"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- indicator buffers
double         Label1Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Label1Buffer,INDICATOR_DATA);
   
//---
   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[])
  {
//---
   int limit =0;
   if(rates_total<=0)return(0);
   if(prev_calculated==0)limit=0;
   else limit = rates_total- prev_calculated +1;
   
   for(int i=limit;i<rates_total;i++)
   Label1Buffer[i]= (high[i]+low[i]+close[i])/3;
   
   
    
 static int cnt =0;
 if(cnt<10)
     {
       cnt++;
       Print(cnt,":rates_total = ",rates_total,", pre_calculated = ",prev_calculated);
       if(cnt<4)return(-1);
       else if (cnt<7)return(0);
       else return(cnt);
     }
     
 if(cnt==11 && prev_calculated>0) //next tick,  cnt== 11
    {       
         Print(cnt,":rates_total = ",rates_total,", pre_calculated = ",prev_calculated);
         cnt++;
         return(rates_total+1); // next tick, cnt==12
     }
     
 if(cnt==12)
 {
   Print(cnt,":rates_total = ",rates_total,", pre_calculated = ",prev_calculated); //prev_calculated = 0
   cnt++;
 }         
     
 if(cnt==10)cnt++;  // cnt == 11  
     
     
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

//---

Reason: