Run my EA only once at every new 1min candle opening

 

Hello,


When I test my EA with the "1 min OHLC", I get excellent result, but when I test it with "every tick based on real tick", I get awful result.

I launched my EA in a real account and the "every tick based on real tick", seem to give accurate result when I compare the result from the test and the trade that my EA actually took in real.


So, I would like to add a code in my EA to let it run only one time on every new 1min candle (that's how the "1min OHLC" works, if I understood it correctly). 


Any idea to code something like that?


I tried that, but doesn't seem to do the job :

int new1mincandle = iBarShift(_Symbol,PERIOD_M1,TimeCurrent(),true);
   
if(new1mincandle != -1) // There is a new 1min candle
        {

        // my code

        }


I tried that also, but not working as well :

datetime new1mincandle = iTime(_Symbol,PERIOD_M1,0);
   
if(new1mincandle + 5000 >= TimeCurrent()) // There less than 5sec since the last 1min candle opening
        {
        //my code
        }


My goal is to have a similar result in the  "every tick based on real tick" tester, than I have in the  "1 min OHLC" tester.

Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Account Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Account Properties
  • www.mql5.com
Account Properties - Environment State - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

You can't know when a candle closes. Only when a new tick arrives that starts a new bar is the old bar closed.

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.
          MT4: New candle - MQL4 programming forum #3 (2014)
          MT5: Accessing variables - MQL4 programming forum #3 (2022)

I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
          Running EA once at the start of each bar - MQL4 programming forum (2011)

 
William Roeder #:

You can't know when a candle closes. Only when a new tick arrives that starts a new bar is the old bar closed.

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.
          MT4: New candle - MQL4 programming forum #3 (2014)
          MT5: Accessing variables - MQL4 programming forum #3 (2022)

I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
          Running EA once at the start of each bar - MQL4 programming forum (2011)


I try with this code (from your link) : 


void OnTick(void)
{
   // Detect new bar condition
      static datetime dtBarCurrent  = WRONG_VALUE;
             datetime dtBarPrevious = dtBarCurrent;
                      dtBarCurrent  = (datetime) SeriesInfoInteger( _Symbol, _Period, SERIES_LASTBAR_DATE );
             bool     bBarNew       = ( dtBarCurrent != dtBarPrevious );

   // Execute only on a new bar condition
      if( bBarNew )
      {
         // Do something here
      };
};


Doesn't working. What do I miss?

 
CrokCrypto #: Doesn't working. What do I miss?
  1. “Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
         How To Ask Questions The Smart Way. (2004)
              When asking about code
              Be precise and informative about your problem

  2. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

 
William Roeder #:

You can't know when a candle closes. Only when a new tick arrives that starts a new bar is the old bar closed.

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.
          MT4: New candle - MQL4 programming forum #3 (2014)
          MT5: Accessing variables - MQL4 programming forum #3 (2022)

I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
          Running EA once at the start of each bar - MQL4 programming forum (2011)


I edited the code I wrote before, and that seem to work, the EA run at the start of new 1 min candle and take the trade if the conditions are met, BUT I still get very bad result compared to the test with "1 min OHLC", could I know exactly how the "1mon OHLC" working for run the EA during the test? I want to get the same result with the real tick…

 
William Roeder #:
  1. “Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
         How To Ask Questions The Smart Way. (2004)
              When asking about code
              Be precise and informative about your problem

  2. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)


I got it working : it seems to trade only at the beginning of new 1min candle, but the result is far from what I get from "1min OHLC" test. How the "1min OHLC" get the result that he show me?

 

Do not double post!!!

I have deleted your duplicated topic.