以下MQL4
脚本将MetaTrader中的数据导出到csv
文件。不幸的是(至少对我而言),生成的csv
文件中的数据顺序从0到1000,0是最新的(存在到过去)。我希望文件从1000到0(过去到现在)排序。
我将下面的写入数据循环更改为:for (int bar=Export_Bars; bar==0 bar--)
,但这只是生成了一个空的csv
文件。
#property script_show_inputs
input string Export_FileName = "data\\data.csv";
input int Export_Bars = 20000;
input int StartHour = 10;
input int EndHour = 19;
void OnStart()
{
int file = FileOpen(Export_FileName, FILE_WRITE|FILE_CSV|FILE_ANSI, ',');
if (file != INVALID_HANDLE && (Hour() >= StartHour) && Hour() < EndHour)
{
// Write the header of data
string row="";
for (int i=0; i<=5; i++)
{
if (StringLen(row))
row += ",";
row += "Open"+i+",High"+i+",Low"+i+",Close"+i;
}
FileWrite(file, row);
// Copy all required information from the history
MqlRates rates[], rate;
int count = Export_Bars + 5;
if (CopyRates(Symbol(), Period(), 1, count, rates) < count)
{
Print("Error! Not enough history size for exporting required information.");
return;
}
ArraySetAsSeries(rates, true);
// Write data
for (int bar=0; bar<Export_Bars; bar++)
{
row="";
double zlevel=0;
for (int y=0; y<=5; y++)
{
if (StringLen(row))
row += ",";
rate = rates[bar+y];
if (y==0)
zlevel = rate.open; // level of price calculation
row += NormalizeDouble(rate.open -zlevel, Digits()) + ","
+ NormalizeDouble(rate.high -zlevel, Digits()) + ","
+ NormalizeDouble(rate.low -zlevel, Digits()) + ","
+ NormalizeDouble(rate.close-zlevel, Digits());
}
FileWrite(file, row);
}
FileClose(file);
Print("Export of data is finished successfully.");
} else Print("Error! Failed to create the file for data export. ", GetLastError());
}
所以我的问题是,为了将过去的数据导出到目前的顺序,需要对脚本进行哪些更改?
答案 0 :(得分:1)
for ( int bar = Export_Bars - 1; // .LOOP-INIT(s)
bar >= 0; // .LOOP-RUN-PRE-CONDITION
bar-- // .LOOP-RUN-POST-UPDATE(s)
) {...} // .LOOP-RUN-BODY