Algorithmic Trading with Python and MT5: Building a Simple Strategy

Sebastian Ospina Valencia
3 min readAug 25, 2021

--

In the past article, we reviewed how to establish the connection between Python and Metatrader 5 using the Python library MetaTrader5. But Why Python in first place?. In my experience there was several adventages why use Python:

  1. Language simplicity over other languages like MQL, C, C+, C#
  2. Ability to connect to other sources of Data (Web scrapping for news, social media etc)
  3. Ability to incorporate Machine Learning models to the decission taking process
  4. Ability to connect to other external applications like PowerBI
  5. Speed in processing data

So using Python, allowed me to easily create new indicators, incorporate theories like chaos theory or ML to the robots increasing their chances to survive in the market.

Building the Trading Robot from Scratch

As we have mentioned in previous posts, a trading robot needs:

  1. Data to be processed
  2. Trading Signal
  3. Sent Order

This time we will use the EURUSD 1 hour frame data and the RSI indicator as a signal, to send an order to MT5. In order to do this, first we will get the data from Metatrader5 using the next lines of code:

name = “your number”

key = “your key”

serv = “Pepperstone-MT5-Live01” #Your server and Broker

path = r”C:\Program Files\MetaTrader 5 B\terminal64.exe”

symbol = “EURUSD”

lot = 0.02

# Get the Data

mt5.initialize( login = name, server = serv, password = key, path = path)

symbol_info=mt5.symbol_info(“EURUSD”)

rates = mt5.copy_rates_from_pos(“EURUSD”, mt5.TIMEFRAME_H1, 0, 1000)

rates_frame = pd.DataFrame(rates)

rates_frame[‘time’]=pd.to_datetime(rates_frame[‘time’], unit=’s’)

Then we will process the data to build our signal. To generate the trading signal we will use the TAlib Python library to build the RSI indicator using the closing price as input of the RSI indicator:

#Signal Processing

close = np.array(rates_frame[‘close’])

rsi_ind = RSI(close, timeperiod = 14)

array_length = len(rsi_ind)

last_element = rsi_ind[- 1]

last_rsi = last_element

array_length3 = len(close)

last_element3 = close[-1]

last_price = last_element3

price = mt5.symbol_info_tick(symbol).ask

deviation = 20

Then we generate the market order using as criteria the value of the las RSI: If it is above 70, price is overbought, we will sell. if it is below 30, price is oversell, we will buy:

#Decission Taking and Send Order

if last_rsi>70 :

request = {

“action”: mt5.TRADE_ACTION_DEAL,

“symbol”: symbol,

“volume”: lot,

“type”: mt5.ORDER_TYPE_SELL,

“sl”: price + 0.002,

“tp”: price — 0.005,

“deviation”: deviation,

“magic”: 202003,

“comment”: “My first robot”,

“type_time”: mt5.ORDER_TIME_GTC,

“type_filling”: mt5.ORDER_FILLING_IOC}

print(‘Order executed, Lots 0.01’)

mt5.order_send(request)

if 30>last_rsi :

request = {

“action”: mt5.TRADE_ACTION_DEAL,

“symbol”: symbol,

“volume”: lot,

“type”: mt5.ORDER_TYPE_BUY,

“sl”: price + 0.002,

“tp”: price — 0.005,

“deviation”: deviation,

“magic”: 202003,

“comment”: “My First Robot”,

“type_time”: mt5.ORDER_TIME_GTC,

“type_filling”: mt5.ORDER_FILLING_IOC}

print(‘Order executed 0.01 lots’)

mt5.order_send(request)

else:

print(‘Waiting for Market Signal’)

The last lines of code fits in a dictionary named request the execution order in MQL language that will be sent to metatrader5 using the command mt5.order_send(request).

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

For full code and description https://gitlab.com/elospieconomics/algorithmic-trading/-/blob/main/RSI_Robot.py

--

--

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