Basic Usage with TELNET
MTsocketAPI opens two TCP ports by default:
- TCP 77 (Commands): This port can be used for send commands and receive MTsocketAPI replies.
- TCP 78 (Data): This port can be used to stream tick or OHLC prices for the selected Timeframe.
Note
You can connect multiple clients to TCP 78 (data) port.
You can use any tool, script or application to start sending commands. For example:
- Telnet command
- PuTTY or any other telnet tool
- Java
- .NET Core / .NET Framework / PowerShell
- Python
- C / C++
- NodeJS
- R
- Any software that can use sockets (TCP)
First steps
You can connect to default port TCP/77 to start sending queries to MTsocketAPI using TELNET:
Then paste the following code:And finally hit Enter and you will get a list of available commands:
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
{"MSG":"HELP"}
{"MSG":"HELP","AVAILABLE_COMMANDS":["TRACK_PRICES","TRACK_OHLC","CUSTOM_INDICATOR","MA_INDICATOR","ATR_INDICATOR","SYMBOL_INFO","SYMBOL_LIST","TERMINAL_INFO","ACCOUNT_STATUS","QUOTE","ORDER_SEND","ORDER_MODIFY","ORDER_CLOSE","OPENED_POSITIONS","TRADE_HISTORY","EXIT"],"ERROR_ID":0,"ERROR_DESCRIPTION":"no error"}
If you want to get more information about each command:
Reply:
{
"MSG":"HELP",
"COMMAND":"QUOTE",
"DESCRIPTION":"Get Quote from a SYMBOL",
"MANDATORY_TAGS":["SYMBOL (String)"],
"OPTIONAL_TAGS":[null],
"ERROR_ID":0,
"ERROR_DESCRIPTION":"no error"
}
Let's see and example:
Example 1: Get Price
If we want to view the current EUR/USD price:
Warning
Please check with your broker that the symbol name exists. Some brokers use EURUSD, EURUSD.fx, EUR/USD... You can see the symbols names using SYMBOL_LIST command.
Reply:
{
"MSG":"QUOTE",
"SYMBOL":"EURUSD",
"ASK":1.09047,
"BID":1.09045,
"FLAGS":6,
"TIME":"2022.04.11 12:15:31.0",
"VOLUME":0,
"ERROR_ID":0,
"ERROR_DESCRIPTION":"no error"
}
Note
You can also subscribe to one or more symbols and MTsocketAPI will start streaming tick or OHLC data over the port TCP78 (data). To stream tick data use TRACK_PRICES command and to stream OHLC data use TRACK_OHLC command.
Example 2: Send Order
You want to place a 0.01 lots Buy Order (at market) on EURUSD:
Reply:
As you can see you received the ticket number assigned to your order.Example 3: Close Order
Now if we want to close our newly created order:
And this is the reply:
{
"MSG":"ORDER_CLOSE",
"TICKET":47094374,
"TYPE":"FULLY_CLOSED",
"ERROR_ID":0,
"ERROR_DESCRIPTION":"no error"
}
Example 4: Get Order History
Finally, let's check the closed price and profit for this order:
Note
You can use any date format accepted by MT4. More info.
Result:
{
"MSG":"TRADE_HISTORY",
"TRADES":[
{
"SYMBOL":"EURUSD",
"MAGIC":0,
"TICKET":47094374,
"OPEN_TIME":"2022.04.11 12:26:56",
"CLOSE_TIME":"2022.04.11 12:30:07",
"OPEN_PRICE":1.09026,
"CLOSE_PRICE":1.09006,
"TYPE":0,
"LOTS":0.01,
"STOP_LOSS":0.00000,
"TAKE_PROFIT":0.00000,
"SWAP":0.00,
"COMMISSION":0.00,
"COMMENT":null,
"PROFIT":-0.20
}
],
"ERROR_ID":0,
"ERROR_DESCRIPTION":"no error"
}
Here you can see the Full API Command List