How to detect a new bar - page 3

 
Stephen Njuki:
I use this...

Thanks man

 
Stephen Njuki:
I use this...

I had many problems using time as a variable and I was looking for a way to use variables bars


thank you, work!


 
Mladen Rakic:

Why not simply using something like this :

   static datetime prevTime=0;
          datetime lastTime[1];
          if (CopyTime(_Symbol,_Period,0,1,lastTime)==1 && prevTime!=lastTime[0])
          {
               prevTime=lastTime[0];
              
               // ...
          }
It should work in all cases (even when there is an error with CopyTime() it avoids a trap)

thank you bro!

 
Mladen Rakic:

Why not simply using something like this :

   static datetime prevTime=0;
          datetime lastTime[1];
          if (CopyTime(_Symbol,_Period,0,1,lastTime)==1 && prevTime!=lastTime[0])
          {
               prevTime=lastTime[0];
              
               // ...
          }
It should work in all cases (even when there is an error with CopyTime() it avoids a trap)

Perfect.

I was looking for the same thing.


God bless you

 

Thanks Leonard, really helpful.

Noticed a few issues when working with iHigh, iLow and iClose directy after so added a quick Sleep(10000) after calling.

void OnTick()
   {
    if(isNewBar())
       {
       Sleep(10000);
       ...
       }
   }

bool isNewBar()
   {
   
    static datetime prevTime = 0;
    datetime lastTime[1];
    if(CopyTime(Symbol(), Period(), 0, 1, lastTime) == 1 && prevTime != lastTime[0])
       {
        prevTime = lastTime[0];
        return(true);
       }
    return(false);
   }
 

I wrote this function, and I usually use it in optimization mood which can make a real difference if you want to call it a million times. I think this is as fast as you can get the new bar, and it's looks clean:

inline bool IsNewBar(ENUM_TIMEFRAMES timeframe = PERIOD_CURRENT)
{
   static datetime lastBar;
   return lastBar != (lastBar = iTime(_Symbol, timeframe, 0));
}
 
Null_Pointer:

I wrote this function, and I usually use it in optimization mood which can make a real difference if you want to call it a million times. I think this is as fast as you can get the new bar, and it's looks clean:

Thanks!

Question, what means "Inline"?

 
fdesu: Question, what means "Inline"?

In "C" and "C++", the "inline" keyword would recommend to the compiler to use inline expansion for optimisation.

However, in MQL, no such feature is documented even though the compiler accepts it and does not give an error during compilation. It may simple just be tolerated but have no effect.

 
fdesu:

Thanks!

Question, what means "Inline"?

12. MQL5: Added support for the inline, __inline and __forceinline specifiers when parsing code. The presence of the specifiers in the code causes no errors and does not affect the compilation. At the moment, this feature simplifies transferring С++ code to MQL5.
Find more information about specifiers in MSDN.

New MetaTrader 5 platform build 1930: Floating window charts and .Net libraries in MQL5
New MetaTrader 5 platform build 1930: Floating window charts and .Net libraries in MQL5
  • 2018.10.25
  • www.mql5.com
The updated version of the MetaTrader 5 platform will be released on October 26, 2018...
 
Alain Verleyen: 12. MQL5: Added support for the inline, __inline and __forceinline specifiers when parsing code. The presence of the specifiers in the code causes no errors and does not affect the compilation. At the moment, this feature simplifies transferring С++ code to MQL5. Find more information about specifiers in MSDN.
Thanks for the information! Pity they have not included that in the documentation, as I could find no mention of it in neither the online version nor in the MetaEditor help file.