Monday, August 08, 2016

Filled Under: , , ,

MATLAB-MQL5 Interaction: Semi-Automated Way to Transfer Data

Monday, August 08, 2016

Introduction

Matlab and the Metatraders form a great trading software stack that provides powerful research tools and a quick access to the foreign exchange market. In this software setup, Matlab acts as the calculating engine and the Metatrader supply the market data. Trading models from the simple technical analysis to the sophisticated machine learning or econometric models are far more efficient to implement in Matlab relative to the Metatrader’s mql4 (5).  This article presents a semi-automated way to transfer historical market data from Metatrader 5 to Matlab. This method is useful when looking out for potentially profitable models that are inclined towards using linear algebra, time-series methods, and differential equations, etcetera kind of mathematics. Later, a fully automated way of matlab-mql5 live trading interaction will be presented.

Basic Structure of the Process

MATLAB R2014B for Mac OS and MT5 are used in this demonstration. The proposed method has two major parts; MQL5 script that copies the symbol data from the MT5 terminal to CSV files. The other part is a Matlab script that reads the CSV files and imports the data to the Matlab workspace as a table data structure variable.

The MQL5 Script 

The first thing is to load the ‘writedatatofile’ script on the respective chart you wish to download data. Ensure the historical data you wish to copy to CSV file is visible on the chart. The script has inputs that specify the various options of copying the method. Below is the screenshot after loading the script. 
 
Choose the data date range you wish to send to Matlab and the date format of choice. The script provides the option to copy only the data of the current chart or all the symbols in the market watch. The script shows the progress of copying the data to CSV file.
 
The CSV file is stored in the common data folder, that is  (‘/MQL5/Files/DATA_OHLC/Symbol'). The script creates the DATA_OHLC folder. Inside that folder contains a folder for each symbol that is copied. The file created has an extension of the time frame. For example, if the script is run on a EURUSD daily chart, the file is named as EURUSD_D1 and if it is on a 4-hour chart, it is named EURUSD_H4.

The Matlab Script

 The other step is to extract the CSV data files on the mql5 common folder to the Matlab workspace. A unique script, like the one below (EURUSD) is developed to extract data for each symbol.
  
   
The second line of the script directs Matlab to the file path where the CSV file of the symbol is stored. Replace the file path given to the appropriate one. The appropriate file path depends on where Metatrader 5 is installed. The script is named after the name of the symbol; for instance, EURUSD, GBPJPY etcetera. When you type EURUSD on Matlab’s command window, the script requests the time frame you wish to import from the CSV files. If you require the daily OHLC, type EURUSD_D1.

Conclusion

When the script runs smoothly without errors/warnings, the OHLC data and respective dates and time are imported. The data is stored as a table data structure. The data can now be pre-processed for further analysis. The proposed method is useful since it eliminates the repetitive process of having to copy the data from excel files into Matlab workspace. It is suitable when you require to importing huge data from MT5 to Matlab frequently.
                
 
 
 
 

0 comments:

Post a Comment