Skip to content

Python

Important

All these source codes are only examples used for testing. We don't provide any guarantee or responsibility about it. Use these examples at your own risk.

Generate source code instantly

Select your desired programming language via our web interface to automatically retrieve code:

MTsocketAPI source code

Example 1: Get EURUSD price

getquote.py
import http.client

conn = http.client.HTTPConnection("127.0.0.1:81")

headers = { 'Accept': "application/json" }

conn.request("GET", "/v1/quote?symbol=EURUSD", headers=headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))

Run code:

python3 getquote.py

Example 2: Send buy order

sendorder.py
import http.client

conn = http.client.HTTPConnection("127.0.0.1:81")

payload = ""

headers = {
    'Content-Type': "application/json",
    'Accept': "application/json"
}

conn.request("POST", "/v1/order?symbol=EURUSD&volume=0.01&type=ORDER_TYPE_BUY", payload, headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))

Run code:

python3 sendorder.py

Example 3: Stream EURUSD price using websockets

streamprices.py
import websocket
import http.client

#Send REST API request
conn = http.client.HTTPConnection("127.0.0.1:81")

payload = ""

headers = {
    'Content-Type': "application/json",
    'Accept': "application/json"
}

conn.request("POST", "/v1/track/prices?symbols=EURUSD", payload, headers)

res = conn.getresponse()
data = res.read()

#Show the response to see if the request was successful
print(data.decode("utf-8"))

#Connect using websockets to start receiving prices
ws = websocket.WebSocket()
ws.connect("ws://127.0.0.1:81")

try:
    while True:
        message = ws.recv()
        print(f"Received: {message}")
except KeyboardInterrupt:
    print("\nClosing connection...")
finally:
    ws.close()

Run code:

python3 streamprices.py