BarsSince ???

 

Hi.

I am very new to MQL4, so I don't have much experience programing with this language. I am used to Metastock EA language, wich is far different from MQL4. So I would like to know if there is a way to express a function similar to Metastock function "BarsSince(EventXX)". In other words, I need a code to tell if an Event XX ocurred at least 2 bars ago.

Best regards.

 
lmrf:

Hi.

I am very new to MQL4, so I don't have much experience programing with this language. I am used to Metastock EA language, wich is far different from MQL4. So I would like to know if there is a way to express a function similar to Metastock function "BarsSince(EventXX)". In other words, I need a code to tell if an Event XX ocurred at least 2 bars ago.

Best regards.

Use iBarShift().

https://docs.mql4.com/series/iBarShift

 

If you know the datetime of the event, iBarShift.

Otherwise loop through the bars to determine when. For example MA crossover:

for (int shift=0; shift < Bars; shift++){
   double fast.now  = iMA(NULL,0,18, 0, MODE_EMA, PRICE_TYPICAL, shift+0),
          slow.now  = iMA(NULL,0,36, 0, MODE_EMA, PRICE_TYPICAL, shift+0),
          fast.prev = iMA(NULL,0,18, 0, MODE_EMA, PRICE_TYPICAL, shift+1),
          slow.prev = iMA(NULL,0,36, 0, MODE_EMA, PRICE_TYPICAL, shift+1);
   if ( (fast.now-slow.now)*(fast.prev-slow.prev) < 0 ){
       break; // Moving averages crossed at Time[shift] 
   }
}
 

Thanks for the help. I will follow your suggestions and see what I can do.

Best regards.

Reason: