Getting Data from MT5 to Python

As an Amazon Associate I earn from qualifying purchases.

4 Min Read

The data from my trader comes from MetaTrader5 (MT5 platform). The MT5 platform allows you to collect data from any broker that you may already be trading with on the platform. You may wonder why I decided to use MT5 over the more popular MT4. This is because there is no python API integration with MT4. A lot of brokers these days support MT5 so I didn’t see it as an issue.

Setting up

Start by installing the following:

Python : https://www.python.org/downloads/

MT5 Desktop Application: https://www.metatrader5.com/en/download

MT5 API: Run the following line in a command line to install the MT5 Python API :

pip install MetaTrader5

The next step is to create an account on MT5. For the purpose of this blog, I will be using a demo account.

To create an account go to:

File> Open an Account> Search ‘Metatrader’> Click ‘MetaQuotes Software Corp’> Next> Next> Enter required info> Next

If successful, you should see the following screen:

Save your login and password for later. This is important.

The last step you will need to do on the MT5 desktop application is enable ‘Algo Trading’. This is done from the toolbar:

Clicking the ‘Algo Trading’ button will enable you to connect the Python MT5 to the MT5 desktop application.

Now you have Python, the MT5 desktop application and the MT5 API. Now let’s connect to the MT5 desktop application using our python library. For this example, I am using IDLE.

The code

First, import the MetaTrader5 library and the datetime library (you will be using this later):

import MetaTrader5 as mt5
from datetime import datetime

Then assign your account id to a variable . Since you are already logged into the account on the MT5 desktop application you do not need to enter a password:

account = int(YOUR ID HERE)

After this, initialize the connection and login to MT5:

mt5.initialize()
authorized=mt5.login(account)

if authorized:
    print("Connected: Connecting to MT5 Client")
else:
    print("Failed to connect at account #{}, error code: {}".format(account, mt5.last_error()))

If you are successful in connecting to MT5 desktop application via python you should see the following message:

Connected: Connecting to MT5 Client
>>> 

Now that you’ve connected to the MT5 desktop application, let’s get some data! For this example I will be pulling in 4 hour tick data for EURUSD. Start by defining the date range you want to collect data for. I have chosen to collect data from 2021/1/1 – 2021/1/10:

utc_from = datetime(2021, 1, 1)
utc_to = datetime(2021, 1, 10)

The next step is to actually pull the data. This is achieved using the copy_rates_range() function:

rates = mt5.copy_rates_range("EURUSD", mt5.TIMEFRAME_H4, utc_from, utc_to)

To see the data, you can use the following code which will print out each line:

for rate in rates:
    print(rate)

You should now see the data printed on screen. In the upcoming blog posts I will show you how to make this data more human readable:

Connected: Connecting to MT5 Client
(1609718400, 1.22395, 1.22557, 1.2228, 1.22526, 10671, 0, 0)
(1609732800, 1.22526, 1.22591, 1.22413, 1.2255, 9383, 0, 0)
(1609747200, 1.22549, 1.23004, 1.2242, 1.22932, 14169, 0, 0)
(1609761600, 1.22932, 1.2306, 1.22863, 1.22988, 16335, 0, 0)
(1609776000, 1.2299, 1.23098, 1.225, 1.22529, 25580, 0, 0)
(1609790400, 1.22529, 1.22568, 1.22416, 1.22473, 9482, 0, 0)
(1609804800, 1.22466, 1.22632, 1.22432, 1.22572, 6256, 0, 0)
(1609819200, 1.22572, 1.22768, 1.22569, 1.22723, 7677, 0, 0)
(1609833600, 1.22724, 1.22839, 1.22551, 1.22723, 14790, 0, 0)
(1609848000, 1.22724, 1.22906, 1.22605, 1.22626, 15457, 0, 0)
(1609862400, 1.22626, 1.23051, 1.2254, 1.22994, 23074, 0, 0)
(1609876800, 1.22991, 1.23056, 1.22895, 1.22965, 8126, 0, 0)
(1609891200, 1.22968, 1.23261, 1.22811, 1.22892, 15458, 0, 0)
(1609905600, 1.22893, 1.23065, 1.22757, 1.22967, 16348, 0, 0)
(1609920000, 1.22966, 1.23455, 1.22965, 1.23382, 21580, 0, 0)
(1609934400, 1.23383, 1.23494, 1.23166, 1.23196, 20239, 0, 0)
(1609948800, 1.23195, 1.23206, 1.22657, 1.2299, 26706, 0, 0)
(1609963200, 1.2299, 1.23399, 1.22964, 1.23252, 17928, 0, 0)
(1609977600, 1.23256, 1.23441, 1.23228, 1.23306, 8261, 0, 0)
(1609992000, 1.23308, 1.23327, 1.23107, 1.23125, 8758, 0, 0)
(1610006400, 1.23125, 1.23256, 1.22642, 1.2269, 18702, 0, 0)
(1610020800, 1.22688, 1.22785, 1.22452, 1.22686, 17734, 0, 0)
(1610035200, 1.22687, 1.22826, 1.22464, 1.22655, 23308, 0, 0)
(1610049600, 1.22657, 1.2279, 1.22628, 1.2271, 7986, 0, 0)
(1610064000, 1.22713, 1.22731, 1.22349, 1.22479, 10254, 0, 0)
(1610078400, 1.22479, 1.22681, 1.22455, 1.22578, 9159, 0, 0)
(1610092800, 1.22578, 1.22691, 1.22133, 1.224, 18236, 0, 0)
(1610107200, 1.224, 1.22845, 1.22326, 1.22715, 18304, 0, 0)
(1610121600, 1.22711, 1.22835, 1.22222, 1.22288, 30760, 0, 0)
(1610136000, 1.22288, 1.22288, 1.21932, 1.22218, 13425, 0, 0)

And that’s it! You should now be able to connect to the MT5 desktop application using Python and pull 4 hour data for EURUSD. I hope that you have found this useful.

As always, If you have any comments/questions please feel free to post them below.

The full script for this tutorial can be downloaded here:

import MetaTrader5 as mt5
from datetime import datetime

account = int(YOUR ID HERE)

mt5.initialize()
authorized=mt5.login(account)

if authorized:
    print("Connected: Connecting to MT5 Client")
else:
    print("Failed to connect at account #{}, error code: {}"
          .format(account, mt5.last_error()))

utc_from = datetime(2021, 1, 1)
utc_to = datetime(2021, 1, 10)
rates = mt5.copy_rates_range("EURUSD", mt5.TIMEFRAME_H4, utc_from, utc_to)

for rate in rates:
    print(rate)

21 thoughts on “Getting Data from MT5 to Python”

  1. Avatar

    Hey, this is an amazing series. I am following along the way, however, there seem to be one error in line 17 (or at least I had it). The ticker code seems to end with a “.”, i.e., “EURUSD.” instead of “EURUSD”. Inclusion of “.” gave me the list of rates, and without “.” gave me “TypeError: ‘NoneType’ object is not iterable”

  2. Avatar

    Hello Conor,
    First of all, thank you for your wonderful work !

    I had a question concerning the MetaTrader API on macOS.

    I tried to installing using “pip install MetaTrader5”. And I get this error:

    “ERROR: Could not find a version that satisfies the requirement MetaTrader5 (from versions: none)
    ERROR: No matching distribution found for MetaTrader5”

    After some research I found out that MetaTrader is not coded to work on MacOS …

    Do you have any information about this ?

    Thank you in advance for your help 🙂

    1. Conor O'Hanlon

      Thank you! Yeah, I believe this will only work on Windows for now. You might want to setup an EC2 instance on AWS. I believe the base tier is free 🙂

  3. Avatar

    Hi, I keep getting ModuleNotFoundError: No module named ‘MetaTrader5’ when i try to import. I have uninstalled and installed but not sure why it’s not working. Sorry, I’m sure this is a basic error.

  4. Avatar

    Hi – I’ve downloaded MT5 and Python, but when I try and the MT5 API I’m running into issues. I tried typing in pip install MetaTrader5 in command prompt, but I’m getting the following “‘pip’ is not recognized as an internal or external command,
    operable program or batch file.” Any thoughts? I’m attempting this on my work laptop – could I be hitting a firewall? Thanks.

  5. Avatar

    Hi Conor – Ben Jones again. Thanks in advance for your help on this, and I may be just running up against aggressive firewalling from my company, but when I type in python or any of the other prompt suggestions, I get the following:

    ‘python’ is not recognized as an internal or external command,
    operable program or batch file.

    I’m interested most in your tutorial and offerings here because I’m curious about building scripts for trading. I already do it as a hobby, but I love the idea of building coding skills while simultaneously learning about the market. Honestly, I may just not have the hardware bandwidth to pull it off, but I’ve got resources to remedy that, so I’d rather not waste time. Thoughts? Thanks again.

  6. Avatar

    I’m loving this read, Connor! You’re amazing for bringing us all along on your ride!
    I’m looking forward to better understanding how MT5 integrates with my Broker (e.g., which API is used in your app, how it knows where to submit the order, etc.). Thanks!

  7. Avatar

    Unable to install MT5 on Raspberry Pi 400 (No matching distribution found for MetaTrader5).

    Can you suggest a workaround? Thanks.

    1. Avatar
      Anand Betanabhotla

      OK I got it going on Windows and created a demo account and logged in.

      Now when I try to connect to the desktop app from Python it throws error: No IPC connection. What am I missing? Thanks.

  8. Avatar

    Hi!
    I try to run “pip install MetaTrader5” in terminal on Mac OS, but there are two errors:
    ERROR: Could not find a version that satisfies the requirement MetaTrader5 (from versions: none)
    ERROR: No matching distribution found for MetaTrader5
    How can I fix this or what other services you’d recommend?

Leave a Comment

Your email address will not be published.

Subscribe to my newsletter to keep up to date with my latest posts

Holler Box