Download MetaTrader 5
To add comments, please log in or register
Do you want to deposit to your account? Do this in any convenient way!
Dominic Gilbert
1632
Dominic Gilbert 2014.10.07 11:22 

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";

Carl Schreiber
5959
Carl Schreiber 2014.10.07 11:49  
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!
whroeder1
13641
whroeder1 2014.10.07 13:31  
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
    :
    

Dominic Gilbert
1632
Dominic Gilbert 2014.10.07 15:23  
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...??
Carl Schreiber
5959
Carl Schreiber 2014.10.07 16:18  

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();
    ...
  }
  ..
}
rocketman99
122
rocketman99 2014.10.08 03:32  

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.

Dominic Gilbert
1632
Dominic Gilbert 2014.10.08 08:38  
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. 

/
To add comments, please log in or register