- Return true if current 1hr candle closes on 4hr timeframe
- FOREX - Trends, Forecasts and Implications (Episode 18: August 2012)
- How to make the function not to return 0?
just declare a datetime variable and store the bar open time, then compare it to the live value and if it has changed , you know there was a new bar.
datetime D1;
if(D1!=iTime(Symbol(),PERIOD_D1,0)) // new candle on D1 { //Do Something... D1=iTime(Symbol(),PERIOD_D1,0); // overwrite old with new value }
Musngi:
How to detect NEW Bar? I want to reset my indicator variables if there's a new bar. I'm thinking to use the volumes data to detect it but I don't know if that's right formula or best code to do it?? |
| ||
Marco vd Heijden: just declare
D1=iTime(Symbol(),PERIOD_D1,0); |
|
Volume works fine. " Volume[0]==1 " means the first tick of a new bar.
Volume works fine. " Volume[0]==1 " means the first tick of a new bar.
Nope, volume works really badly. Your terminal will miss ticks so sometimes the new bar will start on >1
Use time.
Also realize that when a new bar arrives for your chart symbol, it does not mean that there will be new bars for all symbols.
This is important when you are building multi symbol robots.
After detecting the first new bar for a symbol, it can take quite some time before all symbols will return the new bar time.
So then you check all bar open times in a loop until they are all returning the new open time.
Nope, volume works really badly. Your terminal will miss ticks so sometimes the new bar will start on >1
Use time.
I've never had a problem using the ==1. But I understand that the terminal might miss a tick or two and there is a better way.
You are right. Time is better and more reliable.
I guess you can also monitor previous open candle open prices. If the open prices are no longer the same , it means that a new candle has opened and the candle index numbers have moved up. So there is a very slight chance that the open price might be the same, so i would pick any two random candles and monitor for change. The problem with this method is, you are dependent on the next tick , so its not good for a time sensitive function, unless you run on a timer.
// Create in global scope to map the initial open prices of candle 0 and candle 5 (Any random candles) double barOpen1 = Open[0]; double barOpen2 = Open[5]; void OnTick() { if(barOpen1 != Open[0] || barOpen2 != Open[5]) //If any of the statements pass, a new candle must have opened changing the index number. { barOpen1 = Open[0]; // Update variables with current prices barOpen2 = Open[5]; } }
- www.mql5.com
But the open price can be the same as the previous bar while the open time can not be the same so time is the preferred method.
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.
New candle - MQL4 programming forum #3 2014.04.04
I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
Running EA once at the start of each bar - MQL4 programming forum 2011.05.06
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use