Understanding Closed Bars Signal Generation in MQL5

 

Hello,

I'm programming several indicators for an expert advisor. Looking at other expert advisors I use as reference, I noticed that indicator signals usually happen only after the bar is closed, not during open bars. I could only reproduce this behavior using 'rates_total-1' in the loop I use for signal generation.

for(int i=MathMax(backstep,prev_calculated-1); i < rates_total-1 && !IsStopped();i++)

I don't want the signal logic to execute on each tick of an open bar. I want it to consider only closed bars. Is the approach I'm using correct? Or do you suggest another way?

Additionally, studying other programmers' code, I saw a case where 'return (rates_total-1)' was used in the OnCalculate function. I asked an AI to search for citations about this in MQL5 documentation, and it said that 'return (rates_total-1)' is a recommended approach, considered optimized, that avoids reprocessing by returning only the last valid index of the indicator.

However, I don't see 'return(rates_total-1)' in MetaQuotes indicators. So I'm wondering if this is a correct or necessary approach. I'd like to understand this better. I appreciate any help.

Considering that my indicators are not just plots or calculations, I generate signals for an expert advisor. I think that maybe for signals this issue might have some relevance.

Thanks!

 
Cassio Centurion:

Hello,

I'm programming several indicators for an expert advisor. Looking at other expert advisors I use as reference, I noticed that indicator signals usually happen only after the bar is closed, not during open bars. I could only reproduce this behavior using 'rates_total-1' in the loop I use for signal generation.

I don't want the signal logic to execute on each tick of an open bar. I want it to consider only closed bars. Is the approach I'm using correct? Or do you suggest another way?

Additionally, studying other programmers' code, I saw a case where 'return (rates_total-1)' was used in the OnCalculate function. I asked an AI to search for citations about this in MQL5 documentation, and it said that 'return (rates_total-1)' is a recommended approach, considered optimized, that avoids reprocessing by returning only the last valid index of the indicator.

However, I don't see 'return(rates_total-1)' in MetaQuotes indicators. So I'm wondering if this is a correct or necessary approach. I'd like to understand this better. I appreciate any help.

Considering that my indicators are not just plots or calculations, I generate signals for an expert advisor. I think that maybe for signals this issue might have some relevance.

Thanks!

run a test indicator in the tester and check it's behavior if it draws up to the bar next to the live bar only.

imo the best method is to run the calculation internally in the EA 

And you will have to be vigilant with the indicator , if more bars load in the past or get spliced in, prev_calculated will turn to 0 so if an EA is "listening" on every tick it will pick it up as a signal.(because your loop will run again)

If you ignore the newly loaded bars indication in the indicator your buffers with the signal will desync from the chart

 
Cassio Centurion:

Hello,

I'm programming several indicators for an expert advisor. Looking at other expert advisors I use as reference, I noticed that indicator signals usually happen only after the bar is closed, not during open bars. I could only reproduce this behavior using 'rates_total-1' in the loop I use for signal generation.

I don't want the signal logic to execute on each tick of an open bar. I want it to consider only closed bars. Is the approach I'm using correct? Or do you suggest another way?

Additionally, studying other programmers' code, I saw a case where 'return (rates_total-1)' was used in the OnCalculate function. I asked an AI to search for citations about this in MQL5 documentation, and it said that 'return (rates_total-1)' is a recommended approach, considered optimized, that avoids reprocessing by returning only the last valid index of the indicator.

However, I don't see 'return(rates_total-1)' in MetaQuotes indicators. So I'm wondering if this is a correct or necessary approach. I'd like to understand this better. I appreciate any help.

Considering that my indicators are not just plots or calculations, I generate signals for an expert advisor. I think that maybe for signals this issue might have some relevance.

Thanks!

the return of OnCalculate (rates_total) is only to provide information to the kernel about how many bars have been updated, so you can use rates_total - 1 if you want but I don't think it's necessary. If you return less than the bars needed it can affect the working state of the indicator

this loop you have is fine although it will ignore the current bar entirely for signals

for(int i=MathMax(backstep,prev_calculated-1); i < rates_total-1 && !IsStopped();i++)

if you wanted to trade after current bar completion (and check the current close price again), then you should make a separate function that identifies when a new bar has arrived and make it return true or false, true if there's a new bar, and false otherwise


if you like bars of confirmation (which is usually good as it's stable), you can extend the trend signal to look at the current as well as previous 2 bars:

if(ma[i] > ma[i-1] && ma[i-1] > ma[i-2])
     uptrend[i] = 1; downtrend[i] = 0; 

 and then you can go back to looping towards rates_total instead of rates_total  - 1

 

Rates_total - 1works for closed bars & return (rates_total - 1) help in efficiency. Have you tested its speed in your EA?