Back to home

MetaAPI Docs

RPC Python Examples

Python examples for common user actions such as reading account data, sending orders and receiving realtime events.

Example 1: Get Account Info

import http.client
import json

conn = http.client.HTTPConnection("127.0.0.1", 6000)

payload = json.dumps({
    "jsonrpc": "2.0",
    "id": "account-1",
    "method": "get_account_info",
    "params": {}
})

headers = {
    "Authorization": "Bearer <auth_token>",
    "Content-Type": "application/json"
}

conn.request("POST", "/rpc", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Example 2: Send Buy Order

import http.client
import json

conn = http.client.HTTPConnection("127.0.0.1", 6000)

payload = json.dumps({
    "jsonrpc": "2.0",
    "id": "order-1",
    "method": "place_order",
    "params": {
        "symbol": "EURUSD",
        "volume": 0.01,
        "type": "ORDER_TYPE_BUY"
    }
})

headers = {
    "Authorization": "Bearer <auth_token>",
    "Content-Type": "application/json"
}

conn.request("POST", "/rpc", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Example 3: Stream Events Using WebSocket

import json
import websocket

ws = websocket.WebSocket()
ws.connect(
    "ws://127.0.0.1:6000/ws",
    header=["Authorization: Bearer <auth_token>"]
)

try:
    while True:
        message = ws.recv()
        print("Received:", message)
except KeyboardInterrupt:
    print("Closing connection...")
finally:
    ws.close()