我得到了以下作为脚本运行的代码,但我遇到了中间有空白行的问题,如下所示:
我一直在试图找出但不确定为什么有空白行。任何帮助将不胜感激。这是代码:
#property script_show_inputs
input datetime StartDateTime = D'2023.11.21 00:00';
input datetime EndDateTime = D'2023.11.22 12:00';
// ------ OnInit() function ------
int OnInit()
{
string filename = "MyMT4Data.csv";
int file_handle = FileOpen(filename, FILE_WRITE | FILE_CSV | FILE_ANSI);
if (file_handle == INVALID_HANDLE) {
Print("Failed to open file: ", filename);
return(INIT_FAILED);
}
// Loop through bars in reverse order, and filter by StartDateTime and EndDateTime
for (int i = Bars - 1; i >= 0; i--) {
datetime barTime = Time[i];
if (barTime >= StartDateTime && barTime <= EndDateTime) {
// Build CSV line
// Explicit Line Initialization (Safeguard)
string line ="";
line = TimeToString(barTime, TIME_DATE | TIME_SECONDS) + "," +
DoubleToString(Open[i], Digits) + "," +
DoubleToString(High[i], Digits) + "," +
DoubleToString(Low[i], Digits) + "," +
DoubleToString(Close[i], Digits) + "\n";
// Modify line to get four-digit year
line = StringSubstr(line, 0, 8) + StringSubstr(line, 11);
// Remove any leading spaces (if needed)
line = StringTrimLeft(line);
// Write to file
FileWrite(file_handle, line);
line ="";
}
}
FileClose(file_handle);
return(INIT_SUCCEEDED);
}
我尝试打印调试很多次,但仍然出现空白行
我假设,由于您单独声明每一行并将其添加到 CSV 中,因此在添加
\n (new line)
后,这些行不需要 Close
字符。
删除那个
\n
,我想你会没事的。