How to properly use the isNewBar function?

 

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

        }
 
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.

 
Muso LLC: I am getting an compile error
  1. An error which you don't state, nor post the code up to the line generating the error. There is nothing wrong with your posted MT4 code.
  2. Why did you post your MT4 question in the MT5 EA section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum?

 
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.
 
William Roeder #:
  1. An error which you don't state, nor post the code up to the line generating the error. There is nothing wrong with your posted MT4 code.
  2. Why did you post your MT4 question in the MT5 EA section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum?

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

 
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.
 
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.
 

Compact version of NewBar()

//+------------------------------------------------------------------+
//| NewBar                                                           |
//+------------------------------------------------------------------+
bool NewBar()
  {
   static datetime prevTime = iTime(_Symbol, PERIOD_CURRENT, 0);
   return (prevTime != (prevTime = iTime(_Symbol, PERIOD_CURRENT, 0)));
  }
 
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: