New candle fonction base on iTme

 

hi everyone, hope you're well

I found this function on this video here from @ Tobias Christian Witzigmann, can you confirm that the variable previousTime is read only once with al static function ?

bool IsNewBar(){
    static datetime previousTime = 0;       
    datetime currentTime = iTime(_Symbol, PERIOD_CURRENT, 0);
    if(previousTime != currentTime){
        previousTime = currentTime;
        return true;
    }
    return false;
}

I thought I understood that from the MQL5 code documentation, but I'd rather confirm it. or am I still missing the point?

Best Reguards,
ZeroCafeine

Tobias Christian Witzigmann
Tobias Christian Witzigmann
  • 2023.02.07
  • www.mql5.com
Trader's profile
 

No, "static" does not mean read-only. It means that it retains its value even after returning from the function.

Also, instead of using a function please also see the following ...

Code Base

Detecting the start of a new bar or candle

Fernando Carreiro, 2022.04.24 00:38

Detecting the start of a new bar or candle, in the OnTick() event handler of an expert advisor.

Documentation on MQL5: Language Basics / Variables / Static Variables
Documentation on MQL5: Language Basics / Variables / Static Variables
  • www.mql5.com
Static Variables - Variables - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Your topic has been moved to the section: Expert Advisors and Automated Trading — In the future, please consider which section is most appropriate for your query.
MQL5 forum: Expert Advisors and Automated Trading
MQL5 forum: Expert Advisors and Automated Trading
  • www.mql5.com
How to create an Expert Advisor (a trading robot) for Forex trading
 

thank you for your quick reply and the link,

before discovering your code, which looks quite complete after a quick glance. 

why, when the function is called a 2nd time, the variable previousTime is not initiated to 0 ?  is it because it already exists and its value is greater than 0? or am I still missing the point ?

if the answer is in your link don't bother replying and thanks again.🙂

 
ZeroCafeine #: thank you for your quick reply and the link, before discovering your code, which looks quite complete after a quick glance. why, when the function is called a 2nd time, the variable previousTime is not initiated to 0 ?  is it because it already exists and its value is greater than 0? or am I still missing the point ?if the answer is in your link don't bother replying and thanks again.🙂

Because it is "static". As I state in my previous post, it retains its value. It does not lose its value when you return from a function, no matter how many times the function is called.

"Static variables exist from the moment of program execution and are initialized only once before the specialized functions OnInit() is called. If the initial values are not specified, variables of the static storage class are taking zero initial values.

Local variables declared with the static keyword retain their values throughout the function lifetime. With each next function call, such local variables contain the values that they had during the previous call. Any variables in a block, except formal parameters of a function, can be defined as static. If a variable declared on a local level is not a static one, memory for such a variable is allocated automatically at a program stack."

 
Merci @Fernando Carreiro, tks you 🙂
Fernando Carreiro
Fernando Carreiro
  • 2023.04.19
  • www.mql5.com
Trader's profile