Algorithmic Trading with Python and MT5: Introduction II. The Basics

Sebastian Ospina Valencia
3 min readAug 18, 2021

--

The first thing i try to learn when i was starting on this world of algorithmic trading was how to connect python to a streaming data source in order to make statistical analysis and support my entries into the market. Although this purpose was not related with the algorithmic trading, get the data, this process is the initial step of any trading robot.

At the start of this process i only found partial solutions: brokers api’s with its own Python syntaxys and obviously, implied that you have to open an account with that broker. I was frustrated due the fact that i don’t wanted to depend of a broker to get the data. Fortunately, Metatrader 5 developed a Python library to connect your trading account, from any broker, to your python terminal. Before getting inside the code i want to show you the basic architecture and workflow of the Metatrader 5 -Python framework. Know this, simple, but powerful architecture will, in few words, save you a lot of headaches in the future deployment of your trading robots.

Figure 1.Pythn-Mt5 Framework

The framework in figure 1 shows how Metatrader and Python interact in order to generate the orders. At the core of the process is the data processing in Python, that has 4 main process:

  1. Data Processing : where you will modify, clean, analyze and create new features.
  2. Signal creation: where you will use your data and features to geneare buy/sell signals
  3. Generate Market orders: where you will translate your signals into market orders
  4. Send Market orders: Where you will send your markets orders to be executed on Metatrader

As you will notice in this framework there is no third party API is just you (in Python) talking directly to your Metatrader account which gives you an amazing power to create any custom indicator you imagine. But before we talk we will start connecting Python to Metatrader to gather the data symbols you want.

Gathering the Data

To gather the data you have to log in from python to your Metatrader account using the library MetaTrader5: To achieve this, you will need, your account number, your password and the server (broker) where is your account.

import pandas as pd
import numpy as np
import MetaTrader5 as mt5

name = 123746535 #The account Number
key = “In2020” #The account key
serv = “Pepperstone-MT5-Live06” #The server of your account
path = r”C:\Program Files\MetaTrader 5 B\terminal64.exe” #The folder where the MT5.exe is located
symbol = “EURUSD” #The symbol you want the data

Once you have defined this key variables you can initialize the connection with Metatrader 5

#mt5.initialize() establishes the connection with MT5. Here we pass the access keys and the path
mt5.initialize( login = name, server = serv, password = key, path = path)

Finally we gather the candlestick data of EURUSD in 1 hour timeframe. but you could use another frames to analyze (but we will talk later of this)

#mt5.initialize() establishes the connection with MT5. Here we pass the access keys anad the path
mt5.initialize( login = name, server = serv, password = key, path = path)

#First we get the last 1000 candlestick using the 1 hour data
rates = mt5.copy_rates_from_pos(symbol, mt5.TIMEFRAME_H1, 0, 1000)

#With these rates, then we create the dataframe with the organized columns
rates_frame = pd.DataFrame(rates)

#Finally we transform the Time column into a Timestamp column
rates_frame[‘time’]=pd.to_datetime(rates_frame[‘time’], uni
t= ‘s’

For full code, you can access to the gitlab repository where i will upload all the code presented here

https://gitlab.com/elospieconomics/algorithmic-trading

Do you have a strategy and want to automatize it while learning how to become an algorithmic trader? Contact me on Instagram @elospi18

--

--

Sebastian Ospina Valencia

Chief Developer Officer at InUp. Data Scientist with 5 years of experience in Banking. Teacher of Algorithmic Trading at Universidad del Rosario