What does hitting "refresh" on a chart do to the indicator buffers?

 

I had a glitch on one of my indicator plotting lines which is meant only to be shown for the last 2 bars, which would sometimes show itself in historical bars when restarting MT5 after it was closed for a long time. Hitting refresh would fix it, but eventually I decided to head the problem off at the pass by making sure the first run of OnCalculate (0) would set all previous bars to EMPTY_VALUE.

Nonetheless, I am curious as to exactly what it is that "refresh" does.

I was assuming the issue was that because I had no logic in my indicator to actually initialize any past values in the buffer array, it was possible that random data in memory, or even old values that were not overwritten by other memory content in that array space, was leading to some old bars being plotted when MT5 was re-opened after being closed a long time. Because this only happened during times when MT5 was off for several hours, I assume this means the overall memory reservation or construct remained intact, occupying the same space as when starting MT5 up again. I should note that I had MetaEditor for that MT5 installation open the entire time, so perhaps that was keeping some memory reserved. I can't imagine this glitch only showing for the time MT5 was closed and then the current line buffer continuing to function at the same time for the current bar, without that space remaining reserved and continuing where it left off when restarting MT5.

My guess is that "refresh" wipes the slate clean for these buffer memory zones, re-establishing them as if they've been newly declared. After that, based on my test, it appears to set prev_calculated to 0 for either all the charts with that indicator, or all the charts. Perhaps it also wipes the memory for all charts/indicators as well. As far as I can tell, it does not re-initialize the indicators.


Edit: For your reference I have the plot for this buffer set to begin on the last 2 bars.

SetIndexBuffer(1,Buffer_Stoch1_Main_Line_Current,INDICATOR_DATA); 
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, Bars(_Symbol, _Period) - 2);

And on every new bar OnCalculate will clear out the 3rd last bar to empty_value (i'm wondering if it's possible for this to glitch if there is no tick for a new bar period and the next tick comes after skipping over a non-tick bar)

//---new bar
   if(rates_total - 1 == prev_calculated) //new bar
     {
      Buffer_Stoch1_Main_Line_Current[rates_total - 3]   = EMPTY_VALUE;   //erase current bar line for 2nd to last closed candle, make sure current bar line is only showing for 1 bar
      Buffer_Stoch1_Main_Line[rates_total-1] = EMPTY_VALUE;    //clear main line for current bar
     }


So when some bars were skipped while MT5 was closed, since it was already plotting before, and didn't get a chance to clear anything out on the next bars, multiple bars pass before MT5 being opened again, there will remain some artifacts, which I believe I have fixed by adding:

(edit 2: i and start were already declared earlier, they get reused for a bunch of stuff)

//--- clear out potential old current line buffer values that were left behind
//--- this only needs to be done once at the beginning
   if(prev_calculated == 0)   //first run
     {
      start = 0;  //begin all the way to the left
      for(i=start; i<rates_total-2; i++)
        {
         Buffer_Stoch1_Main_Line_Current[i] = EMPTY_VALUE;
        }
     }
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Other Constants
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Other Constants
  • www.mql5.com
Other Constants - Named Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Files:
 

Does it really matters what a refresh does to indicator buffers ? As you noted by yourself you should always initialize your buffers and refresh call OnCalculate with prev_calculated=0.

The rest is internal business of the platform. If you really want to know more, you can check it by yourself by comparing the values before and after a refresh.

 
Alain Verleyen #:

Does it really matters what a refresh does to indicator buffers ? As you noted by yourself you should always initialize your buffers and refresh call OnCalculate with prev_calculated=0.

The rest is internal business of the platform. If you really want to know more, you can check it by yourself by comparing the values before and after a refresh.

At the end of the day I suppose it does not matter, although it was a burning curiosity, though apparently it has simmered down and I don't care anymore either :D