stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

IsNewBar code
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=4915
Page 1 of 1
Author:  renexxxx [ Sun Sep 25, 2016 10:57 pm ]
Post subject:  IsNewBar code

I here like to post a little class that I created for detecting whether a new bar has started on a given SYMBOL and TIMEFRAME. For those of you coders, who are only ever creating single-symbol, single-timeframe EAs, you don't need this as you can just use the good old:

Code: Select all

bool isNewBar() {
   static datetime lastBarTime = -1;
   bool newBar = false;

   if ( lastBarTime < iTime( _Symbol, _Period, 0 ) ) {
      lastBarTime = iTime( _Symbol, _Period, 0 ) + 1;
      newBar = true;
   }
   return(newBar);
}
Btw: Note the '+ 1' on line 6 in the code-snippet. This is to avoid triggering two 'isNewBar's' if this code was run inside OnTick() and two new ticks would arrive within the same second (it happens).

The attached CBar class is to be used if you (like myself) are running multi-symbol/multi-timeframe code. As you know ticks for different symbols are arriving at different times. So, it is possible that one symbol has started a new candle (bar) whilst the other is still on the old candle. Is this important? I don't know -- that depends on your code.

The use of the CBar class is dead-simple:

1. Somewhere at the top of you file, you create a CBar object like so

Code: Select all

#include <Bar.mqh>

CBar myBar;
2. Then whenever you need to know if a new candle has started for a given symbol (string) and timeframe (int), you simply call:

Code: Select all

   if ( myBar.isNewBar( symbol, timeFrame ) ) {
      // do something
   }
The CBar class builds up an internal structure each time isNewBar() is called, so that it can store the latest iTime(symbol, timeframe, 0) for each combination of [symbol, timeframe].

I am a big fan of building (and sharing) a set of re-usable software components. This makes the task of creating new EAs much easier and quicker.

Hope this is useful for some of the coders.

Edit: I have made a little change to CBar, to stop it from reporting a new bar when it is first loaded, whilst in the middle of a bar. It should now patiently wait for a real new bar.
Author:  oligodar [ Mon Sep 26, 2016 11:37 am ]
Post subject:  IsNewBar code

Very useful. Thank you.
Author:  gprince66 [ Mon Sep 26, 2016 4:58 pm ]
Post subject:  IsNewBar code

Many thanks renexxxx. I learn something new every time I read one of your posts or scan your code.

:good:

Gary
Author:  heart1010 [ Tue Sep 27, 2016 7:19 am ]
Post subject:  IsNewBar code

Thanks :good:
Author:  Raj4x [ Tue Nov 29, 2016 8:16 pm ]
Post subject:  IsNewBar code

Here's an alternative:

Code: Select all

bool isNewBar() {
   static datetime last_time=0;
   datetime lastbar_time=SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE);

   if(last_time==0) {
      last_time=lastbar_time;
      return(false);
     }

   if(last_time!=lastbar_time) {
      last_time=lastbar_time;
      return(true);
     }

   return(false);
}
Author:  alecsa72 [ Wed Nov 30, 2016 1:27 pm ]
Post subject:  IsNewBar code

Btw: Note the '+ 1' on line 6 in the code-snippet. This is to avoid triggering two 'isNewBar's' if this code was run inside OnTick() and two new ticks would arrive within the same second (it happens).
@renexxxx

If I use +15 instead of +1 in that code snippet - will that mean that I will detect the new bar exactly 15 seconds after the new bar actually started?

PS: I actually do need such functionality
Author:  renexxxx [ Fri Dec 02, 2016 7:47 pm ]
Post subject:  IsNewBar code

alecsa72 ยป Wed Nov 30, 2016 11:27 pm wrote: If I use +15 instead of +1 in that code snippet - will that mean that I will detect the new bar exactly 15 seconds after the new bar actually started?
No, it doesn't. If you need this functionality, you will need to do something like this:

Code: Select all

//+------------------------------------------------------------------+
//| bool isNewBar(int waitSeconds)                                   |
//| ---  Wait for an additional 'waitSeconds' after a new Candle     |
//+------------------------------------------------------------------+
bool isNewBar(int waitSeconds = 300) {

   static datetime prevBar = -1;
   static datetime timeToWait = -1;

   bool newBar=false; 

   if ( iTime(_Symbol,_Period,0) != prevBar ) {
      timeToWait = TimeCurrent() + waitSeconds;       
      prevBar=iTime(_Symbol,_Period,0);
   }

   if ( (timeToWait > 0) && ( TimeCurrent() >= timeToWait ) ) {
      newBar = true;
      timeToWait = -1;
   }

   return(newBar);
}
(This only works for the current symbol and timeframe ...) Also, if waitSeconds is longer than _Period, it won't ever return true.
Author:  alecsa72 [ Fri Dec 02, 2016 9:41 pm ]
Post subject:  IsNewBar code

Thanks!
All times are UTC Page 1 of 1