Randomically prev_calculated set to 0 by terminal - page 2

 
antony23 #:

 Send a "signal" that we don't know the reason of, is not "implementation" specific, is absurde logic. After a lot of posts, we are again on the same point. I would know the reason prev_calculated is reset to 0, so that in my code i can decide what to do.

Im my indicator, i use ticks (using CopyTicks) and not refer to bars, so i would know if buffers are modified when prev_calculated is reset to 0, and how.

Just as a by side example, can you imagine how many reasons could be that a connection is dropped for a while? There are too much, and any of them can be handled in different manner. Memory reallocations is another aspect, which could force recalculation. And so on. In every software there is a lot of internal implemention "tricks" which are not documented and cannot be documented. You need to use public API, not ask how it's implemented inside. For open source software you can find the answer yourself, if you want, but MT5 is a private proprietary thing.

In other words - you don't need to know why prev_calculated is reset to 0, you need only to handle such situations properly. It's very well known what to do.

 
If you have some specific problems with excessive resets specifically for your indicator (whereas other indicators are not reset), then there is a chance that you have an error in your indicator - then please post a test case (source code).
 
antony23 #:
Im my indicator, i use ticks (using CopyTicks) and not refer to bars, so i would know if buffers are modified when prev_calculated is reset to 0, and how.

I got an answer to this question. When terminal reset prev_calculated to 0 and rates_total to "max bars chart", it removes (free up) recent calculated indexes buffer, and not the oldest ones. 

E.g. if rates_total is 50015 and terminal reset buffers to 50000, it removes the recent 15 calculated indexes (from 0 to 14 in timeseries), keep the oldest indexes untuched.


That's a really weird logic, why don't remove oldest ones and shift newest indexes? This would avoid recalculate indicators (if we don't care about oldest data). While removing the newest data, it forces us to recalculate the entire buffer!

 
antony23 #:

I got an answer to this question. When terminal reset prev_calculated to 0 and rates_total to "max bars chart", it removes (free up) recent calculated indexes buffer, and not the oldest ones. 

E.g. if rates_total is 50015 and terminal reset buffers to 50000, it removes the recent 15 calculated indexes (from 0 to 14 in timeseries), keep the oldest indexes untuched.


That's a really weird logic, why don't remove oldest ones and shift newest indexes? This would avoid recalculate indicators (if we don't care about oldest data). While removing the newest data, it forces us to recalculate the entire buffer!

I don't think you're right. Terminal removes most outdated quotes while shrinking excessive data. Probably, your misunderstanding comes from the fact that quotes are indexed in reversed order (called "as series") - 0-th index is the current bar.
 
Stanislav Korotky #:
I don't think you're right. Terminal removes most outdated quotes while shrinking excessive data. Probably, your misunderstanding comes from the fact that quotes are indexed in reversed order (called "as series") - 0-th index is the current bar.
Whatever data are removed, you can't count on that. The whole buffers should be recalculated when prev_calculated = 0.
 
Alain Verleyen #:
Whatever data are removed, you can't count on that. The whole buffers should be recalculated when prev_calculated = 0.
Of course so (and I did not mention anything to the contrary), but my reply was an objection to the statement, that most recent bars are removed, which is not true.
 
Stanislav Korotky #:
Of course so (and I did not mention anything to the contrary), but my reply was an objection to the statement, that most recent bars are removed, which is not true.
I know, I should have answered to antony not you.
 

My indicator is a renko bar indicator, built on ticks dara and not on timeseries data. I don't need to recalculate all buffers when prev_calculated is 0, it is not mandatory in this case.

I tried setting buffers as series, but also as not series, anyway i can confirm that when terminal shrink buffers it cleans the most right indexes, meaning the most right ones in memory, indipendently of indexing order. 

Here a sample code to check. Put it on M1 chart. I set my max bars to 1000000.

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1


double line_data[];

int OnInit()
{
   PlotIndexSetInteger(0,PLOT_DRAW_TYPE, DRAW_LINE);
   PlotIndexSetString(0,PLOT_LABEL,"LINE");
   SetIndexBuffer(0, line_data, INDICATOR_DATA); 
   
   ArraySetAsSeries(line_data,true);
   ArrayInitialize(line_data,0);
   
   return(INIT_SUCCEEDED);
}

int OnCalculate(const int32_t rates_total,
                const int32_t 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 int32_t &spread[])
{

   if(prev_calculated!=rates_total){
      Print("1. prev/total: "+prev_calculated+"/"+rates_total+" --- Buffer[0]: "+line_data[0]+" - buffer[last]: "+line_data[rates_total-1]);
      
      for(int i=0; i<rates_total; i++){
         line_data[i] = i+1;
      }
      
      Print("2. prev/total: "+prev_calculated+"/"+rates_total+" --- Buffer[0]: "+line_data[0]+" - buffer[last]: "+line_data[rates_total-1]);
      
   }

   return(rates_total);
}


At some point, right click on chart and click "Refresh", so terminal reset prev_calculated to 0 and rates_total to 1000000. Here the log:

2026.04.29 15:58:35.741 testShrink (USTEC,M1)   1. prev/total: 0/1000006 --- Buffer[0]: 0.0 - buffer[last]: 0.0
2026.04.29 15:58:35.743 testShrink (USTEC,M1)   2. prev/total: 0/1000006 --- Buffer[0]: 1.0 - buffer[last]: 1000006.0
2026.04.29 15:58:58.628 testShrink (USTEC,M1)   1. prev/total: 1000006/1000007 --- Buffer[0]: 0.0 - buffer[last]: 1000006.0
2026.04.29 15:58:58.630 testShrink (USTEC,M1)   2. prev/total: 1000006/1000007 --- Buffer[0]: 1.0 - buffer[last]: 1000007.0
2026.04.29 15:59:58.576 testShrink (USTEC,M1)   1. prev/total: 1000007/1000008 --- Buffer[0]: 0.0 - buffer[last]: 1000007.0
2026.04.29 15:59:58.577 testShrink (USTEC,M1)   2. prev/total: 1000007/1000008 --- Buffer[0]: 1.0 - buffer[last]: 1000008.0
2026.04.29 16:00:01.960 testShrink (USTEC,M1)   1. prev/total: 0/1000000 --- Buffer[0]: 9.0 - buffer[last]: 1000008.0
2026.04.29 16:00:01.961 testShrink (USTEC,M1)   2. prev/total: 0/1000000 --- Buffer[0]: 1.0 - buffer[last]: 1000000.0

As you can see, when i clicked Refresh, the 0 element was 9, so it removed newest indexes elements.


It is simple to try without as series, comment "ArraySetAsSeries(line_data,true);". Here the log: 

2026.04.29 16:00:44.651 testShrink (USTEC,M1)   1. prev/total: 0/1000000 --- Buffer[0]: 0.0 - buffer[last]: 0.0
2026.04.29 16:00:44.653 testShrink (USTEC,M1)   2. prev/total: 0/1000000 --- Buffer[0]: 1.0 - buffer[last]: 1000000.0
2026.04.29 16:00:58.575 testShrink (USTEC,M1)   1. prev/total: 1000000/1000001 --- Buffer[0]: 1.0 - buffer[last]: 0.0
2026.04.29 16:00:58.577 testShrink (USTEC,M1)   2. prev/total: 1000000/1000001 --- Buffer[0]: 1.0 - buffer[last]: 1000001.0
2026.04.29 16:01:58.587 testShrink (USTEC,M1)   1. prev/total: 1000001/1000002 --- Buffer[0]: 1.0 - buffer[last]: 0.0
2026.04.29 16:01:58.589 testShrink (USTEC,M1)   2. prev/total: 1000001/1000002 --- Buffer[0]: 1.0 - buffer[last]: 1000002.0
2026.04.29 16:02:58.624 testShrink (USTEC,M1)   1. prev/total: 1000002/1000003 --- Buffer[0]: 1.0 - buffer[last]: 0.0
2026.04.29 16:02:58.626 testShrink (USTEC,M1)   2. prev/total: 1000002/1000003 --- Buffer[0]: 1.0 - buffer[last]: 1000003.0
2026.04.29 16:03:58.757 testShrink (USTEC,M1)   1. prev/total: 1000003/1000004 --- Buffer[0]: 1.0 - buffer[last]: 0.0
2026.04.29 16:03:58.758 testShrink (USTEC,M1)   2. prev/total: 1000003/1000004 --- Buffer[0]: 1.0 - buffer[last]: 1000004.0
2026.04.29 16:04:12.003 testShrink (USTEC,M1)   1. prev/total: 0/1000000 --- Buffer[0]: 1.0 - buffer[last]: 1000000.0
2026.04.29 16:04:12.004 testShrink (USTEC,M1)   2. prev/total: 0/1000000 --- Buffer[0]: 1.0 - buffer[last]: 1000000.0

Even so, the newest indexes elements was removed.

So it seems to me clear that terminal free up the most right element in memory, indipendently from indexing order.

 
antony23 #:

My indicator is a renko bar indicator, built on ticks dara and not on timeseries data. I don't need to recalculate all buffers when prev_calculated is 0, it is not mandatory in this case.

I tried setting buffers as series, but also as not series, anyway i can confirm that when terminal shrink buffers it cleans the most right indexes, meaning the most right ones in memory, indipendently of indexing order. 

Here a sample code to check. Put it on M1 chart. I set my max bars to 1000000.


At some point, right click on chart and click "Refresh", so terminal reset prev_calculated to 0 and rates_total to 1000000. Here the log:

As you can see, when i clicked Refresh, the 0 element was 9, so it removed newest indexes elements.


It is simple to try without as series, comment "ArraySetAsSeries(line_data,true);". Here the log: 

Even so, the newest indexes elements was removed.

So it seems to me clear that terminal free up the most right element in memory, indipendently from indexing order.

This is unreliable.

You ALWAYS have to recalculate when prev_calculated = 0 because you CAN'T know what the Terminal will do with your buffers. It's not documented, not under MQL developer control, so the only way to deal with it reliably is to recalculate all.

 
Alain Verleyen #:

This is unreliable.

You ALWAYS have to recalculate when prev_calculated = 0 because you CAN'T know what the Terminal will do with your buffers. It's not documented, not under MQL developer control, so the only way to deal with it reliably is to recalculate all.

Yes, I understood it unfortunately. This improvement could be an important optimization... why don't they do it ?! 

Thanks anyway.