Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1468

 
Novichokkk number of bars available to the indicator for calculation and corresponds to the number of bars available on the chart.

For example MA5, average of 5 bars, set to display 100 bars. Then the indicator will calculate from the 104th to the 100th bar and will start drawing the line from the 100th bar. Then rates_total=5, or 100, or 104?

Rates_total is the number of bars of the chart available for calculation. In other words, it is Bars().

prev_calculated is how many bars the indicator has calculated at the previous OnCalculate() call. It is necessary for organising economical calculations.

If prev_calculated is equal to rates_total, it means that nothing has been calculated yet, and it is necessary to initialise the indicator buffers and perform a full calculation of the whole history.

In general, we can use the following construction:

//--- Проверка и расчёт количества просчитываемых баров
   int limit=rates_total-prev_calculated;
   if(limit>1)
     {
      // Присваиваем limit значение количества доступных баров-1
      limit=rates_total-1;
      // Вот тут нужно инициализировать все буферы пустыми значениями
      ArrayInitialize(Buffer0,EMPTY_VALUE);
      ArrayInitialize(Buffer1,0);
      ArrayInitialize(Buffer2,clrNONE);
      // ... и т.д.
     }
//--- Экономный просчёт индикатора
   for(int i=limit;i>=0;i--)
     {
      Buffer0[i]=(/*что-то там*/);
     }
 
Artyom Trishkin #:

rates_total is the number of chart bars available for calculation. In other words, it is Bars().

prev_calculated is how many bars the indicator has calculated on the previous call OnCalculate(). It is necessary to organise economical calculations.

If prev_calculated is equal to rates_total, it means that nothing has been calculated yet, and it is necessary to initialise the indicator buffers and perform a full calculation of the whole history.

In general, we can use the following construction:

Why not write instead of rates_total - Bars()?

int limit=Bars()-prev_calculated;

Sorry, of course, for being a lamer. I'm digging through the documentation, several books on MKL5. Not all the nuances are explained. Not in the books either. Separately on the language in the books is clear. How constructions are assembled-caput, what from where and for what.... I'm getting into it. A lot of checks and double-checks. I opened a simple mashki code, and there are so many lines. And it seemed that all you need to do is to unload an array (or access it), calculate for one value from such-and-such index in the array to such-and-such, and go through a sliding window with these calculations on the array indicating by indices from where to where. Yes to display the line on the screen. No.
 
Novichokkk #:

Why not write Bars() instead of rates_total ?

Sorry, of course, for being a lamer. I'm digging through the documentation, several books on MKL5. Not all the nuances are explained. In the books too. Separately on the language in the books is clear. How constructions are assembled-caput, what from where and for what.... I'm getting into it. A lot of checks and double-checks. I opened a simple mashki code, and there are so many lines. And it seemed that all you need to do is to unload an array (or access it), calculate for one value from such-and-such index in the array to such-and-such, and go through a sliding window with these calculations on the array indicating by indices from where to where. Yes to display the line on the screen. And no.

You can use such a construction too. But why? When there are already predefined variables in the handler's parameters...

Bars() should be used when you really need it - when getting the number of available bars by another symbol/timestamp.

 
Artyom Trishkin #:

You can use this design too. But why? When there are already predefined variables in the handler's parameters...

Bars() should be used when it is really needed - when getting the number of available bars by another symbol/timeframe.

Artyom, sometimes anyone gets tired and makes mistakes. Don't consider it a criticism, but rates_total - Bars() will always be zero...

In general, it's very difficult to explain to this character. A victim of the USE...

 
Alexey Viktorov #:

Artyom, sometimes everyone gets tired and makes mistakes. Don't consider it a criticism, but rates_total - Bars() will always be zero...

In general, it is very difficult to explain to this character. A victim of the USE...

Yes, you're right. I didn't think that someone would decide to calculate like that. I saw Bars in the calculation out of the corner of my eye and thought it was instead of rates_total.

It happens, thanks.

 
Artyom Trishkin #:
//--- Проверка и расчёт количества просчитываемых баров
   int limit=rates_total-prev_calculated;
   if(limit>1)
     {
      // Присваиваем limit значение количества доступных баров-1
      limit=rates_total-1;
      // Вот тут нужно инициализировать все буферы пустыми значениями
      ArrayInitialize(Buffer0,EMPTY_VALUE);
      ArrayInitialize(Buffer1,0);
      ArrayInitialize(Buffer2,clrNONE);
      // ... и т.д.
     }
//--- Экономный просчёт индикатора
   for(int i=limit;i>=0;i--)
     {
      Buffer0[i]=(/*что-то там*/);
     }

I understand that you wanted to help the person, but apparently by mistake you only confused them.

The economical miscalculation in this case looks a little different.

for(int i=limit;i<rates_total;i++)
 
Aleksandr Slavskii #:

I understand that you wanted to help the person, but apparently by mistake only confused.

The economic miscalculation in this case looks a little different.

What do you think I wrote wrong? Justify it, please.

What is the limit and where will the loop come from in my and your example.

 
Novichokkk #:

Why not write Bars() instead of rates_total ?

In essence rates_total and Bars() are the same thing, onlyBars() is a function, so its call will cost more in terms of execution time than reading the value of the rates_total variable.

 
Artyom Trishkin #:

What do you think I wrote wrong? Justify it, please.

What is the limit and where the loop will come from in my and your example.

Oops))))))

I didn't see it right

if(limit>1)

I didn't see what you wrote at all.

I apologise.

 
Alexey Viktorov #:

Artyom, sometimes anyone gets tired and makes mistakes. Don't consider it a criticism, but rates_total - Bars() will always be zero...

In general, it is very difficult to explain to this character. A victim of the USE...

rates_total isBars() in the sentence. That is,rates_total is Bars().

In the code below

int limit=Bars()-prev_calculated;

He's right.

I didn't take the USE. It didn't exist then.
Reason: