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 without execution of the if(Old_Time!=New_Time) loop..
I hope you understand what I mean.
Sorry for my bad english.
static datetime Old_Time=D'1970.01.01 00:00';
// copying the last bar time to the element New_Time
if(copied>0) // ok, the data has been copied successfully
if(Old_Time!=New_Time) // 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," old time was ",Old_Time);
Old_Time=New_Time; // saving bar time
Alert("OT=NT ---> Old_Time=",Old_Time," New_Time=",New_Time);
Alert("Error in copying historical times data, error =",GetLastError());
//--- EA should only check for new trade if we have a new bar
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
static bool IsNewBar=false;
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.
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!
I do not know why Old_Time is equal to New_Time without execution of the if(Old_Time!=New_Time) loop.
The loop did execute, your code is good, try the attached.