For those who would find it useful: Behold the universal "IsNewBarOnTimeframes()"

macpee
900

I think the below code for IsNewBarOnTimeframes() might be useful for anyone who wishes to monitor the end of a bar from any time frame whatsoever in MT4. Hence, I decided to share it for perusal. Happy using:

bool IsNewBarOnTimeframe(int timeframe)
  {
   datetime BarOpenTime = iTime(Symbol(),timeframe,0);
   if (timeframe == PERIOD_M1) 
     {
      static datetime  TickTime_M1 = BarOpenTime;
      if (TickTime_M1 != BarOpenTime)
        {
         TickTime_M1 = BarOpenTime;
         return true;
        }
      else 
        {
         return false;
        }
     }
   if (timeframe == PERIOD_M5) 
     {
      static datetime  TickTime_M5 = BarOpenTime;
      if (TickTime_M5 != BarOpenTime)
        {
         TickTime_M5 = BarOpenTime;
         return true;
        }
      else 
        {
         return false;
        }
     }
   if (timeframe == PERIOD_M15) 
     {
      static datetime  TickTime_M15 = BarOpenTime;
      if (TickTime_M15 != BarOpenTime)
        {
         TickTime_M15 = BarOpenTime;
         return true;
        }
      else 
        {
         return false;
        }
     }
   if (timeframe == PERIOD_M30) 
     {
      static datetime  TickTime_M30 = BarOpenTime;
      if (TickTime_M30 != BarOpenTime)
        {
         TickTime_M30 = BarOpenTime;
         return true;
        }
      else 
        {
         return false;
        }
     }
   if (timeframe == PERIOD_H1) 
     {
      static datetime  TickTime_H1 = BarOpenTime;
      if (TickTime_H1 != BarOpenTime)
        {
         TickTime_H1 = BarOpenTime;
         return true;
        }
      else 
        {
         return false;
        }
     }
   if (timeframe == PERIOD_H4) 
     {
      static datetime  TickTime_H4 = BarOpenTime;
      if (TickTime_H4 != BarOpenTime)
        {
         TickTime_H4 = BarOpenTime;
         return true;
        }
      else 
        {
         return false;
        }
     }
   if (timeframe == PERIOD_D1) 
     {
      static datetime  TickTime_D1 = BarOpenTime;
      if (TickTime_D1 != BarOpenTime)
        {
         TickTime_D1 = BarOpenTime;
         return true;
        }
      else 
        {
         return false;
        }
     }
   if (timeframe == PERIOD_W1) 
     {
      static datetime  TickTime_W1 = BarOpenTime;
      if (TickTime_W1 != BarOpenTime)
        {
         TickTime_W1 = BarOpenTime;
         return true;
        }
      else 
        {
         return false;
        }
     }
   if (timeframe == PERIOD_MN1) 
     {
      static datetime  TickTime_MN1 = BarOpenTime;
      if (TickTime_MN1 != BarOpenTime)
        {
         TickTime_MN1 = BarOpenTime;
         return true;
        }
      else 
        {
         return false;
        }
     }
   return false;
  }
macpee
900
macpee  

Short and concise but not easy to understand though. Thanks! But why not test it. It says not tested. Also, does the one I posted remove the ability to detect ticks?

William Roeder
28334
William Roeder  
macpee #: ! But why not test it. It says not tested.

I just typed it in. I didn't test it because I don't need it. Apparently, nobody needs it because nobody tested it and/or posted corrections.

BeeXXI Corporation
10852
macpee:

I think the below code for IsNewBarOnTimeframes() might be useful for anyone who wishes to monitor the end of a bar from any time frame whatsoever in MT4. Hence, I decided to share it for perusal. Happy using:

I think it's easier to use through a class. In this case, the symbol can be considered, and not just the timeframe. But you need to create instances of the class for the necessary symbols and timeframes.

class CIsNewBar {
private:
   datetime          last_bar_time_open;
   ENUM_TIMEFRAMES   tf;
   string            symbol;
public:
                     CIsNewBar(ENUM_TIMEFRAMES _tf = 0, string _symbol = NULL) {
      symbol = _symbol;
      tf = _tf;
      last_bar_time_open = iTime(symbol, tf, 0);
   };
                    ~CIsNewBar();
   bool              IsNewBar() {
      datetime cur_bar_time_open = iTime(symbol, tf, 0);
      bool result = cur_bar_time_open != last_bar_time_open;
      last_bar_time_open = cur_bar_time_open;
      return result;
   }
};
R4tna C
419
R4tna C  
Nikolai Semko #:

I think it's easier to use through a class. In this case, the symbol can be considered, and not just the timeframe. But you need to create instances of the class for the necessary symbols and timeframes.

Neat - nice and concise

macpee
900
macpee  
Nikolai Semko #:

I think it's easier to use through a class. In this case, the symbol can be considered, and not just the timeframe. But you need to create instances of the class for the necessary symbols and timeframes.

Looks professional; I should give it a try.

William Roeder
28334
William Roeder  
R4tna C #: Neat - nice and concise
macpee #: Looks professional; I should give it a try.

But only works with one timeframe. If you need more, then you need the code in #1.

R4tna C
419
R4tna C  
William Roeder #:

But only works with one timeframe. If you need more, then you need the code in #1.

Really? I have not tested this example but assumed as timeframe is passed as an argument and used in the iTime functions it would be multi-timeframe.

I had written a similar one which works the same way - that does work across all timeframes, but as we already have so many examples I refrained from posting it

Perhaps if it said it could be fine... just guessing

                     CIsNewBar(ENUM_TIMEFRAMES _tf = PERIOD_CURRENT, string _symbol = NULL) {
BeeXXI Corporation
10852
William Roeder #:

But only works with one timeframe. If you need more, then you need the code in #1.

Why?
The values ​​null and 0 are the default values ​​if there are no parameters when declaring an instance of the class.  Then the current symbol and current timeframe will be used.  Everything will work with parameters.  If you do not specify the second parameter, then the current symbol will be applied.
R4tna C
419
R4tna C  
Nikolai Semko #:
Why?

I tested it on M1 & M5 and it was fine.

Only observation was that if you switch timeframes on the chart it can return true on the first tick - I had that issue too... small bug but easily fixed