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
}
-
Don't double
post! You already had this thread open.
General rules and best pratices of the Forum. - General - MQL5 programming forum - 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 - You are using all CPU doing nothing - it's your electric bill.
- Once you exit the loop, your Predefined Variables are wrong.
- 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 № 4The 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.
- 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 - 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
Do not double post.
I have deleted your duplicate post.
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
}
Thanks Brian.
Does onint() go in the early code before init()
and
does ontick() go after start()
Thanks
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
Event Handling Functions - Functions - Language Basics - MQL4 Reference
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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;
}