Test for end of bar

To add comments, please log in or register
dweems
43
dweems  
Is there a way to signal when a bar ends and before the next bar begins?  I'd like to set pending orders before a bar starts (typically a day bar).  Is there a way to do that?  I know how to get the beginning of a bar but getting the end of a bar alludes me.
William Roeder
20207
William Roeder  
dweems: Is there a way to signal when a bar ends and before the next bar begins? 

No, because you never know which tick will be the last one, until you get a new one starting a new bar.

What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific.) requires knowledge of when your broker stops and starts (not necessary the same as the market.)
          "Free-of-Holes" Charts - MQL4 Articles
          No candle if open = close ? - MQL4 and MetaTrader 4 - MQL4 programming forum

Marius Ovidiu Sunzuiana: Try to check something like this... Open[0]==EMPTY_VALUE
No new tick no new bar. New tick, new bar, open will always be a valid price.
nicholi shen
2392
nicholi shen  
dweems:
Is there a way to signal when a bar ends and before the next bar begins?

Yes, it doesn't matter if there is a tick or not you just need to use the OnTimer handler. The first tick after initialization you will set an event timer for the delta of the number of seconds in the period minus the time lapsed since the beginning of the period minus one second. When OnTimer fires you reset the event timer equal to the period seconds so OnTimer will fire one second for the end of each bar in the series.

#property strict
int OnInit()
{
   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
   EventKillTimer();
}
//+------------------------------------------------------------------+
void OnTick()
{
   static bool  false;
   if (!onbar_init) {
      int lapsed = int(TimeCurrent() - Time[0]);
      int seconds = PeriodSeconds(PERIOD_CURRENT) - lapsed - 1;
      EventSetTimer(seconds);
       true;
      Print("Set initial timer to ", seconds);
   }
}

void OnTimer()
{
   EventSetTimer(PeriodSeconds(PERIOD_CURRENT));
   Alert("OnBarEnd");
}
To add comments, please log in or register