test on IsNewBar function and class

 

Happy new year to all,

is someone able to help check what's wrong with this code?

string symbol1="XAUUSD";
string symbol2="EURUSD";

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);
        }
    }
};

ENUM_TIMEFRAMES TF_M1=PERIOD_M1;
CIsNewBar newBarOnXAUUSD(symbol1, TF_M1);
CIsNewBar newBarOnEURUSD(symbol2, TF_M1);

int OnInit() {
    Print("start new bar test:");
    return(INIT_SUCCEEDED);
}

void OnTick() {

    if(IsNewBar(symbol1, TF_M1))
        Print(TimeToString(TimeCurrent(),TIME_DATE|TIME_MINUTES|TIME_SECONDS)," 1 NewBar Func  ",symbol1);
    if(newBarOnXAUUSD.isNewBar())
        Print(TimeToString(TimeCurrent(),TIME_DATE|TIME_MINUTES|TIME_SECONDS)," 2 NewBar Class ",symbol1);
    if(IsNewBar(symbol2, TF_M1))
        Print(TimeToString(TimeCurrent(),TIME_DATE|TIME_MINUTES|TIME_SECONDS)," 3 NewBar Func  ",symbol2);
    if(newBarOnEURUSD.isNewBar())
        Print(TimeToString(TimeCurrent(),TIME_DATE|TIME_MINUTES|TIME_SECONDS)," 4 NewBar Class ",symbol2);
}


bool IsNewBar(string strSymbol, ENUM_TIMEFRAMES TF) {
    static datetime prevTime = iTime(strSymbol, TF, 0);
    return (prevTime != (prevTime = iTime(strSymbol, TF, 0)));
}


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.

QK      0       15:33:59.835    isNewBar (EURUSD,M1)    start new bar test:
LM      0       15:34:00.131    isNewBar (EURUSD,M1)    2022.01.05 09:34:00 3 NewBar Func  EURUSD
MF      0       15:34:00.131    isNewBar (EURUSD,M1)    2022.01.05 09:34:00 4 NewBar Class EURUSD
NL      0       15:34:00.431    isNewBar (EURUSD,M1)    2022.01.05 09:34:00 2 NewBar Class XAUUSD
PO      0       15:35:01.575    isNewBar (EURUSD,M1)    2022.01.05 09:35:01 1 NewBar Func  XAUUSD
EI      0       15:35:01.575    isNewBar (EURUSD,M1)    2022.01.05 09:35:01 2 NewBar Class XAUUSD
QO      0       15:35:01.575    isNewBar (EURUSD,M1)    2022.01.05 09:35:01 4 NewBar Class EURUSD
CH      0       15:36:00.398    isNewBar (EURUSD,M1)    2022.01.05 09:36:00 3 NewBar Func  EURUSD
FK      0       15:36:00.398    isNewBar (EURUSD,M1)    2022.01.05 09:36:00 4 NewBar Class EURUSD
KS      0       15:36:01.196    isNewBar (EURUSD,M1)    2022.01.05 09:36:01 1 NewBar Func  XAUUSD
CI      0       15:36:01.196    isNewBar (EURUSD,M1)    2022.01.05 09:36:01 3 NewBar Func  EURUSD
MG      0       15:36:01.400    isNewBar (EURUSD,M1)    2022.01.05 09:36:01 1 NewBar Func  XAUUSD
EM      0       15:36:01.400    isNewBar (EURUSD,M1)    2022.01.05 09:36:01 3 NewBar Func  EURUSD
HJ      0       15:36:02.605    isNewBar (EURUSD,M1)    2022.01.05 09:36:02 1 NewBar Func  XAUUSD
PP      0       15:36:02.605    isNewBar (EURUSD,M1)    2022.01.05 09:36:02 3 NewBar Func  EURUSD
PS      0       15:36:03.563    isNewBar (EURUSD,M1)    2022.01.05 09:36:03 2 NewBar Class XAUUSD
 

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

Universal Expert Advisor: the Event Model and Trading Strategy Prototype (Part 2)
Universal Expert Advisor: the Event Model and Trading Strategy Prototype (Part 2)
  • www.mql5.com
This article continues the series of publications on a universal Expert Advisor model. This part describes in detail the original event model based on centralized data processing, and considers the structure of the CStrategy base class of the engine.
 
  1.         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!

  2.     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++.

    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. 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:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum #2 (2013)

  3.     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)

  4.     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)

 
Carl Schreiber #:

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

 
William Roeder #:
  1. Use not equal. There was a post around 2002, where a server clock was mis-set and then corrected — stopped the EA for days!

  2. 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++.

    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. 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:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum #2 (2013)

  3. You assign to prevTime and then test if the result is not equal to prevTime. Always false.
              New candle - MQL4 programming forum #3 (2014)

  4. 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);
}