When i run FileOpen from within int OnInit(), then the file opens perfectly.
When i do the same from Ontimer() then it fails with INVALID_HANDLE ?
this is the function which is called each day from Ontimer(without success) or OnInit (with success)
In InitCSVFile i open the file like this :this works
this does not work
I can't see why this is different? I'm also sure not other thread or program had a lock on the file
is the file closed after each open ? or does it try to reopen it while its still open ?
Your code is incomplete for us to analyse properly!
However, you seem to be assigning the file handle to a local variable in the "InitCSVFile" function, so how are you accessing the file (and closing it later) in the other functions, if the handle is local?
Also, if the file is already open in the "OnInit", then trying to re-open it again in "OnTimer" is probably going to fail (I've never tried to open a file multiple times in MQL before).
Save the handle in a globally scoped variable and access it from other functions, including the "OnTimer". That way, it does not need to be "reopened".
Alternatively define a class, and save the handle in a member variable.
is the file closed after each open ? or does it try to reopen it while its still open ?
bool InitCSVFile() { long lastcheckedweek; string ls_string; string tmp_string[]; ushort lu_char; int li_Filehandle = FileOpen("stock.csv",FILE_READ|FILE_CSV|FILE_ANSI); if(li_Filehandle!=INVALID_HANDLE) { total_lines = 0; while(!FileIsEnding(li_Filehandle)) { ls_string = FileReadString(li_Filehandle); CsvFulline[total_lines] = ls_string+"\n"; if(total_lines>=1){ if(ls_string.Length() !=0){ lu_char = StringGetCharacter(";",0); int li_str = StringSplit(ls_string,lu_char,tmp_string); if(li_str > 4) return false; CsvSymb[total_lines-1] = tmp_string[0]; CsvValue[total_lines-1] = tmp_string[1]; CsvSaved[total_lines-1] = tmp_string[2]; } } else{ // get last checked freq_week lu_char = StringGetCharacter(";",0); int li_str = StringSplit(ls_string,lu_char,tmp_string); if(li_str > 4) return false; lastcheckedweek = StringToInteger(tmp_string[3]); if((current_weeknumber-lastcheckedweek) <= freq_week) { FileClose(li_Filehandle); return false; } } total_lines++; } } FileClose(li_Filehandle); return true; }
yes i close it
Your code is incomplete for us to analyse properly!
However, you seem to be assigning the file handle to a local variable in the "InitCSVFile" function, so how are you accessing the file (and closing it later) in the other functions, if the handle is local?
Also, if the file is already open in the "OnInit", then trying ti re-open it again in "OnTimer" is probably going to fail (I've never tried to open a file multiple times in MQL before).
Save the handle in a globally scoped variable and access it from other functions, including the "OnTimer". That way, it does not need to be "reopened".
Alternatively define a class, and save the handle in a member variable.
I open, do some work and close at the end at the function.
I've checked for all function exits , the file should be properly closed.
I did what you proposed by making the file handler global, but the very first time i call FileOpen after starting the program, the function already return invalid.
I discovered when i change this OnInit
EventSetTimer(5); // every 5 seconds instead of //EventSetTimer(60*60*24); // every day
Then the file opens well ??? What can this delay have to do with the file opening?
As you correctly close it in one instance where you return :
if((current_weeknumber-lastcheckedweek) <= freq_week) { FileClose(li_Filehandle); return false; }
you have to also close it at the other returns :
if(li_str > 4) return false; if(li_str > 4) return false;
that is it .
The daily timer probably was a random occurence as it happens with errors some times
As you correctly close it in one instance where you return :
you have to also close it at the other returns :
that is it .
The daily timer probably was a random occurence as it happens with errors some times
My function was faulty but that was not the problem.
I discovered i cannot use FileOpen when i run the debugger on historical data. When i run the debugger on current data, then it works fine.
Then after searching the net, i found that the files need to be on a different place when using the history tester , which is strange too me . But now it works.
i copied all files to "Agent-127.0.0.1-3001\MQL5\Files"
My function was faulty but that was not the problem.
I discovered i cannot use FileOpen when i run the debugger on historical data. When i run the debugger on current data, then it works fine.
Then after searching the net, i found that the files need to be on a different place when using the history tester , which is strange too me . But now it works.
i copied all files to "Agent-127.0.0.1-3001\MQL5\Files"
Good to know .
Its not that strange though , imagine an EA continuing live from where the tester left of .

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
When i run FileOpen from within int OnInit(), then the file opens perfectly.
When i do the same from Ontimer() then it fails with INVALID_HANDLE ?
this is the function which is called each day from Ontimer(without success) or OnInit (with success)
In InitCSVFile i open the file like this :this works
this does not work
I can't see why this is different? I'm also sure not other thread or program had a lock on the file