Requirements
Python 3.8+, the websockets library (pip install websockets) and a Deriv account with an API token.
Step 1: get an API token
Go to Settings → API Token in your Deriv account. Create a token with trading permissions.
Step 2: basic connection
Connect to the Deriv WebSocket and authorise with your token:
import asyncio
import json
import websockets
API_URL = "wss://ws.derivws.com/websockets/v3?app_id=1089"
async def connect():
async with websockets.connect(API_URL) as ws:
# Authorise
await ws.send(json.dumps({
"authorize": "YOUR_API_TOKEN"
}))
resp = await ws.recv()
print(json.loads(resp))
asyncio.run(connect())
Step 3: open a trade
Once authorised, you can open a contract (e.g. a CALL on R_100 for 5 minutes):
async def buy_contract(ws):
await ws.send(json.dumps({
"buy": 1,
"price": 10,
"parameters": {
"contract_type": "CALL",
"symbol": "R_100",
"duration": 5,
"duration_unit": "m",
"basis": "stake",
"amount": 1,
"currency": "USD"
}
}))
The full documentation is at api.deriv.com. For more on bots, read our guide on how to build a trading bot.
⚠️ Important
ALWAYS test on a demo account before using real money. Include risk management in your bot: a daily stop loss and a trade limit.
⚠️ Risk warning
Trading binary options carries significant risk. The majority of traders lose money. This site contains affiliate links.