MT4导出脚本

MT4 export script

以下 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());
}

所以我的问题是需要对脚本进行哪些更改才能将过去的数据导出为现在的顺序?

更改时间反向迭代器步进的循环构造函数:

 for ( int bar  = Export_Bars - 1;        // .LOOP-INIT(s)
           bar >= 0;                      // .LOOP-RUN-PRE-CONDITION
           bar--                          // .LOOP-RUN-POST-UPDATE(s)
       ) {...}                            // .LOOP-RUN-BODY