Download MetaTrader 5

Function StrToTime causes error 4051

To add comments, please log in or register
Add our Facebook page to friends. Follow the latest news!
kl-mueller
168
kl-mueller 2014.02.12 18:56 

Hi,

the editor in build >600 doesn't like the function StrToTime.

Calling it causes the error 4051. What's wrong??

code is here:

//+------------------------------------------------------------------+
//| test.mq4 |
//+------------------------------------------------------------------+

#property version "1.00"

//--- input parameters
input string TimeClose_="21:45";
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---

//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---

}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
datetime xxxxx = StrToTime("21:45"); // makes no difference with TimeClose_ from the input
Print("error: "+GetLastError());
}
//+------------------------------------------------------------------+

The result with every tick:

What's wrong here. Compilation with editor 509 causes no error, of course

Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2014.02.12 18:59  
kla-mue:

Hi,

the editor in build >600 doesn't like the function StrToTime.

Calling it causes the error 4051. What's wrong??

code is here:

<deleted>

Please post your code using the SRC button
kl-mueller
168
kl-mueller 2014.02.12 19:04  

OK, now I know... next time...

do you have a solution for my problem?

//+------------------------------------------------------------------+
//|                                                         test.mq4 |
//+------------------------------------------------------------------+

#property version   "1.00"

//--- input parameters
input string   TimeClose_="21:45";
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   datetime xxxxx = StrToTime("21:45");
   Print("error: "+GetLastError());
  }
//+------------------------------------------------------------------+
whroeder1
14517
whroeder1 2014.02.12 19:35  
kla-mue: OK, now I know... next time...
  1. Next time edit your original post.
  2.    datetime xxxxx = StrToTime("21:45");
       Print("error: "+GetLastError());
    test EURUSD,H1: error: 4051
    Don't call GetLastError() unless you have an error. Is xxxx == 0? Why didn't you print it along with GLE?
  3. Perhaps you should try the new StringToTime
Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2014.02.12 19:54  
kla-mue:

OK, now I know... next time...

do you have a solution for my problem?

It might have been fixed . . . https://www.mql5.com/en/forum/149475 but please report it to the Service Desk anyway.

kl-mueller
168
kl-mueller 2014.02.12 20:06  
WHRoeder:
  1. Next time edit your original post.
  2. test EURUSD,H1: error: 4051
    Don't call GetLastError() unless you have an error. Is xxxx == 0? Why didn't you print it along with GLE?
  3. Perhaps you should try the new StringToTime



@2: how should i find out if there is an error, if i do not ask GLE?

This code is to find out, why my EA is giving error-messages. No other purpose.


@3: the StringToTime causes the same error :(


Is there an other possibility?

whroeder1
14517
whroeder1 2014.02.12 20:31  
kla-mue: @2: how should i find out if there is an error, if i do not ask GLE?

GLE does not tell you IF you have an error. It tells you what the error is WHEN you have one.

GLE returns the last error code. That code is associated with what code? Unknown. Could have been something associated with internal terminal processing or a call from a previous function you called.

Don't call GetLastError() unless you have an error
You have an error if OrderSend returns negative ticket, Time[i] returns zero, or if StringToTime returns zero, etc.
qjol
3243
qjol 2014.02.12 20:33  

StringToTime

The function converts a string containing time or date in "yyyy.mm.dd [hh:mi]" format into datetime type.

datetime StringToTime(
string value // date string
);

Parameters

value

[in] String in " yyyy.mm.dd hh:mi " format.

datetime xxxxx = StringToTime(D'21:45');
whroeder1
14517
whroeder1 2014.02.12 20:35  
qjol:
D'21:45'

That is not a string, that is a datetime already (21:45 of the day you compiled the code.)
Read the doc StrToTime()

  datetime var1;
  var1=StrToTime("2003.8.12 17:35");
  var1=StrToTime("17:35");      // returns the current date with the given time
  var1=StrToTime("2003.8.12");  // returns the date with the midnight time of "00:00"
qjol
3243
qjol 2014.02.12 20:44  
but u have to use "yyyy.mm.dd hh:mm" format otherwise u get error 4051
kl-mueller
168
kl-mueller 2014.02.12 21:00  

PERFECT ! Now its runs without an error =)

It must be the complete date-format!

TNX a lot!!

1234
To add comments, please log in or register