Download MetaTrader 5

Date as Variable compared with TimeCurrent OnTick

To add comments, please log in or register
Eduardo Aguirre
77
Eduardo Aguirre  

Hello everyone! This is my very first post and I could find an specific forum for discussing coding on mql5, just this EA subforum so sorry for any inconvenience.

I am new to MQL5 and I am trying to do a simple EA which asks for a Date as a string in " yyyy.mm.dd hh:mi " format. Then it converts the variable to datetime and compares it with current time OnTick like this:

//+------------------------------------------------------------------+
//|                                             FundamentalDates.mq4 |
//|                         Copyright 2016, Eduardo Aguirre Quintana |
//|                                                 www.windfury.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Eduardo Aguirre Quintana"
#property link      "www.windfury.net"
#property version   "1.00"
#property strict
extern string DateNFA = "2016.01.11 23:00";//[in]  String in " yyyy.mm.dd hh:mi " format.
extern int StopLoss = 10;

 
 datetime  StringToTime( 
    string  DateNFA      // date string 
   );



void OnTick()
  {
  
  

  if (DateNFA == TimeCurrent());
  {
  Alert("Same Hour TimeCurrent: ", TimeCurrent());
  Alert("Same Hour DateNFA: ", DateNFA);
  Sleep(7000);
  }
  
  
   
  }

 But whenever I put the EA on a chart, the  if (DateNFA == TimeCurrent()) comparison seemS to be always true which is imposible. Even If I change it to the opposite case if (DateNFA != TimeCurrent()) i get the exact same Alert:

 

 Any suggestions?

Alain Verleyen
Moderator
33598
Alain Verleyen  

Remove the ;

if (DateNFA == TimeCurrent());
Eduardo Aguirre
77
Eduardo Aguirre  

Oh I see thanks however now that I just fixed that part it seems that the "if" statment is never acomplished so now the Alert is never displayed. Does anyone know why this is happening?

Or whats the best way to compare a time specified by the user and the current time OnTick?

SearchSurf
453
SearchSurf  
Eduardo Aguirre:

Oh I see thanks however now that I just fixed that part it seems that the "if" statment is never acomplished so now the Alert is never displayed. Does anyone know why this is happening?

Or whats the best way to compare a time specified by the user and the current time OnTick?

You're comparing it on Ontick basis, if an exact specific time comparison elapsed before execution of a tick, the statement will never be accomplished, and not sure about the sleep thing but for what I know it's a pause which contributes again on the delay to capture that time comparing statement.

...also you might want to convert the TimeCurrent() to string other than DateNFA to Time to get up to the minutes only, since in your declaration on the DateNFA, the time got by up to minutes only, I think TimeCurrent() fully uses up to it's seconds in which if in string like comparison is concern it will always fall to false between the two.

Check the article here too: https://www.mql5.com/en/articles/599

MQL5 Programming Basics: Time
MQL5 Programming Basics: Time
  • 2013.04.26
  • Dmitry Fedoseev
  • www.mql5.com
The article focuses on standard MQL5 functions for working with time, as well as programming techniques and practically useful functions for working with time that are required when creating Expert Advisors and indicators. Particular attention is paid to the general theory of time measurement. This article should be of interest primarily to novice MQL5 programmers.
Eduardo Aguirre
77
Eduardo Aguirre  
SearchSurf:

You're comparing it on Ontick basis, if an exact specific time comparison elapsed before execution of a tick, the statement will never be accomplished, and not sure about the sleep thing but for what I know it's a pause which contributes again on the delay to capture that time comparing statement.

...also you might want to convert the TimeCurrent() to string other than DateNFA to Time to get up to the minutes only, since in your declaration on the DateNFA, the time got by up to minutes only, I think TimeCurrent() fully uses up to it's seconds in which if in string like comparison is concern it will always fall to false between the two.

Check the article here too: https://www.mql5.com/en/articles/599

Thank you very much SearchSurf, at the end the problem was comparing DateNFA (time with only minutes) to TimeCurrent() (time with seconds) so it was always false as you mentioned. I ended up converting TimeCurrent() to string as you said and leaving DateNFA as a string variable. Also the article was really helpful! :D 

SearchSurf
453
SearchSurf  
Eduardo Aguirre:

Thank you very much SearchSurf, at the end the problem was comparing DateNFA (time with only minutes) to TimeCurrent() (time with seconds) so it was always false as you mentioned. I ended up converting TimeCurrent() to string as you said and leaving DateNFA as a string variable. Also the article was really helpful! :D 


... still watch out for the execution , since it's ontick if and no tick occurs by minute/s it won't detect. 

To add comments, please log in or register