start of a new bar in m30 timeframe

 

hello all,


just wanna know how to identify start of new candle in mq4 ..seen itime but can't understand it what value it returns.

 

iTime()  returns a datetime variable . . 

datetime M30Bar1Time;


if(iTime(NULL, PERIOD_M30, 1) != M30Bar1Time)
   {
   //  we have a new M30 bar . . . .
   
   M30Bar1Time = iTime(NULL, PERIOD_M30, 1);
   }
 
it keeps on repeating..ie with each tick  the condition if(iTime(NULL, PERIOD_M30, 1) != M30Bar1Time)
get fulfilled ....i have to be alerted only once when new bar forms..   
 
ankit29030:
it keeps on repeating..ie with each tick  the condition if(iTime(NULL, PERIOD_M30, 1) != M30Bar1Time)
My code doesn't . . .  show your code.
 
ankit29030: just wanna know how to identify start of new candle in mq4 ..seen itime but can't understand it what value it returns.
  1. datetime M30Bar1Time;
    
    
    if(iTime(NULL, PERIOD_M30, 1) != M30Bar1Time)
       {
       //  we have a new M30 bar . . . .
       
       M30Bar1Time = iTime(NULL, PERIOD_M30, 1);
       }
    IFF M30Bar1Time is static or common (global scope) that will identify the start of a new M30 bar

  2. Also you can compute directly:
    static datetime timeM30;
    datetime now = TimeCurrent(),
             tM30= now - now % 1800;
    if (timeM30 == tM30) return; timeM30 = tM30;
    // start of a new M30 bar.

  3. If you want to identify the start of a new candle on the chart time frame:
    static datetime time0; if (time0 == Time[0]) return; time0 = Time[0];
    // start of a new bar.

 
RaptorUK:

iTime()  returns a datetime variable . . 

 

 

int start()
  {
datetime M30Bar1Time;


if(iTime(NULL, PERIOD_M30, 1) != M30Bar1Time)
   {
   Alert("  we have a new M30 bar . . . .");
   
   M30Bar1Time = iTime(NULL, PERIOD_M30, 1);
   }
   return(0);
  }

see with each tick it gives an alert "new bar formation"



 
Read WHRoeder post. He told you to use static.
 
ankit29030:

see with each tick it gives an alert "new bar formation"

You were meant to read what I posted and understand it before using it . . .   this should be a globally declared variable . . .  not local to start()

datetime M30Bar1Time;
 
RaptorUK:

You were meant to read what I posted and understand it before using it . . .   this should be a globally declared variable . . .  not local to start()

 


u wont told me to declare it globally.i was following your post..anyways the problems being solved.
 
ankit29030:

u wont told me to declare it globally.i was following your post..anyways the problems being solved.

I didn't tell you to copy and paste my code directly into start() either but you took the decision to do it,  take responsibility for "your" code.  If I told you to add this to your code and follow the instructions in every Alert it made would you ?

Alert("Hurry, the end of the world is today,  find a cliff and jump ! ! ! !");

 
The important thing is that you understand why the code that you used did what it did . . . .  do you understand why using a globally declared variable fixes the problem ?  do you know what a static variable is ?

 
RaptorUK:

iTime()  returns a datetime variable . . 

 

 


sir this condition executes 2 -3 times during a bar formation....wat to do??
Reason: