Merge branch 'master' of https://github.com/khramkov/MQL5-JSON-API
This commit is contained in:
@@ -12,10 +12,10 @@
|
||||
|
||||
## About the Project
|
||||
|
||||
This project was developed to work as a server for Backtrader Python trading framework. It is based on ZeroMQ sockets and uses JSON format to communicate. But now it has grown to the independent project. You can use it with any language that has [ZeroMQ binding](http://zeromq.org/bindings:_start).
|
||||
This project was developed to work as a server for the Backtrader Python trading framework. It is based on ZeroMQ sockets and uses JSON format to communicate. But now it has grown to the independent project. You can use it with any language that has [ZeroMQ binding](http://zeromq.org/bindings:_start).
|
||||
|
||||
|
||||
Backtrader Python client located here: [Python Backtrader - Metaquotes MQL5 ](https://github.com/khramkov/MQL5-Backtrader-API)
|
||||
Backtrader Python client located here: [Python Backtrader - Metaquotes MQL5 ](https://github.com/khramkov/Backtrader-MQL5-API)
|
||||
|
||||
In development:
|
||||
* Add error handling to docs
|
||||
@@ -42,42 +42,42 @@ Tested on macOS Mojave / Windows 10 in Parallels Desktop container.
|
||||
The script uses four ZeroMQ sockets:
|
||||
|
||||
1. `System socket` - recives requests from client and replies 'OK'
|
||||
2. `Data socket` - pushes data to client depending on request via System socket.
|
||||
2. `Data socket` - pushes data to client depending on the request via System socket.
|
||||
3. `Live socket` - automatically pushes last candle when it closes.
|
||||
4. `Streaming socket` - automatically pushes last transaction info every time it happens.
|
||||
|
||||
The idea is to send requests via `System socket` and recieve results/errors via `Data socket`. Event handlers should be created for `Live socket` and `Streaming socket` because server sends data to theese sockets automatically. See examples in [Live data and streaming events](#live-data-and-streaming-events) section.
|
||||
The idea is to send requests via `System socket` and recieve results/errors via `Data socket`. Event handlers should be created for `Live socket` and `Streaming socket` because the server sends data to theese sockets automatically. See examples in [Live data and streaming events](#live-data-and-streaming-events) section.
|
||||
|
||||
`System socket` request uses default JSON dictionary:
|
||||
|
||||
```
|
||||
{
|
||||
"action": None,
|
||||
"actionType": None,
|
||||
"symbol": None,
|
||||
"chartTF": None,
|
||||
"fromDate": None,
|
||||
"toDate": None,
|
||||
"id": None,
|
||||
"magic": None,
|
||||
"volume": None,
|
||||
"price": None,
|
||||
"stoploss": None,
|
||||
"takeprofit": None,
|
||||
"expiration": None,
|
||||
"deviation": None,
|
||||
"comment": None
|
||||
"action": null,
|
||||
"actionType": null,
|
||||
"symbol": null,
|
||||
"chartTF": null,
|
||||
"fromDate": null,
|
||||
"toDate": null,
|
||||
"id": null,
|
||||
"magic": null,
|
||||
"volume": null,
|
||||
"price": null,
|
||||
"stoploss": null,
|
||||
"takeprofit": null,
|
||||
"expiration": null,
|
||||
"deviation": null,
|
||||
"comment": null
|
||||
}
|
||||
```
|
||||
Check out the available combinations of `action` and `actionType`:
|
||||
|
||||
action | actionType | Description |
|
||||
-----------|----------------------|----------------------------|
|
||||
CONFIG | None | Set script configuration |
|
||||
ACCOUNT | None | Get account settings |
|
||||
BALANCE | None | Get current balance |
|
||||
POSITIONS | None | Get current open positions |
|
||||
ORDERS | None | Get current open orders |
|
||||
CONFIG | null | Set script configuration |
|
||||
ACCOUNT | null | Get account settings |
|
||||
BALANCE | null | Get current balance |
|
||||
POSITIONS | null | Get current open positions |
|
||||
ORDERS | null | Get current open orders |
|
||||
HISTORY | DATA | Get data history |
|
||||
HISTORY | TRADES | Get trades history |
|
||||
TRADE | ORDER_TYPE_BUY | Buy market |
|
||||
@@ -209,7 +209,7 @@ All examples will be on Python 3. Lets create an instance of MetaTrader API clas
|
||||
api = MTraderAPI()
|
||||
```
|
||||
|
||||
First of all we should configure script `symbol` and `timeframe`. Live data stream will be configured to the seme params.
|
||||
First of all we should configure the script `symbol` and `timeframe`. Live data stream will be configured to the seme params.
|
||||
|
||||
``` python
|
||||
rep = api.construct_and_send(action="CONFIG", symbol="EURUSD", chartTF="M5")
|
||||
@@ -223,7 +223,9 @@ rep = api.construct_and_send(action="ACCOUNT")
|
||||
print(rep)
|
||||
```
|
||||
|
||||
Get historical data. `fromDate` should be in timestamp format. If `toDate` is `None` There are some issues:
|
||||
Get historical data. `fromDate` should be in timestamp format. The data will be loaded to the last candle if `toDate` is `None`. Notice, that the script sends the last unclosed candle too. You should delete it manually.
|
||||
|
||||
There are some issues:
|
||||
|
||||
- MetaTrader keeps historical data in cache. But when you make a request for the first time, MetaTrader downloads data from a broker. This operation can exceed `Data socket` timeout. It depends on your broker. Second request will be handeled quickly.
|
||||
- It takes 6-7 seconds to process `50000` M1 candles. It was tested on Windows 10 in Parallels Desktop container with 4 cores and 4GB RAM. So if you need more data there are three ways to handle it. 1) Increase `Data socket` timeout. 2) You can load data partially using `fromDate` and `toDate`. 3) You can use more powerfull hardware.
|
||||
@@ -307,15 +309,15 @@ There are only two variants of `Live socket` data. When everything is ok, the sc
|
||||
{"status":"CONNECTED","data":[1560780120,1.12186,1.12194,1.12186,1.12191,15.00000]}
|
||||
```
|
||||
|
||||
If the terminal has lost connection to market:
|
||||
If the terminal has lost connection to the market:
|
||||
|
||||
```
|
||||
{"status":"DISCONNECTED"}
|
||||
```
|
||||
|
||||
When the terminal reconnects to market, it sends last closed candle again. So you should update historical data. Make the `action="HISTORY"` request with `fromDate` equal to your last candle timestamp.
|
||||
When the terminal reconnects to the market, it sends the last closed candle again. So you should update your historical data. Make the `action="HISTORY"` request with `fromDate` equal to your last candle timestamp before disconnect.
|
||||
|
||||
`OnTradeTransaction` function is called when the trade transaction event occurs. `Streaming socket` sends `TRADE_TRANSACTION_REQUEST` data every time it happens. You can create and modify orders/positions in terminal manually and check expert logging tub for better understanding. Also see [MQL5 docs](https://www.mql5.com/en/docs/event_handlers/ontradetransaction).
|
||||
`OnTradeTransaction` function is called when the trade transaction event occurs. `Streaming socket` sends `TRADE_TRANSACTION_REQUEST` data every time it happens. Try to create and modify orders in the MQL5 terminal manually and check the expert logging tab for better understanding. Also see [MQL5 docs](https://www.mql5.com/en/docs/event_handlers/ontradetransaction).
|
||||
|
||||
`TRADE_TRANSACTION_REQUEST` request data:
|
||||
|
||||
@@ -357,4 +359,5 @@ When the terminal reconnects to market, it sends last closed candle again. So yo
|
||||
|
||||
## License
|
||||
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See `LICENSE` for more information.
|
||||
|
||||
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See `LICENSE` for more information.
|
||||
|
||||
Reference in New Issue
Block a user