I was hoping someone could look at my code and tell me why it won't open a file. This is in meta-editor, the software for MQL4. Everything else runs correctly. Also no errors are given. It simply will not open a file. The value of Handle is one when a file is opened. This function works fine in MQL5, not in MQL4. Is this an issue with MQL4 exclusively or is it something with my code
//+------------------------------------------------------------------+
//| DailyReport.mq4 |
//| Copyright 2018, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
int OnStart()
{
int Handle;
Handle = FileOpen("Indicator Output", FILE_WRITE|FILE_TXT);
if(Handle == INVALID_HANDLE){
Alert("Error while opening file");
return(-1);
}
int count = 0;
int end_value = count + 100;
double open_value;
while (count < end_value){
string string1, string2, string3, string4, string5, final_string;
double values[5];
values[0] = iMomentum(0, 0, 14, 0, count); //calculated at closing
values[1] = iStochastic(0, 0, 5, 3, 3, 0, 0, 0, count);
values[2] = iMA(0,0, 14, 0, 0, 0, count); //this is calculating at close
values[3] = iMFI(0, 0, 14,count);
values[4] = iOpen(0, 0, count);
open_value = iOpen(0, 0, count + 1);
if (values[0] >= 100){
values[0] = 1;
}
else{
values[0] = 0;
}
if (values[1] >= 50){
values[1] = 1;
}
else{
values[1] = 0;
}
if (values[2] >= values[4]){
values[2] = 1;
}
else{
values[2] = 0;
}
if (values[3] >= 50){
values[3] = 1;
}
else{
values[3] = 0;
}
if (values[4] >= open_value){
values[4] = 1;
}
else{
values[4] = 0;
}
string1 = IntegerToString(values[0], 1, " ");
string2 = IntegerToString(values[1], 1, " ");
string3 = IntegerToString(values[2], 1, " ");
string4 = IntegerToString(values[3], 1, " ");
string5 = IntegerToString(values[4], 1, " ");
final_string = string1 + " " + string2 + " " + string3 + " " + string4 + " " + string5;
printf(final_string);
FileWrite(Handle, final_string);
count = count + 1;
}
FileClose(Handle);
return(1);
}
File
functions work same in both MQL4 and MQL5. First, you should check the reason why you cannot open a file (otherwise you wont have any error as you wrote).
if(Handle==INVALID_HANDLE){
Alert("failed to open file. error=",GetLastError());
return;}
Next, file name should be passed with extention, otherwise it might be a problem to open it in editor. string fileName="Indicator Output.txt"; int handle=FileOpen(fileName, FILE_WRITE|FILE_TXT);
The main reason might be that you opened a file and forget to close it while working on your code. The easiest way to check that is to close MT4, reopen and try the script again. All opened by MT4 files are closed when MT4 shutdown. If that does not help - check the error.