FileReadDatetime ignoring minutes and seconds if a second field is present in the CSV file (SOLVED)
int file_handle = FileOpen(DirectoryPath+"\\"+FileName,FILE_READ|FILE_CSV);
-
You didn't specify the separator.
-
You didn't specify the file being characters or wide.
-
We have no idea that DirectoryPath is. All file reference is relative to the sandbox.
File Write Problem (Error: 5002) - Expert Advisors and Automated Trading - MQL5 programming forum #1-2 (2020)
and FolderDelete using TERMINAL_DATA_PATH - General - MQL5 programming forum (2017) -
Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
What are Function return values ? How do I use them ? - MQL4 programming forum (2012)
-
You didn't specify the separator.
-
You didn't specify the file being characters or wide.
-
We have no idea that DirectoryPath is. All file reference is relative to the sandbox.
File Write Problem (Error: 5002) - Expert Advisors and Automated Trading - MQL5 programming forum #1-2 (2020)
and FolderDelete using TERMINAL_DATA_PATH - General - MQL5 programming forum (2017) -
Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
What are Function return values ? How do I use them ? - MQL4 programming forum (2012)
Thanks for your reply. For some reason I was not notified by email despite the setting being enabled. My spam folder is empty.
1. I don't need to. If the csv-file delimiter is not specified, the default delimiter is ";". I tried by explicitly setting the delimiter and it doesn't work.
2. You mean like FILE_UNICODE or FILE_ANSI? I tried with FILE_UNICODE or FILE_ANSI without success.
3 DirectoryPath is just a sub-directory of \MQL4\Files\
4. I tried with and without #property strict. I get no error or useful warning.
Using MT5 build 3644 (20 Mar 2023), I got same bug.
Please try following code to reconfirm:
//+------------------------------------------------------------------+ //| testdatetime.mq5 | //| Copyright 2023, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2023, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" #property strict //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- datetime d; d = TimeLocal(); PrintFormat("%u", d); Print(TimeToString(d,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); int file_handle = FileOpen("data.csv",FILE_READ|FILE_CSV,';'); while(!FileIsEnding(file_handle)) { d=FileReadDatetime(file_handle); PrintFormat("%u", d); Print(TimeToString(d,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); } } //+------------------------------------------------------------------+
File data.csv
2023.03.24 07:24:05;123.4;124.3
int file_handle = FileOpen (FileName, FILE_READ | FILE_ANSI | FILE_CSV , ";" );
Simply try the code and the CSV I posted (remember to add the FILE_ANSI flag).
Your code display the result __not__ immediately after reading the datetime from CSV.
(inside the while loop)
//+------------------------------------------------------------------+ //| datetime.mq4 | //| | //| | //+------------------------------------------------------------------+ #property copyright "" #property link "" #property version "1.00" #property strict //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { datetime d; int file_handle = FileOpen("data.csv",FILE_READ|FILE_CSV,';'); while(!FileIsEnding(file_handle)) { d=FileReadDatetime(file_handle); Print(TimeToString(d,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); } }
Inside the file is this:
2023.03.24 04:16:00;-0.591266393661499
The output gives me both the good and the bad:
2023.03.24 04:16:00 2023.03.23 00:00:00
while(!FileIsEnding(file_handle)) { d=FileReadDatetime(file_handle); .... }
Observe that code snippet above closely...
inside the loop, you're reading "Datetime field" until end of file.
That's why you get: both the good and the bad
Try following instead:
//+------------------------------------------------------------------+ //| datetime.mq4 | //| | //| | //+------------------------------------------------------------------+ #property copyright "" #property link "" #property version "1.00" #property strict //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { datetime d; int file_handle = FileOpen("data.csv",FILE_READ|FILE_CSV,';'); if(!FileIsEnding(file_handle)) { d=FileReadDatetime(file_handle); Print(TimeToString(d,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); } }
- meta_trader2352345 #: Inside the file is this:
2023.03.24 04:16:00;-0.591266393661499
Your file has a datetime and a double, read what the file has.
-
Make your code self documenting.
struct myData{ datetime when; double price; void read(int handle){ when = FileReadDatetime(handle); price = FileReadDouble(handle); } }
-
You don't close the file.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I read a datetime string from a CSV formatted file with the proper extention. Let's say the datetime is 2023.03.23 20:15:00.
When I read the datetime with FileReadDatetime, the value returned is 2023.03.23 00:00:00 instead. The minutes and seconds are ignored.
The problem doesn't occur if the row only contains the datetime and nothing else. Not even a semi-colon:
2023.03.23 21:15:00; some value
2023.03.23 21:15:00;
2023.03.23 21:15:00
I have tried surrounding the datetime string by double quotes, to no avail.
FileReadDatetime
Reads from the file of CSV type a string of one of the formats: "YYYY.MM.DD HH:MI:SS", "YYYY.MM.DD" or "HH:MI:SS" - and converts it into a value of datetime type.
Do you understand what I don't ?