CopyBuffer documentation -- example confuses me, is it a mistake?

 

Another newbie question about this code, found at  CopyBuffer - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

//--- 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++;
     }
//--- try to copy
   if(CopyBuffer(ma_handle,0,0,to_copy,MABuffer)<=0) return(0);


In what cases can prev_calculated ever be greater than rates_total?

I would think that:

a. If there's anything to copy at all, prev_calculated must be less than rates_total, otherwise it was all copies during a prior OnCalculate event;

b. in which case the to_copy value would simply be rates_total - prev_calculated;

c. So, to_copy = rates_total - prev_calculated; would always come up with the correct number of elements to copy.

d. Therefore, the entire if() structure could be simplified to that single assignment statement.

Please let me know where I'm going wrong here.

Thanks!

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

Another newbie question about this code, found at  CopyBuffer - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5


In what cases can prev_calculated ever be greater than rates_total?

I would think that:

a. If there's anything to copy at all, prev_calculated must be less than rates_total, otherwise it was all copies during a prior OnCalculate event;

b. in which case the to_copy value would simply be rates_total - prev_calculated;

c. So, to_copy = rates_total - prev_calculated; would always come up with the correct number of elements to copy.

d. Therefore, the entire if() structure could be simplified to that single assignment statement.

Please let me know where I'm going wrong here.

Thanks!

Prev calculated can be larger than rates total.

When the indicator is up to date and the terminal trims the charts available periods, rates total will be reduced, but prev calculated is the value you returned on las OnCalculate call.

So this is when it happens.
 
Dominik Egert #:
Prev calculated can be larger than rates total.

When the indicator is up to date and the terminal trims the charts available periods, rates total will be reduced, but prev calculated is the value you returned on las OnCalculate call.

So this is when it happens.

Thank you.

 
Millard Melnyk:

Another newbie question about this code, found at  CopyBuffer - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5


In what cases can prev_calculated ever be greater than rates_total?

I would think that:

a. If there's anything to copy at all, prev_calculated must be less than rates_total, otherwise it was all copies during a prior OnCalculate event;

b. in which case the to_copy value would simply be rates_total - prev_calculated;

c. So, to_copy = rates_total - prev_calculated; would always come up with the correct number of elements to copy.

d. Therefore, the entire if() structure could be simplified to that single assignment statement.

Please let me know where I'm going wrong here.

Thanks!

Only on the first run, prev_calculated equals zero and rates_total is whatever it is, say 10000 bars. The whole indicator curve is calculated once and on the second run prev_calculated is equal to rates_total until a new bar is added. But that would mean that the last bar is only updated once, so if both are equal so to_copy is zero, we say that to_copy is one so the last value will be updated each tick (each run of oncalculate).
This is done because it is not necessary to update all values each time  

You can set break points to use the debugger to go through a code step by step and put the variables in observation with right click to see how the values change