Algorithmic Forex Trading With Python: Using MetaTrader5 Python Library For Accurate Data (2024)

Algorithmic Forex Trading With Python: Using MetaTrader5 Python Library For Accurate Data (3)

I am a math person. I got interested in the financial markets because of their enigma of being both stochastic and pattern-full. The financial market, at the core, is a complex system; the emergence of complexity could be spontaneous and the market crash could happen suddenly with no major changes in economic fundamentals. Unlike structured games of chance that have defined beginning and endings, as well as rigid rules to guide your behaviour, the market environment is more like a river, with prices constantly flowing. In fact, the market is unstructured to a point where, as a trader, you are making up all of your trading rules to play by, with a great deal of latitude to do so.

My process of developing trading skills naturally led me to algorithmic trading. To me, the advantages are numerous: aiding pattern identification, strategy development, and backtesting, to name a few.

The goal of an algorithmic trader is to develop a program that ultimately helps us make better trading decisions. The program can have the following roles:

  1. As a signal
  2. As a trader: analyzes the market data and executes trades
  3. As a post-trade analyst

When it comes to algorithmic trading, there are two broad categories of the underlying engine:

Rule-based algorithm

A Rule-based program follows a defined set of instructions to analyze data and place a trade. The defined sets of rules could be based on inputs such as timing, economic fundamentals, earnings or other data releases, and market data such as price, trading volume, etc.

AI-based algorithm

An AI-based program is one that that can learn and adapt without following explicit instructions, by using machine learning techniques and statistical models to analyze and draw inferences from patterns in data.

The defined sets of instructions are based on timing, price, quantity, or any mathematical model. Apart from profit opportunities for the trader, algo trading renders markets more liquid and trading more systematic by ruling out the impact of human emotions on trading activities.

When it comes to forex trading, the vast majority of trading robots being coded are in the mql4 or mql5 language, as MT4 and MT5 are the most prevalent platforms that people use. Mql4/mql5 is the programming language used for the MetaTrader4/5, the most popular trading platform for forex.

However, the trading algorithms (known as expert advisors, or EA) can only be rule-based using the native mql4/5 language — with no AI-capability was built in.

For those of us who love to use Python to accomplish this goal, it wasn’t always doable either. Python is limited NOT in terms of its capability but in terms of connectivity with brokers. This fact once made programming a trading algorithm in Python not scalable as one has to wait for individual brokers to set up API (Oanda, Interactive Brokers, etc.) for this connectivity. Without the API, you had to use surrogate data from other sources to get to the market data for your instruments.

This fact once made using Python trading algorithm hard to work with — in practice, one has to be careful with the market data obtained from other brokers, as a centralized exchange that establishes prices and trading volume does not exist in the forex market.

For those of us who love to use Python, we had to adapt and use mql4/mql5 to build the EAs. There are alternatives to circumvent this limitation of MT4/MT5. ZeroMQ is one of them.

However, with the Python module, MetaTrader5, this hurdle is quickly being eliminated!

With extensive libraries for data cleaning, wrangling and transformation, as well as a variety of machine learning libraries, Python is easily the most popular language used in data science and machine learning.

Advantages

One of the most obvious advantages of using MetaTrader5 is that you can get either OHLC data or tick data from any broker. This freedom that this gives to a trader developer is literally huge:

  1. You can now develop and test your algorithm based on the true market data that you would have received directly from a broker, as if you are operating from the MT5 terminal, but you will be receiving the OHLC or ticker data in a format that you can programmatically inspect, clean, transform and develop with!
  2. Since the market data offered by every broker is slightly different, with MetaTrader5 library, you will be able to access the broker-specific data that your trading system will be trading with!
  3. Furthermore, your development effort can now be scalable since you no longer need to be restricted by the connectivity issues created by a lack of broker APIs.

Connecting to Terminal and to a Specific Account

Connecting to a trading account is done as follows:

if not mt5.initialize():
print("initialize() failed, error code =", mt5.last_error())
quit()
mt5.login(login = <account_num>, password=<"acct_pass">)

Note that the login parameter requires an integer, and the password parameter requires a string input.

Getting Market Data (OHLC or Tick Data)

The following code block will retrieve the OHLC data for the past 150 calendar days:

if not mt5.initialize():
print("initialize() failed, error code =",mt5.last_error())
quit()
mt5.login()timezone = pytz.timezone("Europe/Tallinn")
now = datetime.datetime.now(timezone)
start = datetime.datetime.now(timezone) - timedelta(days=150)
utc_from = datetime.datetime(start.year, start.month, start.day)
utc_to = datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, now.second)
rates = mt5.copy_rates_range(pair, mt5.TIMEFRAME_H4, utc_from, utc_to)
print("Ticks received:",len(rates))
htf = pd.DataFrame(rates)

To get the tick data, you can use either the copy_ticks_from() or copy_ticks_range() functions. These are extremely helpful as they give you access to the tick data, and open up more strategic choices for you.

Placing Trades

The following code snippet shows how an order is implemented in Python using the MetaTrader5 package. Here, I have previously determined the object `threshold` to be the maximum spread with which I am willing to trade, and have calculated the parameters for the object entry, stop, target, size for each pair (i.e., symbol):

try:
if (mt5.symbol_info_tick(symbol).ask \-
mt5.symbol_info_tick(symbol).bid) <= threshold:
o1 = mt5.order_send({"action": mt5.TRADE_ACTION_DEAL,
"symbol": symbol,
"volume": size
"type": mt5.ORDER_TYPE_BUY,
"price": entry,
"sl": stop,
"tp": target,
"deviation": 10,
"magic": 00000,
"comment": "insert comment",
"type_time": mt5.ORDER_TIME_GTC
})
except Exception as e:
print(e)

Note that the “comment” parameter, once given a descriptive string, will show up as descriptions in the MT5 terminal under “comment”. This is very useful information as it provides useful information for analysis later on.

Modifying Trades

Using Python integrated with the MetaTrader5, you can modify your existing positions as follows:

to_close = mt5.order_send({
"action": mt5.TRADE_ACTION_DEAL,
"symbol": pair,
"volume": mt5.positions_get(symbol)[0].volume,
"type": mt5.ORDER_TYPE_SELL,
"position": mt5.positions_get(symbol)[0].identifier,
#"price": mt5.symbol_info_tick(symbol).bid,
"deviation": 10,
"magic": 00000,
"comment": "insert comment",
"type_time": mt5.ORDER_TIME_GTC})

In the code above, mt5.ORDER_TYPE_SELL was used to initiate a market order to close out a long position. You would mt5.ORDER_TYPE_BUY to close out a short position.

Exiting Trades

to_close = mt5.order_send({
"action": mt5.TRADE_ACTION_DEAL,
"symbol": pair,
"volume": mt5.positions_get(symbol=symbol)[0].volume,
"type": mt5.ORDER_TYPE_SELL (to close long), or mt5.ORDER_TYPE_BUY (to close short)
"position": mt5.positions_get(symbol=symbol)[0].identifier,
#"price": mt5.symbol_info_tick(symbol).bid,
"deviation": 10,
"magic": 0000,
"comment": "insert comment",
"type_time": mt5.ORDER_TIME_GTC,

In the code above, mt5.ORDER_TYPE_SELL was used to initiate a market order to close out a long position. You would use mt5.ORDER_TYPE_BUY to close out a short position.

Limitation of MetaTrader5

One serious drawback of MetaTrader5 is that there is no support for MacOS and Linux yet. Mac and Linux users have to rely on some form of Windows virtualization.

Algorithmic Forex Trading With Python: Using MetaTrader5 Python Library For Accurate Data (2024)
Top Articles
Latest Posts
Article information

Author: Kerri Lueilwitz

Last Updated:

Views: 5399

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Kerri Lueilwitz

Birthday: 1992-10-31

Address: Suite 878 3699 Chantelle Roads, Colebury, NC 68599

Phone: +6111989609516

Job: Chief Farming Manager

Hobby: Mycology, Stone skipping, Dowsing, Whittling, Taxidermy, Sand art, Roller skating

Introduction: My name is Kerri Lueilwitz, I am a courageous, gentle, quaint, thankful, outstanding, brave, vast person who loves writing and wants to share my knowledge and understanding with you.