# PyMT5
[![version](https://img.shields.io/pypi/v/pymt5.svg)](https://pypi.org/project/pymt5)
[![pyversion](https://img.shields.io/pypi/pyversions/pymt5.svg)](#)
[![platform](https://img.shields.io/badge/platform-linux|%20win-lightgray.svg)](#platform-availability)
[![license](https://img.shields.io/pypi/l/pymt5.svg)](https://github.com/devcartel/pymt5/blob/master/LICENSE.txt)
[![downloads](https://img.shields.io/pypi/dm/pymt5.svg)](https://pypi.org/project/pymt5)
[![Sponsor](https://img.shields.io/badge/Sponsor%20PyMT5-%2419.99%2Fmonth-orange.svg?style=social&logo=paypal)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=CKHUBNTFNDCB8)
Provides simplified, multithreaded, socket-based Python interfaces to MT5 gateways. PyMT5 requires a [DevCartel MT5 gateway](https://github.com/devcartel/devcartelgateway64) installed on the MT5 platform to work with PyMT5.
<p align="center">
<img src="https://user-images.githubusercontent.com/3415706/119602202-db221780-be14-11eb-859b-356969930613.png" alt="application" width="800"/>
</p>
## Installation
PyMT5 supports both Python 2 and 3. Simply install from [PyPI](https://pypi.org/project/pymt5) using `pip`:
pip install pymt5
## Example
```python
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',
'type':'1',
'login':data.get('login'),
'password':data.get('password'),
'res':'0'})
m = pymt5.PyMT5()
m.onConnected = onConnected
m.onDisconnected = onDisconnected
m.onData = onData
```
Checkout more message [examples](https://github.com/devcartel/pymt5/tree/master/examples).
## API
__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
__pymt5.stop()__
Disconnects all MT5 gateway connections and stop the server.
>> m.stop()
__pymt5.broadcast(_data_)__
_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(<i>client_id</i>, _data_)__
<i>client_id: int</i>
_data: dict_
Sends a message to a connected gateway.
>> #send heartbeat
>> m.send(123145536110592, {'ver':'3','type':'6'})
__pymt5.disconnect(<i>client_id</i>)__
<i>client_id: int</i>
Terminates a connection.
>> m.disconnect(123145536110592)
__pymt5.onConnected(<i>client_info</i>)__
<i>client_info: dict</i>
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': '127.0.0.1', 'client_id': 123145536110592}
>>
>> m = pymt5.PyMT5()
>> m.onConnected = onConnected
__pymt5.onDisconnected(<i>client_info</i>)__
<i>client_info: dict</i>
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
__pymt5.onData(_data_)__
_data: dict_
A callback `onData`, if assigned, is called upon receiving messages from gateways. See [Data Format](#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'`,<br/>`'currency_base'`,`'currency_profit'`,`'currency_margin'`,<br/>`'digits'`,`'tick_flags'`,`'calc_mode'`,`'exec_mode'`,<br/>`'chart_mode'`,`'fill_flags'`,`'expir_flags'`,`'tick_value'`,<br/>`'tick_size'`,`'contract_size'`,`'volume_min'`,`'volume_max'`,<br/>`'volume_step'`,`'market_depth'`,`'margin_flags'`,<br/>`'margin_initial'`,`'margin_maintenance'`,`'margin_long'`,<br/>`'margin_short'`,`'margin_limit'`,<br/>`'margin_stop'`,`'margin_stop_limit'`,`'settlement_price'`,<br/>`'price_limit_max'`,`'price_limit_min'`,`'time_start'`,<br/>`'time_expiration'`,`'trade_mode'` |
| Tick | `'ver':'3','type':4'` | `'symbol'`,`'bank'`,`'bid'`,`'ask'`,`'last'`,`'volume'`,`'datetime'` |
| Order | `'ver':'3','type':5'` | `'symbol'`,`'bank'`,`'bid'`,`'ask'`,`'last'`,`'volume'`,`'datetime'`,<br/>`'order_action'`,`'state'`,`'order'`,`'exchange_id'`,<br/>`'custom_data'`,`'request_id'`,`'symbol'`,`'login'`,<br/>`'type_order'`,`'type_time'`,`'type_fill'`,`'action'`,`'price_order'`,<br/>`'price_sl'`,`'price_tp'`,`'price_tick_bid'`,`'price_tick_ask'`,<br/>`'volume'`,`'expiration_time'`,`'result'` |
| Heartbeat | `'ver':'3','type':6'` | _None_ |
| Deal | `'ver':'3','type':7'` | `'exchange_id'`,`'order'`,`'symbol'`,`'login'`,`'type_deal'`,<br/>`'volume'`,`'volume_rem'`,`'price'`,`'position'` |
| External Deal | `'ver':'3','type':50'` | `'exchange_id'`,`'order'`,`'symbol'`,`'login'`,`'type_deal'`,<br/>`'volume'`,`'volume_rem'`,`'price'`,`'datetime'`,`'comment'` |
## Support
* Get a [DevCartel MT5 Gateway](http://devcartel.com/devcartelgateway64) in order to work with PyMT5
* Report an issue in [issue tracker](https://github.com/devcartel/pymt5/issues)
## Changelog
1.4.0
* 30 October 2022
* Fix potential data loss due to data fragmentation
1.3.0
* 8 August 2021
* Fix parsing data buffer
1.2.0
* 8 July 2019
* Support for Python 3.7
* Update support links
* Add examples
1.1.0
* 21 April 2018
* Released on PyPI
* Added README
1.0.0
* 13 April 2018
* Initial release
Raw data
{
"_id": null,
"home_page": "https://github.com/devcartel/pymt5",
"name": "pymt5",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "metatrader,metaquotes,mt5,gateway,api,python",
"author": "DevCartel",
"author_email": "support@devcartel.com",
"download_url": "https://files.pythonhosted.org/packages/43/55/fcaf12e4e8d24216e36ed0b26e2df23e82fa377126d0787c4ed2cb61dd3c/pymt5-1.4.0.tar.gz",
"platform": null,
"description": "# PyMT5\n[![version](https://img.shields.io/pypi/v/pymt5.svg)](https://pypi.org/project/pymt5)\n[![pyversion](https://img.shields.io/pypi/pyversions/pymt5.svg)](#)\n[![platform](https://img.shields.io/badge/platform-linux|%20win-lightgray.svg)](#platform-availability)\n[![license](https://img.shields.io/pypi/l/pymt5.svg)](https://github.com/devcartel/pymt5/blob/master/LICENSE.txt)\n[![downloads](https://img.shields.io/pypi/dm/pymt5.svg)](https://pypi.org/project/pymt5)\n[![Sponsor](https://img.shields.io/badge/Sponsor%20PyMT5-%2419.99%2Fmonth-orange.svg?style=social&logo=paypal)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=CKHUBNTFNDCB8)\n\nProvides simplified, multithreaded, socket-based Python interfaces to MT5 gateways. PyMT5 requires a [DevCartel MT5 gateway](https://github.com/devcartel/devcartelgateway64) installed on the MT5 platform to work with PyMT5.\n\n<p align=\"center\">\n <img src=\"https://user-images.githubusercontent.com/3415706/119602202-db221780-be14-11eb-859b-356969930613.png\" alt=\"application\" width=\"800\"/>\n</p>\n\n## Installation\nPyMT5 supports both Python 2 and 3. Simply install from [PyPI](https://pypi.org/project/pymt5) using `pip`:\n\n pip install pymt5\n\n## Example\n```python\nimport pymt5\n\ndef onData(data):\n client = data.get('client_id')\n # Login\n if data.get('type') == '1':\n # Send heartbeat\n m.send(client, {'ver':'3','type':'6'})\n # Send login OK response\n m.send(client, {'ver':'3',\n 'type':'1',\n 'login':data.get('login'),\n 'password':data.get('password'),\n 'res':'0'})\n\nm = pymt5.PyMT5()\nm.onConnected = onConnected\nm.onDisconnected = onDisconnected\nm.onData = onData\n\n```\n\nCheckout more message [examples](https://github.com/devcartel/pymt5/tree/master/examples).\n\n## API\n__pymt5.PyMT5([_host=''_], [_port=16838_])__ \n_host: str_ \n_port: int_ \n_\u27a5return: object_ \nStarts a PyMT5 server and listening on a port defined by _port_.\n\n >> m = pymt5.PyMT5()\n\nUpon incoming connection from a gateway, PyMT5 stores client information in `pymt5.requests` in dict format as\n\n__pymt5.stop()__ \nDisconnects all MT5 gateway connections and stop the server.\n\n >> m.stop()\n\n__pymt5.broadcast(_data_)__ \n_data: dict_ \nSends a message to all connected gateways. Consider using this when sending market data.\n\n >> #send a tick\n >> m.broadcast({'ver':'3','type':'4','symbol':'EURUSD.TEST','bank':'dc','bid':'1.2661','ask':'1.2665','last':'1.2665','volume':'1','datetime':'0'})\n\n__pymt5.send(<i>client_id</i>, _data_)__ \n<i>client_id: int</i> \n_data: dict_ \nSends a message to a connected gateway.\n\n >> #send heartbeat\n >> m.send(123145536110592, {'ver':'3','type':'6'})\n\n__pymt5.disconnect(<i>client_id</i>)__ \n<i>client_id: int</i> \nTerminates a connection.\n\n >> m.disconnect(123145536110592)\n\n__pymt5.onConnected(<i>client_info</i>)__ \n<i>client_info: dict</i> \nA 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`.\n\n >> def onConnected(client_info):\n >> print(str(client_info))\n >> # print {'client_port': 64941, 'client_address': '127.0.0.1', 'client_id': 123145536110592}\n >>\n >> m = pymt5.PyMT5()\n >> m.onConnected = onConnected\n\n__pymt5.onDisconnected(<i>client_info</i>)__ \n<i>client_info: dict</i> \nA 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`.\n\n >> def onDisonnected(client_info):\n >> print(str(client_info))\n >>\n >> m = pymt5.PyMT5()\n >> m.onDisconnected = onDisconnected\n\n__pymt5.onData(_data_)__ \n_data: dict_ \nA callback `onData`, if assigned, is called upon receiving messages from gateways. See [Data Format](#data-format) for more information.\n\n >> def onData(data):\n >> print(json.dumps(data))\n >>\n >> m = pymt5.PyMT5()\n >> m.onData = onData\n\n## Data Format\nData is to be composed as a dict with key/value defined below to be sent and received from a gateway.\n\n| Data type | Header | Tags |\n| ------------- | ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |\n| Login | `'ver':'3','type':1'` | `'login'`,`'password'`,`'res'` |\n| Logout | `'ver':'3','type':2'` | _None_ |\n| Symbol | `'ver':'3','type':3'` | `'index'`,`'symbol'`,`'path'`,`'description'`,`'page'`,<br/>`'currency_base'`,`'currency_profit'`,`'currency_margin'`,<br/>`'digits'`,`'tick_flags'`,`'calc_mode'`,`'exec_mode'`,<br/>`'chart_mode'`,`'fill_flags'`,`'expir_flags'`,`'tick_value'`,<br/>`'tick_size'`,`'contract_size'`,`'volume_min'`,`'volume_max'`,<br/>`'volume_step'`,`'market_depth'`,`'margin_flags'`,<br/>`'margin_initial'`,`'margin_maintenance'`,`'margin_long'`,<br/>`'margin_short'`,`'margin_limit'`,<br/>`'margin_stop'`,`'margin_stop_limit'`,`'settlement_price'`,<br/>`'price_limit_max'`,`'price_limit_min'`,`'time_start'`,<br/>`'time_expiration'`,`'trade_mode'` |\n| Tick | `'ver':'3','type':4'` | `'symbol'`,`'bank'`,`'bid'`,`'ask'`,`'last'`,`'volume'`,`'datetime'` |\n| Order | `'ver':'3','type':5'` | `'symbol'`,`'bank'`,`'bid'`,`'ask'`,`'last'`,`'volume'`,`'datetime'`,<br/>`'order_action'`,`'state'`,`'order'`,`'exchange_id'`,<br/>`'custom_data'`,`'request_id'`,`'symbol'`,`'login'`,<br/>`'type_order'`,`'type_time'`,`'type_fill'`,`'action'`,`'price_order'`,<br/>`'price_sl'`,`'price_tp'`,`'price_tick_bid'`,`'price_tick_ask'`,<br/>`'volume'`,`'expiration_time'`,`'result'` |\n| Heartbeat | `'ver':'3','type':6'` | _None_ |\n| Deal | `'ver':'3','type':7'` | `'exchange_id'`,`'order'`,`'symbol'`,`'login'`,`'type_deal'`,<br/>`'volume'`,`'volume_rem'`,`'price'`,`'position'` |\n| External Deal | `'ver':'3','type':50'` | `'exchange_id'`,`'order'`,`'symbol'`,`'login'`,`'type_deal'`,<br/>`'volume'`,`'volume_rem'`,`'price'`,`'datetime'`,`'comment'` |\n\n## Support\n* Get a [DevCartel MT5 Gateway](http://devcartel.com/devcartelgateway64) in order to work with PyMT5\n* Report an issue in [issue tracker](https://github.com/devcartel/pymt5/issues)\n\n## Changelog\n1.4.0\n* 30 October 2022\n* Fix potential data loss due to data fragmentation\n \n1.3.0\n* 8 August 2021\n* Fix parsing data buffer\n\n1.2.0\n* 8 July 2019\n* Support for Python 3.7\n* Update support links\n* Add examples\n\n1.1.0\n* 21 April 2018\n* Released on PyPI\n* Added README\n\n1.0.0\n* 13 April 2018\n* Initial release\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Python API for MetaTrader 5 gateways",
"version": "1.4.0",
"split_keywords": [
"metatrader",
"metaquotes",
"mt5",
"gateway",
"api",
"python"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "52d977ce25f9c0a2465804e36963a0adc601b31a542d477f69f5d5b5a98118f7",
"md5": "ca428e39eb2bdf9a5165e798bb7f8276",
"sha256": "b077cf04d65d2a0f85031c881f62df66722e2f9154fd170979952a3cf8886e12"
},
"downloads": -1,
"filename": "pymt5-1.4.0-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "ca428e39eb2bdf9a5165e798bb7f8276",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 5936,
"upload_time": "2023-02-02T03:34:28",
"upload_time_iso_8601": "2023-02-02T03:34:28.208962Z",
"url": "https://files.pythonhosted.org/packages/52/d9/77ce25f9c0a2465804e36963a0adc601b31a542d477f69f5d5b5a98118f7/pymt5-1.4.0-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "4355fcaf12e4e8d24216e36ed0b26e2df23e82fa377126d0787c4ed2cb61dd3c",
"md5": "d38f0bdc6e099e4fce4bea3508967d8d",
"sha256": "b4fedb8178c6952da341e644d99d6932dac20a4f9a31a7936685715e89ff05df"
},
"downloads": -1,
"filename": "pymt5-1.4.0.tar.gz",
"has_sig": false,
"md5_digest": "d38f0bdc6e099e4fce4bea3508967d8d",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 6306,
"upload_time": "2023-02-02T03:34:29",
"upload_time_iso_8601": "2023-02-02T03:34:29.661688Z",
"url": "https://files.pythonhosted.org/packages/43/55/fcaf12e4e8d24216e36ed0b26e2df23e82fa377126d0787c4ed2cb61dd3c/pymt5-1.4.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-02-02 03:34:29",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "devcartel",
"github_project": "pymt5",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "pymt5"
}