Python
MT5 in Python, without Windows.
No MetaTrader5 package, no VPS, no EA. Python speaks to the account over HTTPS like any other API. The examples use onlyrequests.
1. Install and connect
pip install requests
import os, requests
BASE = "https://api.socketrade.com"
HEAD = {"X-API-Key": os.environ["SOCKETRADE_KEY"]}
usage = requests.get(f"{BASE}/v2/usage", headers=HEAD, timeout=15).json()
print(usage) # {"seats": 1, "seatsUsed": 0}
account = requests.post(
f"{BASE}/v2/accounts",
headers=HEAD,
json={
"accountNumber": "41299492",
"accountPassword": os.environ["MT5_PASSWORD"],
"accountServerHost": "Deriv-Demo",
"name": "Demo",
},
timeout=30,
).json()
account_id = account["id"]2. Read the account
status = requests.get(f"{BASE}/v2/{account_id}/status", headers=HEAD, timeout=15).json()
positions = requests.get(f"{BASE}/v2/{account_id}/positions", headers=HEAD, timeout=15).json()
history = requests.get(f"{BASE}/v2/{account_id}/history", headers=HEAD, timeout=15).json()
print(status["account"]["balance"], status["status"])
for p in positions["positions"]:
print(p["symbol"], p["volume"], p["profit"])3. Place and close an order
Use a demo first, and a symbol the account can actually trade. On a weekend, forex is closed; use a synthetic index.
order = requests.post(
f"{BASE}/v2/{account_id}/OrderSend",
headers=HEAD,
json={"symbol": "EURUSD", "operation": 0, "volume": 0.01},
timeout=30,
).json()
print(order) # {"ticket": 4789544305, ...}
close = requests.post(
f"{BASE}/v2/{account_id}/PositionClose",
headers=HEAD,
json={"ticket": order["ticket"]},
timeout=30,
).json()4. Retry the reads, not the orders
Retry GETs on 429 and 5xx. Do not blind-retry an order: a timeout can mean the order reached the broker. Read positions before you send it again.
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
session = requests.Session()
session.mount("https://", HTTPAdapter(max_retries=Retry(
total=3, backoff_factor=0.5,
status_forcelist=[429, 500, 502, 503, 504],
allowed_methods=["GET"],
)))Need live prices? The WebSocket is documented on its own page. It streams bid/ask ticks; positions and history stay REST reads.
The WebSocket contract →