Whats wrong with this code for detecting new bar please

 
NewCandle = FALSE;
datetime StartBar = iTime("EURUSD",PERIOD_M5,0);
datetime NewBar = StartBar;
while(! NewCandle)
{
NewBar = iTime("EURUSD",PERIOD_M5,0);
if(NewBar != StartBar)
{
NewCandle = TRUE;
StartBar = NewBar;
}
 

Im not sure about your specific code but I use something simple like:


Declare variable double Bar1;

Oninit() {Bar1= iClose(Symbol(),0,1);} // initialize Bar1 value to Previous bar from current bar


Ontick() {

if(iClose(Symbol(),0,1) != Bar1){  // It is a new bar


//run code


Bar1= iClose(Symbol(),0,1);} /give Bar1 the new value of the current bar at position 1 i.e the previous bar

}

 
  1. Don't double post! You already had this thread open.
              General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  3. You are using all CPU doing nothing - it's your electric bill.

  4. Once you exit the loop, your Predefined Variables are wrong.

  5. Your code fails if M5 history needs to be updated. Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4

    The function linked to, opens a hidden chart for the symbol/TF in question (if not already open,) thus updating history, and temporarily placing the symbol on Market Watch (if not already there,) so SymbolInfoDouble(symbol, SYMBOL_BID) or MarketInfo(symbol, MODE_BID) don't also return zero on the first call.

  6. Your code fails if there are broker declarations.
    Broker's use a variety of naming patterns: EURUSD, EURUSDc, EURUSDct, EURUSDi, EURUSDm, EURUSDecn, EURUSDpro, "EUR.USD", "EUR/USD", "EURUSD#", "EURUSD.", "EURUSD..", "EURUSD.c", "EURUSD.G", "EURUSD.SBe", "EURUSD.stp", "EURUSD+", "EURUSD-sb", etc.
    Don't hard code things; just use the predefined _Symbol, or add the adornments to predefined _Symbol via Input Variables, or calculate the adornments:
              SymbolName/Symbol. OrderSend - Symbols - MQL4 programming forum

  7. Code can be simplified to two lines.
    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 programming forum

 
Jimparker:

Do not double post.

I have deleted your duplicate post.

 
Brian Rumbles:

Im not sure about your specific code but I use something simple like:


Declare variable double Bar1;

Oninit() {Bar1= iClose(Symbol(),0,1);} // initialize Bar1 value to Previous bar from current bar


Ontick() {

if(iClose(Symbol(),0,1) != Bar1){  // It is a new bar


//run code


Bar1= iClose(Symbol(),0,1);} /give Bar1 the new value of the current bar at position 1 i.e the previous bar

}

 
Jimparker:

Thanks Brian.

Does onint() go in the early code before init()

and

does ontick() go after start()

Thanks

 
Jimparker:

Thanks Brian.

Does onint() go in the early code before init()

and

does ontick() go after start()

Thanks

It is not before or after.

OnInit goes instead of init and OnTick goes instead of start

 
Start using the new Event Handling Functions.
          Event Handling Functions - Functions - Language Basics - MQL4 Reference
 
Thanks Andrey