Wait "x" number of bars before calling function "y"

 

I can't remember how to do this, but this snippet of code here:

...        
if( H1_close > ema60 && ema21 >= H1_low )
            {
            TriggerBar = iTime(NULL,low,1); 
            H1_Buy_Touch = "H1 Buy Touch"; 
            }

...

All I am wanting is rather than "H1_Buy_Touch = "H1 Buy Touch";" straight away off the back of that if statement being true, I am instead wanting it to wait "x" number of H1 bar closes before "H1_Buy_Touch = "H1 Buy Touch";".

To elaborate further, once that first "if" statement is true, I then want to wait 3 H1 closes and if H1_close > ema60 ONLY THEN change the string: 
H1_Buy_Touch = "H1 Buy Touch";

 
int WaitXBars = 3;
datetime NotBefore = TimeCurrent()+WaitXBars*_Period*60; // to wait x bars
...
if (TimeCurrent()<NotBefore) return; // to skip.. 
I think even the type casting should work that way!
 
gooly:
int WaitXBars = 3;
datetime NotBefore = TimeCurrent()+WaitXBars*_Period*60; // to wait x bars
...
if (TimeCurrent()<NotBefore) return; // to skip.. 

This does not wait x bars, it waits x bar times from the current moment.

  1. If you want x bar times after this bar, you need to replace TimeCurrent with Time[0].
  2. If there are no ticks during a period (M1 or weekend) it opens on the next bar, not x bars.
    int WaitXBars = 3;
    :
    datetime signalTime = TimeCurrent();
    :
    if (iBarShift(NULL,0, signalTime) < 3) return; // to skip.. 
    // x bars since signalTime
    :
    

 
WHRoeder:

This does not wait x bars, it waits x bar times from the current moment.

  1. If you want x bar times after this bar, you need to replace TimeCurrent with Time[0].
  2. If there are no ticks during a period (M1 or weekend) it opens on the next bar, not x bars.
        if( H1_high >= ema21 && CurrentBigFish6 > H1_close )TriggerBar = iTime(NULL,low,1);
        int BarShift = iBarShift(NULL,0, TriggerBar);
        int WaitXBars = BarShift + 3; // This would be BarShift + 3...
        
...  
I'm wanting to compare WaitXBars above, with the current closed bar... if they're equal, i.e. bar 1090 == 1090 (current), then execute the function...??
 

the index of a bar changes every time a new bar is added.

So if you have detected the bar 1090 then use time:

datetime TmeExecFunction; // global definition
...
void OnTick() {
  for(i=limit;i>=0;i--){
    ...
    int DetectedBar = 1090;
    int WaitXBars = 3;
    TmeExecFunction = Time[DetectedBar] + WaitXBars*_Period*60;
    ...
    if (Time[i] >= TmeExecFunction) doFunction();
    ...
  }
  ..
}
 

Consider using event timers if you do not need to trigger on the formation of a new bar. https://docs.mql4.com/eventfunctions/eventsettimer

I use event timers to read indicators that do not need to be updated at every tick. Timers are also useful if you run a large number of graphs/EA's to stop everything syncing with the start of new bars and causing CPU spikes.

 
Thanks for your responses everyone. I should have probably mentioned, but this bit here:

     if( H1_high >= ema21 && CurrentBigFish6 > H1_close )TriggerBar = iTime(NULL,low,1);
        int BarShift = iBarShift(NULL,0, TriggerBar);
        int WaitXBars = BarShift + 3; // This would be BarShift + 3...
        
...  

Is being called on every IsNewCandle() function.... So if it is a new H1 candle (Time[1]) it will then do CheckMaBias()^^

Under CheckMaBias() is the above^

So I store the first "if" statement under TriggerBar and now I have that stored. When the IsNewCandle() is new again, it will then go on to CheckMaBias() for a second time. As this is a new bar, technically, the last passing of CheckMaBias - where the if statement was true - was bar[1]. This time, it would be bar[2] from the current (bar[0]).

I wouldn't require a for loop. Instead, I just want to have another if statement to say: "you are now on the nth bar away from "TriggerBar" = do the function below NOW.

Sorry if I am beating a dead horse, but I cannot get any of what you're saying above working. 

Reason: