我希望有人可以查看我的代码,并告诉我为什么它无法打开文件。 这在MQL4的软件meta-editor中。其他一切均正常运行。也没有错误。它根本不会打开文件。打开文件时,Handle的值为1。此功能在MQL5中可以正常工作,在MQL4中则不能。这是MQL4独有的问题,还是我的代码
//+------------------------------------------------------------------+
//| 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);
}
答案 0 :(得分:0)
File
函数在MQL4和MQL5中均相同。首先,您应该检查无法打开文件的原因(否则您编写时不会有任何错误)。
if(Handle==INVALID_HANDLE){
Alert("failed to open file. error=",GetLastError());
return;}
接下来,应使用扩展名传递文件名,否则在编辑器中打开它可能是一个问题。 字符串fileName =“ Indicator Output.txt”; int handle = FileOpen(fileName,FILE_WRITE | FILE_TXT);
主要原因可能是您打开了一个文件,却忘记了在处理代码时将其关闭。最简单的检查方法是关闭MT4,重新打开并再次尝试脚本。 MT4关闭时,所有由MT4文件打开的文件都会关闭。如果这样做没有帮助,请检查错误。
答案 1 :(得分:0)
以下是一些提示:
OnStart事件处理程序的类型为void。 void OnStart(){}
可以使用for循环来改善while循环
您要打开的文件为txt文件,没有适当的文件
延期。即.txt
,并且您正在使用FileWrite
用于csv文件。您可能需要看一下文档,然后打开
文件作为csv并按预期方式使用FileWrite自动插入
定界符。
您可以选择使用CFile对象来处理文件,因为它会自动关闭 文件。
这是重构后的外观。
#property strict
#include <Files\File.mqh>
#include <stdlib.mqh>
void OnStart()
{
CFile file;
file.Open("indicator_output.csv", FILE_WRITE|FILE_CSV, ',');
if(file.Handle() == INVALID_HANDLE){
Print("FileOpenError: ", ErrorDescription(_LastError));
return;
}
for(int i=0; i<100; i++){
double mom = iMomentum(NULL, 0, 14, 0, i); //calculated at closing
double sto = iStochastic(NULL, 0, 5, 3, 3, 0, 0, 0, i);
double sma = iMA(NULL, 0, 14, 0, 0, 0, i); //this is calculating at close
double mfi = iMFI(NULL, 0, 14, i);
double open = iOpen(NULL, 0, i);
double prev = iOpen(NULL, 0, i + 1);
uint row_bytes = FileWrite(file.Handle(),
mom >= 100 ? 1 : 0,
sto >= 50 ? 1 : 0,
sma > open ? 1 : 0,
mfi >= 50 ? 1 : 0,
open >= prev ? 1 : 0
);
if(row_bytes == 0){
Print("FileWriteError: ", ErrorDescription(_LastError));
break;
}
}
Alert(StringFormat("%s written with a size of %d bytes.",
file.FileName(),
file.Size()
));
}
答案 2 :(得分:0)
首先,检查您的文件是否存在。
//+------------------------------------------------------------------+
//| FileIsExist.mq4 |
//| Copyright 2019, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
string terminal_data_path=TerminalInfoString(TERMINAL_DATA_PATH);
string filename = "teste2.txt";
int fileHandle ;
if(FileIsExist(filename,0))
{
Print("Specified File Has");
fileHandle = FileOpen(filename , FILE_WRITE|FILE_TXT);
FileWriteString(fileHandle,"teste");
FileClose(fileHandle);
Print("Write to Existing File Completed");
}else
{
Print("File Not Available, Regenerating....." );
fileHandle = FileOpen(filename , FILE_READ|FILE_WRITE|FILE_TXT);
FileWriteString(fileHandle,"Writing to Newly Created File Completed - teste \n");
FileClose(fileHandle);
Print("Writing to Newly Created File Completed");
}
}
//+------------------------------------------------------------------+