Skip to main content

Python API for MetaTrader 5 gateways

Project description


version pyversion platform license downloads Sponsor

Provides simplified, multithreaded, socket-based Python interfaces to MT5 gateways. PyMT5 requires a DevCartel MT5 gateway installed on the MT5 platform to work with PyMT5.



PyMT5 supports both Python 2 and 3. Simply install from PyPI using pip:

pip install pymt5


import pymt5

def onData(data):
    client = data.get('client_id')
    # Login
    if data.get('type') == '1':
        # Send heartbeat
        m.send(client, {'ver':'3','type':'6'})
        # Send login OK response
        m.send(client, {'ver':'3',

m = pymt5.PyMT5()
m.onConnected = onConnected
m.onDisconnected = onDisconnected
m.onData = onData

Checkout more message examples.


pymt5.PyMT5([host=''], [port=16838])
host: str
port: int
➥return: object
Starts a PyMT5 server and listening on a port defined by port.

>> m = pymt5.PyMT5()

Upon incoming connection from a gateway, PyMT5 stores client information in pymt5.requests in dict format as

Disconnects all MT5 gateway connections and stop the server.

>> m.stop()

data: dict
Sends a message to all connected gateways. Consider using this when sending market data.

>> #send a tick
>> m.broadcast({'ver':'3','type':'4','symbol':'EURUSD.TEST','bank':'dc','bid':'1.2661','ask':'1.2665','last':'1.2665','volume':'1','datetime':'0'})

pymt5.send(client_id, data)
client_id: int
data: dict
Sends a message to a connected gateway.

>> #send heartbeat
>> m.send(123145536110592, {'ver':'3','type':'6'})

client_id: int
Terminates a connection.

>> m.disconnect(123145536110592)

client_info: dict
A callback onConnected, if assigned, is called upon a successful connection from a client. Client information can be accessed from client_info's values as client_id, client_address and client_port.

>> def onConnected(client_info):
>>     print(str(client_info))
>>     # print {'client_port': 64941, 'client_address': '', 'client_id': 123145536110592}
>> m = pymt5.PyMT5()
>> m.onConnected = onConnected

client_info: dict
A callback onDisconnected, if assigned, is called upon a disconnection from a client. Client information can be accessed from client_info's values as client_id, client_address and client_port.

>> def onDisonnected(client_info):
>>     print(str(client_info))
>> m = pymt5.PyMT5()
>> m.onDisconnected = onDisconnected

data: dict
A callback onData, if assigned, is called upon receiving messages from gateways. See Data Format for more information.

>> def onData(data):
>>     print(json.dumps(data))
>> m = pymt5.PyMT5()
>> m.onData = onData

Data Format

Data is to be composed as a dict with key/value defined below to be sent and received from a gateway.

Data type Header Tags
Login 'ver':'3','type':1' 'login','password','res'
Logout 'ver':'3','type':2' None
Symbol 'ver':'3','type':3' 'index','symbol','path','description','page',
Tick 'ver':'3','type':4' 'symbol','bank','bid','ask','last','volume','datetime'
Order 'ver':'3','type':5' 'symbol','bank','bid','ask','last','volume','datetime',
Heartbeat 'ver':'3','type':6' None
Deal 'ver':'3','type':7' 'exchange_id','order','symbol','login','type_deal',
External Deal 'ver':'3','type':50' 'exchange_id','order','symbol','login','type_deal',




  • 8 August 2021
  • Fix parsing data buffer


  • 8 July 2019
  • Support for Python 3.7
  • Update support links
  • Add examples


  • 21 April 2018
  • Released on PyPI
  • Added README


  • 13 April 2018
  • Initial release

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

pymt5-1.3.0-py2.py3-none-any.whl (5.9 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page