Failing to Understand IndicatorCounted

 

This code is taken directly from the manual/tutorial in the Custom Indicators section - https://book.mql4.com/samples/icustom

I cannot seem to understand how it is that 'IndicatorCounted' (Counted_bars)  can equal anything other than (Bars-1), unless the program is on its first iteration of the start() function.  

As far as I understand, 'IndicatorCounted' returns the amount of bars 'not changed' since the function was last launched. So if there are 300 bars on the chart and the array element values 0-299 have already been defined, then 'Counted_bars' must equal 299 at the next iteration of the start() function (as only the values of the 0 bar have changed since the function was last launched - (300-1=299)). 

The manual however insists that in the following situation:


'Bars' = 301
'Counted_bars' = 299

I just cannot seem to get my head around this one! Essentially, I can't understand how the values of the last (1) bar have 'changed' since the function was last launched. I have run the indicator myself and can see that on the first tick of the new bar:

There is clearly a discrepancy of 2 between 'Bars' and 'Counted_bars'.

If somebody can offer an explanation clearer than that given in the manual/tutorial as to why this would be the case, I would be greatly appreciative.

Very new to coding so apologies if my question seems silly.  

#property indicator_chart_window    // Indicator is drawn in the main window
#property indicator_buffers 2       // Number of buffers
#property indicator_color1 Blue     // Color of the 1st line
#property indicator_color2 Red      // Color of the 2nd line
 
double Buf_0[],Buf_1[];             // Declaring arrays (for indicator buffers)
//--------------------------------------------------------------------
int init()                          // Special function init()
  {
   SetIndexBuffer(0,Buf_0);         // Assigning an array to a buffer
   SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);// Line style
   SetIndexBuffer(1,Buf_1);         // Assigning an array to a buffer
   SetIndexStyle (1,DRAW_LINE,STYLE_DOT,1);// Line style
   return;                          // Exit the special funct. init()
  }
//--------------------------------------------------------------------
int start()                         // Special function start()
  {
   int i,                           // Bar index
       Counted_bars;                // Number of counted bars
//--------------------------------------------------------------------
   Counted_bars=IndicatorCounted(); // Number of counted bars
   i=Bars-Counted_bars-1;           // Index of the first uncounted
   while(i>=0)                      // Loop for uncounted bars
     {
      Buf_0[i]=High[i];             // Value of 0 buffer on i bar
      Buf_1[i]=Low[i];              // Value of 1st buffer on i bar
      i--;                          // Calculating index of the next bar
     }
//--------------------------------------------------------------------
   return;                          // Exit the special funct. start()
  }
Creation of Custom Indicators - Simple Programs in MQL4 - MQL4 Tutorial
Creation of Custom Indicators - Simple Programs in MQL4 - MQL4 Tutorial
  • book.mql4.com
When creating a trading strategy a developer often faces the necessity to draw graphically in a security window a certain dependence calculated by a user (programmer). For this purpose MQL4 offers the possibility of creating custom indicators. Custom Indicator is an application program coded in MQL4; it is basically intended for graphical...
 
  1. Stop using IndicatorCounted. Use the new event handlers.
              Event Handling Functions - Functions - Language Basics - MQL4 Reference
              How to do your lookbacks correctly.

  2. koranged: I just cannot seem to get my head around this one! Essentially, I can't understand how the values of the last (1) bar have changed since the function was last launched. I have run the indicator myself and can see that on the first tick of the new bar:
    A new bar started, therefor the last tick of (now) bar 1 may or may not have been processed. Remember, ticks can be missed.
 
whroeder1:
  1. Stop using IndicatorCounted. Use the new event handlers.
              Event Handling Functions - Functions - Language Basics - MQL4 Reference
              How to do your lookbacks correctly.

  2. A new bar started, therefor the last tick of (now) bar 1 may or may not have been processed. Remember, ticks can be missed.

In this instance apparently that isn't the case. From manual:

"(at) the last tick of the previous bar (at the moment t 2) the function start() was successfully started and execute"

There is an example given later on where the last but one tick is not processed, but in this particular example, the manual specifically says that at the last tick... the function start() was successfully executed. 

 
  1. When start returns the last tick has been processed. No guarantee that prior ticks were.
  2. Don't double post!
              General rules and best pratices of the Forum. - General - MQL5 programming forum
 
whroeder1:
  1. When start returns the last tick has been processed. No guarantee that prior ticks were.
  2. Don't double post!
              General rules and best pratices of the Forum. - General - MQL5 programming forum

The situation you are referring to is the exactly the same as the third example given in the manual - https://book.mql4.com/samples/icustom

The situation I am referring to is the second example given in the manual - which makes no mention of prior ticks not being processed. 

Here is the full example:

Variant 2. A new tick is the first tick of a zero bar (happens from time to time).

In this case the fact of appearance of a new bar is important. Before control is passed to the special function start(), client terminal will draw again all bars present in the security window and re-index all declared indicator arrays (set in correspondence with buffers). Besides, client terminal will remember that there are already 301 bars, not 300 in a chart window.

Fig. 118 contains situation when on the last tick of the previous bar (at the moment t 2) the function start() was successfully started and executed. That's why, though now the first bar (with index 1) finished at the moment t 2 was calculated by the indicator, function IndicatorCounted() will return value that was on the previous bar, i.e. 299:

Counted_bars=IndicatorCounted(); // Number of counted bars

In the next line index i will be calculated, in this case for the first tick of a new bar it will be equal to 1 (301-299-1):

 i=Bars-Counted_bars-1;           // Index of the first uncounted

It means calculation of indicator array values in while() loop at the appearance of a new bar will be performed both for the last bar and for the new zero bar. A little earlier during re-indexation of indicator arrays the client terminal increased sizes of these arrays. Values of array elements with zero indexes were not defined before the calculations in the loop. During calculations in the loop these elements get some values. When calculations in start() are over, control is returned to the client terminal. After that the client terminal will draw indicator lines on the zero bar based on just calculated values of array elements with zero indexes.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Creation of Custom Indicators - Simple Programs in MQL4 - MQL4 Tutorial
Creation of Custom Indicators - Simple Programs in MQL4 - MQL4 Tutorial
  • book.mql4.com
When creating a trading strategy a developer often faces the necessity to draw graphically in a security window a certain dependence calculated by a user (programmer). For this purpose MQL4 offers the possibility of creating custom indicators. Custom Indicator is an application program coded in MQL4; it is basically intended for graphical...
 
koranged:

I cannot seem to understand how it is that 'IndicatorCounted' (Counted_bars)  can equal anything other than (Bars-1), unless the program is on its first iteration of the start() function.  

Think of imperfect world. Processed bars count can be dropped to 0, if some bars was intially missing in history (for some reason), but then downloaded from server. Or the count can be less than Bars - 1 if there were no connection for quite a lot of time - larger than 1 bar frame (or, for example, when the terminal restarts).

Reason: