The following MQL4
script exports data from MetaTrader to a csv
file. Unfortunately ( for me at least ), the order of the data in the generated csv
file from 0 to 1000, 0 being the most recent ( present to past ). I want the file to be sorted from 1000 to 0 ( past to present ).
I altered the write data loop below to: for (int bar=Export_Bars; bar==0 bar--)
but this simply generated an empty csv
file.
#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());
}
So my question what changes need to be made to the script in order to export data in the past to present order?
for ( int bar = Export_Bars - 1; // .LOOP-INIT(s)
bar >= 0; // .LOOP-RUN-PRE-CONDITION
bar-- // .LOOP-RUN-POST-UPDATE(s)
) {...} // .LOOP-RUN-BODY