Download MetaTrader 5

strange datetime assignment

To add comments, please log in or register
Use MQL5 Cloud Network. It is secure!
Laurentiu Sorin Tene
692
Laurentiu Sorin Tene 2013.02.24 21:01 

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;
     }
  }
phi nuts
2184
phi nuts 2013.02.25 04:03  
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()
  {
Laurentiu Sorin Tene
692
Laurentiu Sorin Tene 2013.02.25 07:00  
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.

Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2013.02.25 12:25  
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 ?
Laurentiu Sorin Tene
692
Laurentiu Sorin Tene 2013.02.25 13:31  

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!

Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2013.02.25 13:42  
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 ?
Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2013.02.25 14:07  
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:
To add comments, please log in or register