
MetaTrader5 Python Trading Bot
How to Build a MetaTrader 5 Python Trading Bot: Getting Started
Ever wanted to build an automated trading bot? Got a strategy in mind? Got some skills in Python?
My new series “How to Build a Meta Trader 5 Python Trading Bot” was written just for you.
Update August 2023
I could never have anticipated how popular this series, along with a YouTube channel would become.
After a listening to my readers/viewers feedback, I realized that many people were spending huge amounts of time trying to solve installation/configuration problems — rather than experiencing the joy of algorithmic trading.
I want to change this narrative — and in so doing, open up algorithmic trading for everyone.
To do this, I’ve recently launched Tradeoxy: Trading For Everyone.
If you’re reading this, Tradeoxy will simplify 90% of this series, using a series of powerful API’s and easy to use tooling.
Currently it’s in the early-access / building stage and I’d be incredibly grateful if you’d join us on this adventure. Your feedback will help us shape a better product.
Join the early access program (for free) here.
You can also view our launch video, and follow our journey on LinkedIn, Twitter, Instagram, YouTube.
About the Series
The series shows you how to build your very own Python Trading Bot for Meta Trader 5 using the MetaTrader module for Python.
The series assumes that you have a working knowledge of Python scripting, know how to use an IDE and understand the basics of trading (stop loss, take profit, and so on). All code is used at your own discretion and risk, including developing your own strategy 😊
In This Episode
By the end of this episode, your project will be set up and MT5 will be getting started by Python.
Getting Started
About MetaTrader5
MetaTrader5 (MT5) is one of the most widely used retail trading platforms for Foreign Exchange (Forex), Stocks, and Futures on the planet. Many large brokers (for instance IC Markets) provide it to their retail clients.
Before MT5, there was Meta Trader 4 (MT4). For our purposes, the biggest functional difference between MT4 and MT5 is the Python-based Application Programming Interface (API) implemented in MT5. This expanded the previous C++ based Meta Query Language (MQL) into the more readable and vastly more popular Python Programming Language.
What You Need
For the rest of this series, you need the following:
- A Windows 10 or above computer (for reasons known only to Meta Trader, the macOS and Linux versions of MT5 don’t interface with Python)
- Python 3 installed (I used Python 3.10 for this series)
- A trading account set up through your broker (I highly recommend that you use a Demo Account for this series)
Project Setup
Your project will use main.py and three other files. Set these up now in your IDE of choice (I use Jetbrains Pycharm):
main.py— the main function for our trading botmt5_interface.py— our interface with MT5strategy.py— our trading strategysettings.json— your project settings. If you’re using a Version Control System (VCS), immediately add your settings.json to .gitignore or equivalent
It should look something like this:

Loading Your Settings
For this series, all settings variables are stored in JSON format in a separate file called settings.json. JSON is used as it is a widely used text-based format for representing structured data, especially on websites, and it is stored in a separate file to prevent the hard-coding of sensitive information into scripts.
An example of what settings.json looks like can be found in the file example_settings.json, linked to GitHub here. This file contains the full list of settings needed for the tutorial, with any sensitive information removed.
Let’s get settings.json implemented.
Set Up settings.json
Four variables are needed initially:
username— the username being used for your MT5 login (demo account!)password— the password for the MT5 loginserver— the server your broker gives you to log in tomt5Pathway— this is the pathway to your MT5 .exe on windows
Typically the username, password, and server variables are provided by your broker, while the mt5Pathway is where your MT5 executable is stored. Here’s what mine looked like (fake information used for username/password 😊):
{
"username": "your_username - typically an 8 digit number",
"password": "Do not share with anyone!",
"server": "ICMarkets-Demo",
"mt5Pathway": "C:/Program Files/ICMarkets - MetaTrader 5/terminal64.exe"
}Importing Settings Function
Next, you need to import these settings into the program. This will be done through main.py Here’s the function:







