How can I hard code conditions/code to be executed once per bar?

I have been using the code below to prevent portions of my code from executing more than once per bar. How can I hard code a specific duration, say 5 minutes? Int BarsCount = 0; int start() { if (Bars>BarsCount) { //executed only once per bar BarsCount = Bars; } return(0); } Please pardon me, I’m not sure how code formatting is done here.

//+------------------------------------------------------------------+
//|                                                      iPeriod.mq5 |
//|      Copyright 2016, Marco vd Heijden, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Marco vd Heijden, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

datetime M1,M5,M15,M30,H1,H4,D1,W1,MN1;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- load open times
   M1=iTime(Symbol(),PERIOD_M1,0);
   M5=iTime(Symbol(),PERIOD_M5,0);
   M15=iTime(Symbol(),PERIOD_M15,0);
   M30=iTime(Symbol(),PERIOD_M30,0);
   H1=iTime(Symbol(),PERIOD_H1,0);
   H4=iTime(Symbol(),PERIOD_H4,0);
   D1=iTime(Symbol(),PERIOD_D1,0);
   W1=iTime(Symbol(),PERIOD_W1,0);
   MN1=iTime(Symbol(),PERIOD_MN1,0);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- check for new candles
   if(M1!=iTime(Symbol(),PERIOD_M1,0)) // new candle on M1
     {
      //Do Something...
      M1=iTime(Symbol(),PERIOD_M1,0);    // overwrite old with new value
     }
   if(M5!=iTime(Symbol(),PERIOD_M5,0)) // new candle on M5
     {
      //Do Something...
      M5=iTime(Symbol(),PERIOD_M5,0);    // overwrite old with new value
     }
   if(M15!=iTime(Symbol(),PERIOD_M15,0))// new candle on M15
     {
      //Do Something...
      M15=iTime(Symbol(),PERIOD_M15,0);  // overwrite old with new value
     }
   if(M30!=iTime(Symbol(),PERIOD_M30,0))// new candle on M30
     {
      //Do Something...
      M30=iTime(Symbol(),PERIOD_M30,0);  // overwrite old with new value
     }
   if(H1!=iTime(Symbol(),PERIOD_H1,0)) // new candle on H1
     {
      //Do Something...
      H1=iTime(Symbol(),PERIOD_H1,0);    // overwrite old with new value
     }
   if(H4!=iTime(Symbol(),PERIOD_H4,0)) // new candle on H4
     {
      //Do Something...
      H4=iTime(Symbol(),PERIOD_H4,0);    // overwrite old with new value
     }
   if(D1!=iTime(Symbol(),PERIOD_D1,0)) // new candle on D1
     {
      //Do Something...
      D1=iTime(Symbol(),PERIOD_D1,0);    // overwrite old with new value
     }
   if(W1!=iTime(Symbol(),PERIOD_W1,0)) // new candle on W1
     {
      //Do Something...
      W1=iTime(Symbol(),PERIOD_W1,0);    // overwrite old with new value
     }
   if(MN1!=iTime(Symbol(),PERIOD_MN1,0)) // new candle on MN1
     {
      //Do Something...
      MN1=iTime(Symbol(),PERIOD_MN1,0);    // overwrite old with new value
     }
  }
//+------------------------------------------------------------------+
bool newBar()

  {

   static datetime TimeBar=0;

   bool flag=false;

    if(TimeBar!=Time[0])

       {

        TimeBar=Time[0];

        flag=true;

       } 
// return true if you are in new bar.
    return (flag);

  }

Thanks a lot. One question though. Does this mean that the code within the block would be checked ONLY ONCE when a new bar is formed?

Your’re welcome. Yes it does.

The above is one of my conditions for a trade to be opened. So this means that if the condition is not met at the first tick - the ea will sleep all the time before the next candle is born. How can I rather check for the condition throughout the lifetime of a candle till it’s satisfied ONLY ONCE? The issue here is the condition could be satisfied after the first tick of the candle. I hope I’ve explained what I’m trying to achieve clearly.

you have at least two condition to be met before trying to send a order.

  1. new bar

  2. your other unmentioned condition

you can either check your #2 condition, every tick (every tick means through candle’s life), and if that’s met, then check if new bar is made to send a order.
but that’s not efficient , if efficiency is of your concerns at all.

or

you can check your condition, only if new bar is made already. it’s better this way.

so in the code Marco gave you, inside a desired block you want (the timeframe you check for new bar), check if your #2 condition is met.

//--- check for new candles
   if(M1!=iTime(Symbol(),PERIOD_M1,0)) // new candle on M1
     {
      if(some condition == 1)
       {
        // Do something...
        M1=iTime(Symbol(),PERIOD_M1,0);    // overwrite old with new value, only when condition becomes true...
       }
     }

thank you very much.