Oncalculate function question!

 


int OnCalculateconst  int         rates_total,
                 const  int         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  int        &spread [])

In the MQL5 reference file, Oncalculate has two forms, the second above.

The MQL reference file shows the mechanism of the Oncalculate function:

1. Call only in custom metrics.

2. Oncalculate function is called every time the price changes.

3, can, with different price status (such as: open, close, etc.) to calculate the index.

4. Parameters:

(1) rates_total (total number of bars in the icon, including bars in the fluctuation?)

(2) prev_calculated (number of bars in the icon, excluding bars in the fluctuation?)

(3) all kinds of arrays (all kinds of data stored, used in calculating indicators)

5. The function returns: prev_calculated.


Question:

1. There should be a difference between rates_total and prev_discretion, but my understanding should be wrong, because in the case of the reference file, rates_total and prev_discretion are the same.

2. The two meanings in the reference documents are not understood at all. I hope the master can translate and guide.

    We should note the connection between the return value of OnCalculate() and the second input parameter prev_calculated. During the function call, the prev_calculated parameter contains a value returned by OnCalculate() during previous call. This allows for economical algorithms for calculating the custom indicator in order to avoid repeated calculations for those bars that haven't changed since the previous run of this function.

    For this, it is usually enough to return the value of the rates_total parameter, which contains the number of bars in the current function call. If since the last call of OnCalculate() price data has changed (a deeper history downloaded or history blanks filled), the value of the input parameter prev_calculated will be set to zero by the terminal.

3. Oncalculate function whether there are other mechanisms, please master guidance, thank you.

Documentation on MQL5: Language Basics / Functions / Event Handling Functions
Documentation on MQL5: Language Basics / Functions / Event Handling Functions
  • www.mql5.com
The MQL5 language provides processing of some predefined events. Functions for handling these events must be defined in a MQL5 program; function name, return type, composition of parameters (if there are any) and their types must strictly conform to the description of the event handler function. The event handler of the client terminal...
 

What is prev_discretion ?

 

helloea: There should be a difference between rates_total and prev_discretion, but my understanding should be wrong, because in the case of the reference file, rates_total and prev_discretion are the same.

There can be. First run prev_calculated is zero.

Otherwise, it is what you previously returned. Most examples return rates_total and thus have to check it on the next tick for first/previous run.

That is unnecessary if you return a correct value.
          How to do your lookbacks correctly.

Reason: