MetaTrader5 historical Data to Pandas DataFrame

Posted by

In this article, we will use Python to export MetaTrader5 historical OHLC data and then store the data as a Pandas DataFrame.


import MetaTrader5 as mt
import pandas as pd

If you do not have the modules installed, you can install the using pip.

MetaTrader5 Integration

Now, we will work with the MetaTrader5 library to connect to the platform and to receive historical data. MetaTrader5 has a method called initialize(), which will run the MetaTrader5 Client. After initializing the platform, we will then have to log in to a trading account that will have access to the server data provided by the broker. For that, we will use the method login().

In the example below, we will use an MT5 demo account provided by the broker VantageFX. If you do not have your own MT5 account, you can quickly get one by signing up here or use the example credentials.

login = 123
password = 'password'
server = 'server'

# runs MetaTrader5 client
# MetaTrader5 platform must be installed

# log in to trading account
mt.login(login, password, server) 

Once you execute the code, your MetaTrader5 platform should be running and logged into the trading account based on the credentials provided.

Requesting OHLC Data

Now we can interact with the platform, specifically for our case we are now able to request historical bars. The MetaTrader5 method we will use is copy_rates_from_pos().

symbol = 'EURUSD'
timeframe = 1 # integer value representing minutes
start_bar = 0 # initial position of first bar
num_bars = 1000 # number of bars

bars = mt.copy_rates_from_pos(symbol, timeframe, start_bar, num_bars)

The method copy_rates_from_pos() takes 4 arguments. You pass in the symbol name (which you can find in the platform) as a string, the timeframe of your OHLC data in minutes, the start index of the bar, and the number of bars.

Creating a Pandas DataFrame

We are almost there. Let’s print our variable bars and see what we have.

ohlc array

Printing bars gives us a list of tuples which we can easily convert to a Pandas DataFrame.

df = pd.DataFrame(bars)
pandas df

Here we go, we stored our 1-minute candles into a Pandas DataFrame. What is very convenient is that the column names are automatically assigned to each column when creating a DataFrame from MetaTrader5 bars. Very cool!

Plotting data with Matplotlib

The real fun now begins where we can use this data for data science and data visualization. A quick extra before we conclude this article is to show you, how to plot this data with Matplotlib.

import matplotlib.pyplot as plt

plt.plot(df.index, df['close'])
plot eurusd


This is a quick example of what we can do with the data. With this, we can visualize data and indicators and perform backtests.

I hope that you liked this article and make sure to subscribe to my mailing list if you wish to receive updates regarding new articles!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s