The new candle check is wrong in two ways. First, you can call it only once, if you call it several times it will return false even if it is a new candle. See the static? Second, don't check the bar count, check the time of the current bar, it's more reliable (bar counts may change due to history updates.)
bool IsThisANewCandle() { static datetime bartime=0; //-- last time you've seen a new bar datetime dt=iTime(_Symbol,_Period,0); if(dt==bartime) return false; //-- no new bar bartime=dt; //-- a new bar opened, great return true; } bool IsNewBar; void OnTick() { IsNewBar=IsThisANewCandle(); ... // now work with IsNewBar instead of calling the function }
The new candle check is wrong in two ways. First, you can call it only once, if you call it several times it will return false even if it is a new candle. See the static? Second, don't check the bar count, check the time of the current bar, it's more reliable (bar counts may change due to history updates.)
Exactly.
For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
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
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi , I am learning to code. The EA above is for practice. it is suppose to open a trade if the condition 2 or are met and then manage trades by moving stops to bottom of pervious candle. It is opening a trade on every tick and not closing them. The new candle function seems to work in isolation and so does the open trade function as well as the trade entry conditions. But together it does not. Any advise would be appreciated. Thank you in advance.