# OnCalculate function

17

I am building a new indicator using the OnCalculate function for the first time. As I'm going through the logic in my brain, I can't get past the problem of what to do if I have multiple For loops that depend on prev_calculated settings when a recall occurs in the middle of a For loop. For example, a recall of OnCalculate occurs at iteration 162  of For loop 1, and prev_calculated is set at 162 upon re-entry of the OnCalculate function. For loop 1 continues where it was interrupted and completes its job. Here is where I see a problem... For Loop 2 is now using the prev_calculated value of 162 as the starting point, never processing bars 0 through 161. Can someone please shed some light on how to get through this? It may be something simple that I don't understand about how OnCalculate works, but I want to get past this so I can continue with the logic for my Indicator. Thank you in advance!
|
delete
|

OnCalculate Function in Indicator - MQL4 forum
• www.mql5.com
OnCalculate Function in Indicator - MQL4 forum
3360

tegabach:
I am building a new indicator using the OnCalculate function for the first time. As I'm going through the logic in my brain, I can't get past the problem of what to do if I have multiple For loops that depend on prev_calculated settings when a recall occurs in the middle of a For loop. For example, a recall of OnCalculate occurs at iteration 162  of For loop 1, and prev_calculated is set at 162 upon re-entry of the OnCalculate function. For loop 1 continues where it was interrupted and completes its job. Here is where I see a problem... For Loop 2 is now using the prev_calculated value of 162 as the starting point, never processing bars 0 through 161. Can someone please shed some light on how to get through this? It may be something simple that I don't understand about how OnCalculate works, but I want to get past this so I can continue with the logic for my Indicator. Thank you in advance!
|
delete
|

No interrupts would appear during any event method, but timeout when closing the entire GUI. All history buffers stay unchanged until the event method finishes.

17

Ovo Cz:

No interrupts would appear during any event method, but timeout when closing the entire GUI. All history buffers stay unchanged until the event method finishes.

Thank you! Excuse my ignorance, I am new to MQL4 programming... Can you elaborate on the term 'event method'? From the example given below, if you don't mind, can you identify the event methods? Thank you!

```//+------------------------------------------------------------------+
//| Ichimoku Kinko Hyo                                               |
//+------------------------------------------------------------------+
int OnCalculate(const 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[],
{
int    i,k,pos;
double high_value,low_value;
//---
if(rates_total<=InpTenkan || rates_total<=InpKijun || rates_total<=InpSenkou)
return(0);
//--- counting from 0 to rates_total
ArraySetAsSeries(ExtTenkanBuffer,false);
ArraySetAsSeries(ExtKijunBuffer,false);
ArraySetAsSeries(ExtSpanA_Buffer,false);
ArraySetAsSeries(ExtSpanB_Buffer,false);
ArraySetAsSeries(ExtChikouBuffer,false);
ArraySetAsSeries(ExtSpanA2_Buffer,false);
ArraySetAsSeries(ExtSpanB2_Buffer,false);
ArraySetAsSeries(open,false);
ArraySetAsSeries(high,false);
ArraySetAsSeries(low,false);
ArraySetAsSeries(close,false);
//--- initial zero
if(prev_calculated<1)
{
for(i=0; i<InpTenkan; i++)
ExtTenkanBuffer[i]=0.0;
for(i=0; i<InpKijun; i++)
ExtKijunBuffer[i]=0.0;
for(i=0; i<ExtBegin; i++)
{
ExtSpanA_Buffer[i]=0.0;
ExtSpanA2_Buffer[i]=0.0;
}
for(i=0; i<InpSenkou; i++)
{
ExtSpanB_Buffer[i]=0.0;
ExtSpanB2_Buffer[i]=0.0;
}
}
//--- Tenkan Sen
pos=InpTenkan-1;
if(prev_calculated>InpTenkan)
pos=prev_calculated-1;
for(i=pos; i<rates_total; i++)
{
high_value=high[i];
low_value=low[i];
k=i+1-InpTenkan;
while(k<=i)
{
if(high_value<high[k])
high_value=high[k];
if(low_value>low[k])
low_value=low[k];
k++;
}
ExtTenkanBuffer[i]=(high_value+low_value)/2;
}
//--- Kijun Sen
pos=InpKijun-1;
if(prev_calculated>InpKijun)
pos=prev_calculated-1;
for(i=pos; i<rates_total; i++)
{
high_value=high[i];
low_value=low[i];
k=i+1-InpKijun;
while(k<=i)
{
if(high_value<high[k])
high_value=high[k];
if(low_value>low[k])
low_value=low[k];
k++;
}
ExtKijunBuffer[i]=(high_value+low_value)/2;
}
//--- Senkou Span A
pos=ExtBegin-1;
if(prev_calculated>ExtBegin)
pos=prev_calculated-1;
for(i=pos; i<rates_total; i++)
{
ExtSpanA_Buffer[i]=(ExtKijunBuffer[i]+ExtTenkanBuffer[i])/2;
ExtSpanA2_Buffer[i]=ExtSpanA_Buffer[i];
}
//--- Senkou Span B
pos=InpSenkou-1;
if(prev_calculated>InpSenkou)
pos=prev_calculated-1;
for(i=pos; i<rates_total; i++)
{
high_value=high[i];
low_value=low[i];
k=i+1-InpSenkou;
while(k<=i)
{
if(high_value<high[k])
high_value=high[k];
if(low_value>low[k])
low_value=low[k];
k++;
}
ExtSpanB_Buffer[i]=(high_value+low_value)/2;
ExtSpanB2_Buffer[i]=ExtSpanB_Buffer[i];
}
//--- Chikou Span
pos=0;
if(prev_calculated>1)
pos=prev_calculated-1;
for(i=pos; i<rates_total; i++)
ExtChikouBuffer[i]=close[i];
//---
return(rates_total);
}
//+------------------------------------------------------------------+```
36131

Hello,

Please use the SRC button when you post code. Thank you.

This time, I edited it for you.

36131

tegabach:

Thank you! Excuse my ignorance, I am new to MQL4 programming... Can you elaborate on the term 'event method'? From the example given below, if you don't mind, can you identify the event methods? Thank you!

OnCalculate is an "event method".

Client Terminal Events - MQL4 Documentation
• docs.mql4.com
Client Terminal Events - MQL4 Documentation
17

Alain Verleyen:

OnCalculate is an "event method".

`Thank you! That explains it all. There is no interrupt during a pass of OnCalculate, so the process ignores new ticks of data until control is passed on the Return. My question has been answered, so now I can proceed...`
839

tegabach:

Thank you! Excuse my ignorance, I am new to MQL4 programming... Can you elaborate on the term 'event method'? From the example given below, if you don't mind, can you identify the event methods? Thank you!

"Method" is also used to describe a "function", although usually incorrectly many times.  According to what I have read, a function is what you would think, while a method is a specific type of function that specifically operates on an object.  A function can also exist outside of objects, while methods are usually defined within a specific object.