MetaTrader5
Download All Tick Data for Symbols in MetaTrader 5
If you're looking to download all available tick data for a specific symbol in MetaTrader 5, this guide is for you! This nifty expert advisor will scan your broker's market watch and pull all the ticks or ticks up to a specified date.
Having access to comprehensive historical data is crucial for backtesting your strategies or creating custom charts based on ticks. Just a heads-up: be sure you have enough hard drive space, as the terminal stores this data in the data folder.
Before we dive into the code, we'll need a download manager to facilitate the download process. The CDownloadManager structure will hold everything we need.
struct CDownloadManager
{
bool m_started, m_finished;
string m_symbols[], m_current;
int m_index;
}
State of the download (started/finished)
List of symbols to scan
Current symbol being processed
Index of the symbol being scanned
We’ll also need functions to read and write to the hard drive, especially since we’re working with symbols. Here’s how we can write a string to a file:
void writeStringToFile(int f, string thestring)
{
// Save symbol string
char sysave[];
int charstotal = StringToCharArray(thestring, sysave, 0, StringLen(thestring), CP_ACP);
FileWriteInteger(f, charstotal, INT_VALUE);
for(int i = 0; i < charstotal; i++)
{
FileWriteInteger(f, sysave[i], CHAR_VALUE);
}
}
This function takes:
File handle f (opened for writing with binary flags FILE_WRITE|FILE_BIN)
The string to write to the file
It writes the length of the string followed by each character. To load the string from a file, here’s how you can do it:
string readStringFromFile(int f)
{
string result = "";
// Load symbol string
char syload[];
int charstotal = (int)FileReadInteger(f, INT_VALUE);
if(charstotal > 0)
{
ArrayResize(syload, charstotal, 0);
for(int i = 0; i < charstotal; i++)
{
syload[i] = (char)FileReadInteger(f, CHAR_VALUE);
}
result = CharArrayToString(syload, 0, charstotal, CP_ACP);
}
return(result);
}
This function takes:
File handle f (opened for reading as binary, flags FILE_READ|FILE_BIN)
It reads the integer length of characters expected, processes them into a char array, and returns the result as a string.
Now, let's initialize the download manager and fill it with symbols from the market watch:
//+------------------------------------------------------------------+
//| Grab symbols from the market watch |
//+------------------------------------------------------------------+
void grab_symbols()
{
//! Only from the market watch!
int s = SymbolsTotal(true);
ArrayResize(m_symbols, s, 0);
for(int i = 0; i < ArraySize(m_symbols); i++)
{
m_symbols[i] = SymbolName(i, true);
}
}
Here's a quick rundown of what this function does:
It checks how many symbols are in the market watch (active)
Resizes the m_symbols array to accommodate them
Loops through the total symbols to request their names
Next, we need a function to manage the download process:
//+------------------------------------------------------------------+
//| Manage the download of symbols process |
//+------------------------------------------------------------------+
void manage(string folder, string filename)
{
// Essentially this starts or navigates to the next symbol
// If set
if(ArraySize(m_symbols) > 0)
{
// If not started
if(!m_started)
{
m_started = true;
// Go to first symbol
m_current = m_symbols[0];
m_index = 1;
save(folder, filename);
if(_Symbol != m_current)
{
ChartSetSymbolPeriod(ChartID(), m_current, _Period);
}
else
{
ENUM_TIMEFRAMES new_period = PERIOD_M1;
for(int p = 0; p < ArraySize(TFS); p++)
{
if(_Period != TFS[p])
{
new_period = TFS[p];
break;
}
}
ChartSetSymbolPeriod(ChartID(), m_current, new_period);
}
return;
}
// If started
else
{
m_index++;
if(m_index
2025.02.22