Skip to content

Basic Usage with TELNET

MTsocketAPI opens two TCP ports by default:

  • TCP 77 (Commands): You can use this port for send commands and receive MTsocketAPI replies.
  • TCP 78 (Data): You can use this port to receive tick prices 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)

You can connect to default port TCP/77 to start sending queries to MTsocketAPI using TELNET:

telnet localhost 77
Then paste the following code:

{"MSG": "HELP"}

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","ORDER_LIST","TRADE_HISTORY","EXIT"],"ERROR_ID":0,"ERROR_DESCRIPTION":"The operation completed successfully"}

If you want to get more information about each command:

{"MSG": "HELP","COMMAND":"QUOTE"}

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"
}
We can see that the command QUOTE has a mandatory tag named SYMBOL and this tag accepts strings only.

Examples

Example 1: Get Price from a symbol

If we want to view the current EUR/USD price:

{"MSG": "QUOTE", "SYMBOL": "EURUSD"}
Important

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":0.99739,
    "BID":0.99736,
    "FLAGS":2,
    "TIME":"2022.04.11 12:15:31.0",
    "VOLUME":0,
    "ERROR_ID":0,
    "ERROR_DESCRIPTION":"no error"
}
Tip

You can also subscribe to one or more symbols and MTsocketAPI will start streaming tick (TRACK_PRICES command) or OHLC (TRACK_OHLC command) data over the port TCP 78 (data). See the following Examples.

Example 2: Send new Order

You want to place a 0.01 lots Buy Order (at market) on EURUSD:

{"MSG": "ORDER_SEND","SYMBOL": "EURUSD","TYPE":"ORDER_TYPE_BUY","VOLUME": 0.01}

Reply:

{
    "MSG":"ORDER_SEND",
    "COMMENT":"Request executed",
    "DEAL":"2008856776",
    "ORDER":"2012142352",
    "PRICE":0.99746000,
    "REQUEST_ID":"1",
    "RETCODE":"10009",
    "RETCODE_EXTERNAL":0,
    "VOLUME":0.01000000,
    "ERROR_ID":0,
    "ERROR_DESCRIPTION":"The operation completed successfully"
}
As you can see, we received the ticket number assigned to our order.

Note

SEND_ORDER command accepts limit orders, SL, TP... Please check the documentation here.

Example 3: Close Orders

Now if we want to close our newly created order:

{"MSG": "ORDER_CLOSE", "TICKET": 2012142352}

And this is the reply:

{
   "MSG":"ORDER_CLOSE",
   "TICKET":2012142352,
   "TYPE":"FULLY_CLOSED",
   "VOLUME":0.01,
   "ERROR_ID":0,
   "ERROR_DESCRIPTION":"The operation completed successfully"
}

Example 4: Get Order History

Finally, let's check the closed price and profit for this order:

{"MSG":"TRADE_HISTORY","FROM_DATE":"2022/09/19 09:20:00", "TO_DATE":"2022/09/19 16:30:00", "MODE":"POSITIONS"}

Tip

You can use any date format accepted by MT5. More info.

Reply:

{
    "MSG":"TRADE_HISTORY",
    "POSITIONS":[
        {
            "OPEN_TIME":"2022.09.19 12:49:05.146",
            "SYMBOL":"EURUSD",
            "TICKET":2012142352,
            "TYPE":"BUY",
            "VOLUME":0.01,
            "PRICE_OPEN":0.99746000,
            "MAGIC":0,
            "COMMENT":null,
            "CLOSE_TIME":"2022.09.19 12:50:22.145",
            "PRICE_CLOSE":0.99718000,
            "PROFIT":-0.28,
            "COMMISSION":-0.06,
            "SWAP":0.00,
            "SL":0.00000,
            "TP":0.00000
        }
    ],
    "ERROR_ID":0,
    "ERROR_DESCRIPTION":"The operation completed successfully"
}
Now we disconnect from MTsocketAPI softly:
{"MSG": "EXIT"}

Reply:

Bye

Here you can see the Full API Command List