Exporting data using MQL4

Michael Pogson - MyForexResults.com

Whilst the Strategy Tester on the Metatrader platform produces a variety of useful statistics to help you determine how your strategy performs on previous market data, what do you do if you want more information on how multiple strategies perform together? What was the maximum drawdown on the account when you have four strategies running together, for example. Or what if you want to know the statistics for each individual quarter of the three year period that you were backtesting (or indeed live trading) for?

To take full advantage of the data generated by your expert advisor necessitates importing it into a spreadsheet program such as MS Excel, where you can perform further analysis. Now, you could just copy the results from the Strategy Tester (or Account History for live trades) straight into Excel. However, by automating this process you can make your life just that little bit easier and take advantage of some of the other benefits that this brings as well (such as being able to import any data associated with a trade rather than just those displayed by the Strategy Tester). The first step in this process is to get your expert advisor to save any data that you may have a use for later into a comma separated values (CSV) file for each trade that it executes and this will be the focus of this article.

The MQL4 language provides a number of methods to handle text or CSV files and we want to take advantage of these to achieve the following:-

  • 1. Find data we want to export.
  • 2. Create/Open the export file.
  • 3. Find the end of the file.
  • 4. Append data to the file.
  • 5. Close the file.
There are a few ways that you could find the trades that you are interested in exporting the data for. My favoured way is to find all the trades that closed during the last bar in the chart and then determine if they were from the expert advisor that we are interested in. This method can then be called upon the creation of each new bar on the chart.

First we iterate through all of the orders in the history and select each one for further analysis:-

int i = 0;

for(i=0;i<OrdersHistoryTotal();i++){
  if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
  // More code to go here
  }
}

With each selected order we can determine if it is from the expert advisor that we are interested in. The easiest way to do this is to assign a magic number to each trade placed by a particular expert advisor. That way you can select the order like so:-

int i = 0;

for(i=0;i<OrdersHistoryTotal();i++){
  if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
    if(OrderMagicNumber()==magicNumber){
    // More code to go here
    }
  }
}

Next we need to determine if the order was closed during the last bar of the chart (otherwise we would end up exporting the data for the same order each time this method was called):-

int i = 0;

for(i=0;i<OrdersHistoryTotal();i++){
  if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
    if(OrderMagicNumber()==magicNumber){
      if((OrderCloseTime()>=iTime(NULL,0,1))&&(OrderCloseTime()<iTime(NULL,0,0))){
      // More code to go here
      }
    }
  }
}

Now we have an order that was placed by the expert advisor of interest and that was closed sometime during the last bar of the chart, we can set about exporting the data for it. The first step is to retrieve a handle for the file we want to use to export the data to. The handle simply acts as a reference object for the file so that we can refer to it later on. When we call the FileOpen method we need to pass the following arguments:- name of the file that we want to open; the data type of the file (csv or text); the type of operation we want to perform; the character used to delimit the data values, in this case ';'.

Then we need to find the end of the file by calling the FileSeek method, giving it the handle to the file we generated from the FileOpen method, the offset from the position we want and the location to find in the file (in this case we want the end so that we do not overwrite any data previously written to it):-

int i = 0;
int handle;
string dataFile = "filename.csv";

for(i=0;i<OrdersHistoryTotal();i++){
  if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
    if(OrderMagicNumber()==magicNumber){
      if((OrderCloseTime()>=iTime(NULL,0,1))&&(OrderCloseTime()<iTime(NULL,0,0))){
      handle = FileOpen(dataFile,FILE_CSV|FILE_WRITE|FILE_READ,';');
      FileSeek(handle,0,SEEK_END);
      // More code to go here
      }
    }
  }
}

Finally, we are ready to write the data we want from the selected trade to the file and lastly close the file using the FileWrite and FileClose methods, each time passing the handle as a reference for the file we want to perform the operation on. We specify each set of data we want to write to the file in the FileWrite method as a comma separated list. Each value passed in the example here is a call to the selected trade to get a particular piece of information about it:-

int i = 0;
int handle;
string dataFile = "filename.csv";

for(i=0;i<OrdersHistoryTotal();i++){
  if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
    if(OrderMagicNumber()==magicNumber){
      if((OrderCloseTime()>=iTime(NULL,0,1))&&(OrderCloseTime()<iTime(NULL,0,0))){
      handle = FileOpen(dataFile,FILE_CSV|FILE_WRITE|FILE_READ,';');
      FileSeek(handle,0,SEEK_END);
      FileWrite(handle,OrderTicket(),TimeToStr(OrderOpenTime()),OrderType(),OrderLots());
      FileClose(handle);
      }
    }
  }
}

And that's it! You can call this method when each new bar on the chart is created and it will export all the details of the trades that were executed by the expert advisor that you are interested in to a CSV file that you can then use with your spreadsheet program. If you are running the expert advisor in the testing mode, the file will be stored in the tester/files directory. On live trades the file will be stored in the experts/files directory.

Log in to post comments

Share |