How To Download Ticks & OHLC Data From MT5?

Have you ever tried to download OHLC price data from MT5? After some effort you will realize that unlike MT4, you cannot download OHLC price data from MT5. OHLC means Open, High, Low and Close price. On MT4 you can easily download OHLC data by clicking on Tools > History  In this post I will give you a script that will help you download OHLC price data for any time frame.I will also give you a script in this post that you can use to download ticks data. You will ask why we need this data. If you are into algorithmic trading, then you need this data to build algorithmic trading models and test them. You can also read this post on how to design an algorithmic trading strategy for binary options.

How To Download Ticks Data From MT5?

Tick is when the price changes. Price can change every second. In a minute there can be 20-40 ticks. When you are developing indicators and expert advisors, it becomes very important that you learn how to handle tick data. Ticks data is very important in today’s financial markets. Today there are many traders that includes algorithms. These algorithms can cause flash crashes in less than a minute. So we need to know how to use tick data in predicting price there are a number of models that we can use in predicting price using tick data. In a future post I will show you how to use the probit model using ticks data. Deep learning and neural networks are being used extensively in algorithmic trading. If you want to learn how to use neural networks in trading, you can take a look at my course Neural Networks for Traders. Did you read this post on how to predict the weekly high, low and close? Below is a screenshot of ticks.

EURUSD Ticks Data

You can see from the screenshot above that ticks are not regularly spaced. Sometime you receive ticks in rapid successions and sometimes there is a gap before you receive ticks day. If you are into algorithmic trading then you must have found out how important market microstructure is for modelling. When we talk of market microstructure we need the tick data. Tick data is not equally spaced like the 1 minute, 5 minute, 15 minute, 30 minute, 1 hour etc data. So we need to take this into account when doing the modelling. In this post I am going to discuss in detail how we can download tick data from MT4 and MT5. If you have been doing algorithmic trading you know how difficult it is to get good tick data. Most of the time you will have to buy tick data. Why buy when you can download this data. Sometime we need to model data on 15 second or 30 second timeframe. MT4/MT5 lowest timeframe is 1 minute. We cannot go below it. So we need to use our coding skills to see if we can model data on 30 second timeframe.. Watch this documentary on the life of a millionaire forex trader. Below is a screenshot of 3 ticks data.

EURUSD Ticks Data

MQL5 has predefined structures. Structure is a set of variables of related type.There are a number of predefined structures that we should know. One of the predefined structures is MqlTick. Below is the definition of MqlTick structure. MqlTick allows us to access tick data from the trade server. We can use this structure to download the most recent time, price volume for a currency pair.

struct MqlTick
{
datetime time; 
// Time of the last prices update
double bid; 
// Current Bid price
double ask; 
// Current Ask price
double last; 
// Price of the last deal (Last)
ulong volume; 
// Volume for the current Last price
};

As you can see we can access the recent time, bid price, ask price, price of the last deal and volume using this structure. We will use this predefined CopyTicks() function. We can use MQL5 file functions to read and write this tick data. Below is the CopyTicks() function.

int  CopyTicks(
   string           symbol_name,           
// Symbol name
   MqlTick&         ticks_array[],         
// Tick receiving array
   uint             flags=COPY_TICKS_ALL,  
// The flag that determines the type of received ticks
   ulong            from=0,                
// The date from which you want to request ticks
   uint             count=0                
// The number of ticks that you want to receive
   );

CopyTicks() function first argument is the symbol while the second argument is the MqlTick array that copies the ticks. The third argument is the flag that tells us which type of ticks to copy to the array. It can be COPY_TICK_INFO which will copy the bid and ask ticks. COPY_TICK_TRADES which will copy the last tick trade and the volume while COPY_TICKS_ALL will copy all the ticks. Fourth argument is date from which is a UNIX date in milliseconds from 1970.01.01. If from=0, we start from the last count tick and this is the default value. .Last argument is the number of ticks requested which should not exceed 2000 ticks. Read this post on how to double your account daily trading strategy.

If you want to become a expert algorithmic trader than you should master the intricacies of dealing with tick data. COPY_TICKS_ALL flag is the fastest method to copy the tick data. Other flags like COPY_TICKS_INFO and COPY_TICKS_TRADE can take time to process. Your client terminal saves around 4,000 ticks for each instrument in the fast cache access. If the requested ticks are beyond those saves, CopyTicks() will call the terminal memory so it can take time if you requested ticks for previous days. Now most of the time we will not need more than 1000 ticks so I don’t think we should face delays. If there are synchronization problems, CopyTicks() can wait upto 45 seconds if called from an EA. Keep this in mind 45 seconds is a pretty long time when it comes to algorithmic trading. You can download this pinbar indicator MQL4 code.I have written the following script that download tick data:

//+------------------------------------------------------------------+
//|                                                      Script1.mq5 |
//|                                                     Ahmad Hassam |
//|                                         http://tradingninja.com/ |
//+------------------------------------------------------------------+
#property copyright "Ahmad Hassam"
#property link      "http://tradingninja.com/"
#property version   "1.00"
#property script_show_inputs

// requeted number of ticks 5000
input int getTicks=5000; 
// this is the number of requested ticks to be downloaded we can increase it


//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
  
  int tries = 0; 
  // number of tries for downloading tick data
  bool trySuccess=false; 
  //this will tell us if the try was successful
  MqlTick Ticks[];
  // this is the array that will receive the tick data
  while ( tries < 5) 
  // we make 5 tries to download the tick data
  {
  
  uint startCounting=GetTickCount();
  
 // download the tick data 
  int receivedTicks=CopyTicks(_Symbol,Ticks,COPY_TICKS_TRADE,1,getTicks);
  
  if (receivedTicks!=-1)
  {
  
  // print the tick count
  PrintFormat("%s: receivedTicks %d ticks in %d ms",_Symbol,receivedTicks,
GetTickCount()-startCounting);
  trySuccess=true;
  break;
  }
 // increment the tries if the attempt at downloading tick data failed
  tries++;
 //put some time between the download attempt
  Sleep(2000);
  continue;
  }
  }
// A (here I will add the code for writing the ticks data to a file)

Above script was able to download 5000 ticks. In the above script I have not done any error/exception handling. This is a simple rudimentary script. It make 5 attempts to download the tick data. On a successful attempt it breaks from the while loop. You can check the following message in the Experts tab of Toolbox window! It took around 56 seconds for the CopyTicks() to download the tick data. So you can see it will take at least 30-60 seconds for the above script to download the data.

2017.09.07 19:38:01.211	Script1 (EURUSD,H4) EURUSD: receivedTicks 5000 ticks in 56719 ms

This is how we can copy the tick data. Now we need to write the data that we have copied in our tick_array[] into a csv file. Now we need a function that can write to a csv file. CSV is a comma separated file and this format is widely used to download data. Python, R and Java can easily read csv files. In our case we want the function to write a csv file that contains the data time and ticks data. First we will open a file. This file will be opened in MQL5\Experts\Files directory. We will use the following function to open the file.

int FileOpen(
string file_name, 
// File name
int open_flags, 
// Combination of flags
short delimiter='\t' 
// Delimiter
uint codepage=CP_ACP 
// Code page
)

The parameter open_flags tells the compiler what to do. FILE_READ means only read the file. FILE_WRITE means write into the file.FILE_CSV records the file file as csv with all the recorded values as strings. FileOpen() function returns an integer which is used as a file handle. File handle is something you should take it as being told. We have read and write privileges for the file. Now we have downloaded 5000 ticks from the broker server. We will have to write this ticks data line by line to our file. After writing one line, we need to move the cursor to the next line which is done using FileSeek() function. Read this post on how GBPUSD fell 300 pips when UK PM made annoucement for an early election. Below is the code for the FileSeek() function.

bool  FileSeek(
   int                  file_handle,     
// File handle
   long                 offset,          
// In bytes
   ENUM_FILE_POSITION   origin           
// Position for reference
   );

As you can see FileSeek() function uses File Handle which is a the first paramter. File handle is an important variable when it comes to reading and writing to a file. We will use SEEK_END to move the cursor the start of the new line. SEEK_END is a variable in ENUM_FILE_POSITION. We also need FileWrite() and FileClose() functions.

uint  FileWrite(
   int  file_handle,   
// File handle
   ...                 
// List of recorded parameters
   );

FileWrite() function returns uint which means unsigned integer. First is the file handle variable as usual. Then comes the list of variables that we want to copy and write.

void  FileClose(
   int  file_handle      // File handle
   );

FileClose() function closes the previously opened file with FileOpen(). Reading and writing to files is important for you to know if you want to write the data from your custom indicator or your expert advisor to a file. When you are developing your expert advisor you will need to write the details of each trade to a file so that later on your can check the functioning of the expert advisor. Below is the remaining code for writing the ticks data to the file. You will place it below A in the above code that I have provided for copying the ticks data.

  // write the ticks downloaded to a csv file
int fileHandle = FileOpen("Ticks.csv",FILE_READ|FILE_WRITE|FILE_CSV, ",");

if(fileHandle==INVALID_HANDLE){
Alert("Error opening file");
return;
}

for( int i=1; i<=5000; i++)
{
FileWrite(fileHandle,_Symbol,Ticks[i].time, Ticks[i].bid, Ticks[i].ask, 
Ticks[i].last,Ticks[i].volume);
FileSeek(fileHandle,0,SEEK_END);
}
FileClose(fileHandle);
}
//+------------------------------------------------------------------+

Save this script file and use it whenever you need to download ticks data.

How To Download OHLC Data From MT5?

I started with the question whether you have ever tried to download OHLC price data from MT5. Now I return to that question. The code is almost the same as that for the ticks data. Below is the code. I will explain it below.

//+------------------------------------------------------------------+
//|                                                         OHLC.mq5 |
//|                                                     Ahmad Hassam |
//|                                         http://tradingninja.com/ |
//+------------------------------------------------------------------+
#property copyright "Ahmad Hassam"    // A
#property link      "http://tradingninja.com/"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   MqlRates OHLC[];                    // B
   int copiedOHLC=CopyRates(NULL,0,0,1000,OHLC);
   if(copiedOHLC<=0)
      Print("Error copying OHLC price data ",GetLastError());
   else Print("Copied ",ArraySize(OHLC)," bars");
// write the OHLC data to a csv file    // C

int fileHandle = FileOpen("OHLC.csv",FILE_READ|FILE_WRITE|FILE_CSV, ",");

if(fileHandle==INVALID_HANDLE){
Alert("Error opening file");
return;
}

for( int i=1; i<=2000; i++)
{
FileWrite(fileHandle,_Symbol,OHLC[i].time, OHLC[i].open, OHLC[i].high, 
OHLC[i].low,OHLC[i].close, OHLC[i].spread);
FileSeek(fileHandle,0,SEEK_END);
}
FileClose(fileHandle);
}
//+------------------------------------------------------------------+

Now let me explain the above code. A is the preprocessor statements which are just the copy right statements. In B, we first declare an OHLC array. OHLC array is declared of the type MqlRates structure. After that we use CopyRates() function to copy the Open, High, Low, Close data to the OHLC array. In C we write the OHLC array data to a file named OHLC. The code is almost similar to the code for writing ticks data. We just download 2000 OHLC data. 2000 rows of OHLC data is sufficient for model building. As said in the beginning, we need this data to build machine learning and deep learning models and then test them for their accuracy. The goal is to build a prediction model with above 80% accuracy. This is a challenging task.

Read this post that explains my candlestick trading strategy that makes 100-200 pips per trade with 10 pips stop loss. This is our ultimate goal to build a custom indicator or an expert advisor that can make 100-200 pips with a small stop loss of 10-20 pips. Keep reading my blog, I will show you how to do it.

Published
Categorized as Forex Tagged