You'll probably get much faster what you need if you search here first, because there's almost nothing that hasn't already been programmed for MT4/MT5 and is ready even for you.
Searching for newbar mt5 returns this: https://www.mql5.com/en/articles/2169
- www.mql5.com
-
if(m_lastBarOpenedAt < m_time[0]) {
Use not equal. There was a post around 2002, where a server clock was mis-set and then corrected — stopped the EA for days!
-
static datetime prevTime = iTime(strSymbol, TF, 0);
That is not an assignment; it's initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.
-
They are initialized once on program load.
-
They don't update unless you assign to them.
-
In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).
MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and
Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
-
Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
external static variable - MQL4 programming forum #2 (2013)
-
-
return (prevTime != (prevTime = iTime(strSymbol, TF, 0)));
You assign to prevTime and then test if the result is not equal to prevTime. Always false.
New candle - MQL4 programming forum #3 (2014) -
if(IsNewBar(symbol1, TF_M1)) … ⋮ if(IsNewBar(symbol2, TF_M1)) … Will be true if a new tick has not yet been seen
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)
You'll probably get much faster what you need if you search here first, because there's almost nothing that hasn't already been programmed for MT4/MT5 and is ready even for you.
Searching for newbar mt5 returns this: https://www.mql5.com/en/articles/2169
Hi Carl,
I checked the document in the post, very helpful. Thanks a lot for your reply.
Daniel
-
Use not equal. There was a post around 2002, where a server clock was mis-set and then corrected — stopped the EA for days!
-
That is not an assignment; it's initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.
-
They are initialized once on program load.
-
They don't update unless you assign to them.
-
In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).
MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and
Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
-
Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
external static variable - MQL4 programming forum #2 (2013)
-
-
You assign to prevTime and then test if the result is not equal to prevTime. Always false.
New candle - MQL4 programming forum #3 (2014) -
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)
Hi William,
Thank you very much for your patient and detailed explaination, will update the code accordingly and post when it's working properly.
Daniel
the class below works fine after test for one day.
class CIsNewBar { private: string m_symbol; ENUM_TIMEFRAMES m_tf; datetime m_lastBarOpenedAt; datetime m_time[1]; public: CIsNewBar(string symbol,ENUM_TIMEFRAMES tf) { m_symbol=symbol; m_tf=tf; CopyTime(m_symbol, m_tf, 0, 1, m_time); m_lastBarOpenedAt = m_time[0]; } ~CIsNewBar() {} bool isNewBar() { CopyTime(m_symbol, m_tf, 0, 1, m_time); if(m_lastBarOpenedAt != m_time[0]) { m_lastBarOpenedAt = m_time[0]; return(true); } else { return(false); } } };
below function didn't work well, I will go with the class
bool IsNewBar(string strSymbol, ENUM_TIMEFRAMES TF) { static datetime prevTime = iTime(strSymbol, TF, 0); datetime tmpTime=iTime(strSymbol, TF, 0); bool result=(prevTime!=tmpTime); prevTime=tmpTime; return (result); }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Happy new year to all,
is someone able to help check what's wrong with this code?
Here is the output, there are many missing outputs: suppose to be 1,2,3,4,1,2,3,4 .......after time output of each line.