strange datetime assignment

 

So I have a strange problem. I tried to verify NewBar appearance with the following code from Samuels article "Step-By-Step Guide to writing an Expert Advisor in MQL5 for Beginners". I tried the code on System Tester and I saw that is no trade. Than I saw that the Old_Time variable take the value of New_Time variable without any code.

I mean that Old_Time=New_Time but IsNewBar is never equal to true.

I do not know why Old_Time is equal to New_Time[0] without execution of the  if(Old_Time!=New_Time[0])  loop.
.

I hope you understand what I mean. 

Sorry for my bad english. 

void OnTick()
  {
   static datetime Old_Time=D'1970.01.01 00:00';
   datetime New_Time[1];
   bool IsNewBar=false;

// copying the last bar time to the element New_Time[0]
   int copied=CopyTime(_Symbol,_Period,0,1,New_Time);
   if(copied>0) // ok, the data has been copied successfully
     {
      if(Old_Time!=New_Time[0]) // if old time isn't equal to new bar time
        {
         IsNewBar=true;   // if it isn't a first call, the new bar has appeared
         if(MQL5InfoInteger(MQL5_DEBUGGING)) Print("We have new bar here ",New_Time[0]," old time was ",Old_Time);
         Old_Time=New_Time[0];            // saving bar time
        }
       else
        Alert("OT=NT ---> Old_Time=",Old_Time," New_Time=",New_Time[0]);
     }
   else
     {
      Alert("Error in copying historical times data, error =",GetLastError());
      ResetLastError();
      return;
     }

//--- EA should only check for new trade if we have a new bar
   if(IsNewBar==false)
     {
      Alert("RETURN");
      return;
     }
  }
 
tenlau:

So I have a strange problem. I tried to verify NewBar appearance with the following code from Samuels article "Step-By-Step Guide to writing an Expert Advisor in MQL5 for Beginners". I tried the code on System Tester and I saw that is no trade. Than I saw that the Old_Time variable take the value of New_Time variable without any code.

I mean that Old_Time=New_Time but IsNewBar is never equal to true.

I do not know why Old_Time is equal to New_Time[0] without execution of the  if(Old_Time!=New_Time[0])  loop.
.

I hope you understand what I mean. 

Sorry for my bad english. 

Variable IsNewBar is declared locally, and so it will initiated on every tick with false value. To solve this, declare variable IsNewBar as statically declared local variable or globally declared variable. 

Please read MQL5 doc about variable especially this Visibility Scope and Lifetime of Variables

Here's how to declare as statically declared local variable

void OnTick()
  {
   
   static bool IsNewBar=false;

Here's the global one

bool IsNewBar=false;

void OnTick()
  {
 
phi.nuts:

Variable IsNewBar is declared locally, and so it will initiated on every tick with false value. To solve this, declare variable IsNewBar as statically declared local variable or globally declared variable. 

Please read MQL5 doc about variable especially this Visibility Scope and Lifetime of Variables

Here's how to declare as statically declared local variable

Here's the global one

Thank ypu phi.nuts!

So to answer myself to my question: The if loop( ........) is executed but the variable IsNewBar do not change its value do to its every tick "false" initialization.

 
phi.nuts:

Variable IsNewBar is declared locally, and so it will initiated on every tick with false value. To solve this, declare variable IsNewBar as statically declared local variable or globally declared variable. 

That means once set to true it will always be true and that defeats the aim,  doesn't it ?
 

RaptorUK:
 That means once set to true it will always be true and that defeats the aim,  doesn't it ? 

 

I think that if it is declared as "static variable" its value will be kept on the next function call.

So on first call isNewBar is initialized to "false", than if the "if loop" is "true" will be changed to "true", than on the next call of OnTick() the value remain "true". 

Correct me please if I am wrong!

 
tenlau:

I think that if it is declared as "static variable" its value will be kept on the next function call.

So on first call isNewBar is initialized to "false", than if the "if loop" is "true" will be changed to "true", than on the next call of OnTick() the value remain "true". 

Correct me please if I am wrong!

You are indeed correct,  but it will always be true,  don't you only want it to be true when you have a new bar ?  and the rest of the time want it to be false ?
 
tenlau:

So I have a strange problem. I tried to verify NewBar appearance with the following code from Samuels article "Step-By-Step Guide to writing an Expert Advisor in MQL5 for Beginners". I tried the code on System Tester and I saw that is no trade. Than I saw that the Old_Time variable take the value of New_Time variable without any code.

I mean that Old_Time=New_Time but IsNewBar is never equal to true.

I do not know why Old_Time is equal to New_Time[0] without execution of the  if(Old_Time!=New_Time[0])  loop.

The loop did execute,  your code is good, try the attached.

 

 

Files:
Reason: