How to properly use the isNewBar function?

Muso LLC  

Hi, I am trying to properly use the isNewBar function based on time and I am getting an compile error when I use the code right after the onTick function is called.

I tried initializing with different constants and arrays but the program is working as expected and the remaining functions are being call on every new bar but on tick.


Any advice on how to use the time based onNewBar function?


void OnTick(){
   static datetime timeCur; datetime timePre = timeCur; timeCur=Time[0];
   bool isNewBar = timeCur != timePre;
   if(isNewBar){ 
                //On every new bar code here
                }
        // On every tick code here

        }
Carl Schreiber  
Muso LLC:

Hi, I am trying to properly use the isNewBar function based on time and I am getting an compile error when I use the code right after the onTick function is called.

I tried initializing with different constants and arrays but the program is working as expected and the remaining functions are being call on every new bar but on tick.


Any advice on how to use the time based onNewBar function?


The answer already exists here, just search for isNewBar (top right the lens) and copy the best solution for you.

Please remember that there is almost nothing that has not been programmed for MT5 (and MT4): Searching and copying is much faster than trying everything out yourself.

Alain Verleyen  
Carl Schreiber #:

The answer already exists here, just search for isNewBar (top right the lens) and copy the best solution for you.

Please remember that there is almost nothing that has not been programmed for MT5 (and MT4): Searching and copying is much faster than trying everything out yourself.

Off topic, but I can't read that without reacting. There is a LOT which has not been programmed for MT5 yet, there are very few solid and serious libraries available.
Revo Trades  
Muso LLC #:

I didn't know this was MT4 code, is there a MT5 variation of this code?

in XX5 there is no Time[0], instead, replace it with iTime(NULL,PERIOD_CURRENT,0)

But i am very sure that if you search the site for words like NewBar that there would be a few versions of working code that you can use for both 4/5.
Muso LLC  
Revo Trades #:

in XX5 there is no Time[0], instead, replace it with iTime(NULL,PERIOD_CURRENT,0)

But i am very sure that if you search the site for words like NewBar that there would be a few versions of working code that you can use for both 4/5.
The code worked, thank you.
Andreas Hoogendoorn  

Compact version of NewBar()

//+------------------------------------------------------------------+
//| NewBar                                                           |
//+------------------------------------------------------------------+
bool NewBar()
  {
   static datetime prevTime = iTime(_Symbol, PERIOD_CURRENT, 0);
   return (prevTime != (prevTime = iTime(_Symbol, PERIOD_CURRENT, 0)));
  }
William Roeder  
Andreas Hoogendoorn #: Compact version of NewBar()
  1. Only initialize static variables with constants.
  2. Depending on how the compiler implements the assignment, that code could always return false.
Reason: