Pausing the EA until next bar opens

 

Hello

I just started creating my own EAs. Very exciting stuff. But I've got a problem. 

After I am stopped out, I would like all EA activity to pause until the next bar opens.

What's happening is that once I'm stopped out, my other order is triggering instantaneously. I don't want this to happen.

I don't want to use the Pause function (time based pause), because that is not the correct way to handle this situation. I need to wait until the next bar opens, then the EA should start checking my conditions.

How do I do this?

Can someone help me?

 

I think I may have found the answer here:

https://www.mql5.com/en/forum/108147 

How to wait until the next bar ?
How to wait until the next bar ?
  • 2008.04.15
  • www.mql5.com
Hello, In my EA, when the current trade have a profit level (for example 50) I make a SL of 25...
 
ladyrose:

I think I may have found the answer here:

https://www.mql5.com/en/forum/108147 

true

good luck

 

For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.

I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
          New candle - MQL4 programming forum

 
bool IsNewBar() { 
   static   datetime lastBar;
            datetime currBar  =  iTime(Symbol(), Period(), 0);
   
   if(lastBar != currBar) {
      lastBar  =  currBar;
      return (true); 
   } else {
      return(false);
   }
}

if (IsNewBar()) {
   //DO THE DAMN THING
}
 
William Roeder:

For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.

I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
          New candle - MQL4 programming forum


Thanks for the heads up. I'm worried about missing ticks.

What I'm doing is using the OrderCloseTime function.

This is what I wrote:

       if (Ticket > 0 && buyCount == 0 && sellCount == 0)
       {
       bool Selected = OrderSelect(Ticket,SELECT_BY_TICKET,MODE_HISTORY);
          if (Time[0] > OrderCloseTime())
          {
          StartTime = Time[0];
          }
       }

Ticket is the last order ticket number.

So basically if there are no orders open, and the time > last order close time, then the code can continue.

Will I be missing ticks with my approach?

It seems to be working just fine. Although I can't tell if I'm skipping ticks on the new bar.

 
Frederick Langemark:
Thanks. I will remember this one.
 

Frederick Langemark:

bool IsNewBar() { 
   static   datetime lastBar;
            datetime currBar  =  iTime(Symbol(), Period(), 0);
   
   if(lastBar != currBar) {
      lastBar  =  currBar;
      return (true); 
   } else {
      return(false);
   }
}

if (IsNewBar()) {
   //DO THE DAMN THING
}

Thanks!

This works better than what I did.

 
ladyrose:

Thanks!

This works better than what I did.

My pleasure. Simple code, but certainly gets the job done.

If you want to ensure the script doesn't run on the current bar when loaded to the chart, just add IsNewBar(); to OnInit()