文件不会在 MQL4 中打开
File will not open in MQL4
我希望有人可以查看我的代码并告诉我为什么它无法打开文件。
这是在元编辑器中,MQL4 的软件。其他一切正常运行。也没有给出错误。它根本不会打开文件。打开文件时,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);
}
File
函数在 MQL4 和 MQL5 中的工作方式相同。首先,你应该检查一下你无法打开文件的原因(否则你写的时候不会有任何错误)。
if(Handle==INVALID_HANDLE){
Alert("failed to open file. error=",GetLastError());
return;}
接下来,文件名要带扩展名,否则用编辑器打开可能会出问题。
字符串文件名="Indicator Output.txt";
int handle=FileOpen(文件名, FILE_WRITE|FILE_TXT);
主要原因可能是您在处理代码时打开了一个文件而忘记关闭它。最简单的检查方法是关闭 MT4,重新打开并再次尝试脚本。所有通过MT4打开的文件在MT4关闭时关闭。如果这没有帮助 - 检查错误。
这里有一些提示:
OnStart 事件处理程序的类型为 void。 void OnStart(){}
while循环可以用for循环改进
您在没有合适的文件的情况下将文件打开为 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()
));
}
首先,检查您的文件是否存在。
//+------------------------------------------------------------------+
//| 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");
}
}
//+------------------------------------------------------------------+
我希望有人可以查看我的代码并告诉我为什么它无法打开文件。 这是在元编辑器中,MQL4 的软件。其他一切正常运行。也没有给出错误。它根本不会打开文件。打开文件时,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);
}
File
函数在 MQL4 和 MQL5 中的工作方式相同。首先,你应该检查一下你无法打开文件的原因(否则你写的时候不会有任何错误)。
if(Handle==INVALID_HANDLE){
Alert("failed to open file. error=",GetLastError());
return;}
接下来,文件名要带扩展名,否则用编辑器打开可能会出问题。 字符串文件名="Indicator Output.txt"; int handle=FileOpen(文件名, FILE_WRITE|FILE_TXT);
主要原因可能是您在处理代码时打开了一个文件而忘记关闭它。最简单的检查方法是关闭 MT4,重新打开并再次尝试脚本。所有通过MT4打开的文件在MT4关闭时关闭。如果这没有帮助 - 检查错误。
这里有一些提示:
OnStart 事件处理程序的类型为 void。
void OnStart(){}
while循环可以用for循环改进
您在没有合适的文件的情况下将文件打开为 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()
));
}
首先,检查您的文件是否存在。
//+------------------------------------------------------------------+
//| 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");
}
}
//+------------------------------------------------------------------+