Skip to main content

WebSocket overview

The WebSocket streams live prices and every change on your account: orders, positions, deals, balance and margin. It's also where you learn the result of the orders you place over REST, so a bot should keep it open the whole time it trades.

Connect​

wss://api.onlytradeplatform.com/ws/v1/?session_id=<session_id>
Authorization: Bearer <access_token>

You need both values from your token response:

WhereMissing or wrong
access_tokenAuthorization: Bearer … header, or ?access_token= if your client can't set headersUpgrade refused with HTTP 401
session_id?session_id= query parameterAn unauthorized frame, then the socket closes

Once connected you're automatically subscribed to your own account. Account events start flowing without sending anything. For prices, send start_market_feed once (see Subscribing).

API-key (API_Trader) tokens can connect just like password sessions.

Frames​

The server sends two kinds of frames:

FrameContent
TextA JSON event: {"type": "…", "session_id": "…", "payload": …}
BinaryA price tick: one CSV line — see Events → Prices

What you send is always JSON text: {"type": "…", "payload": {…}}.

There are no request IDs. Actions don't get a direct reply. You see the result as the normal lifecycle events (e.g. position_create), or as an error frame whose payload.reason names the action that failed.

Heartbeat​

  • The server sends a WebSocket ping every 5 seconds. If it gets no pong within about 10 seconds it drops the connection. Standard clients answer pings automatically; just don't block your read loop.
  • You can also send the text 9 as an application-level ping; the server answers with the text 10.

Disconnects​

SituationWhat happens
Network drop, missed pongsThe socket closes without a close frame (you'll usually see code 1006). Reconnect.
Session killed (session limit, broker disconnect, trial expired)You receive session_client_logout, then the socket closes ~5 s later. The tokens are revoked — log in again.
You log out over RESTThe socket closes immediately.
You open a second socket with the same session_idThe first one is disconnected.

Events sent while you're disconnected are not replayed. After every reconnect, re-read your positions and orders over REST. The bot example shows the full reconnect loop.

Quick test​

websocat -t "wss://api.onlytradeplatform.com/ws/v1/?session_id=$SESSION" \
-H "Authorization: Bearer $TOKEN"
{"type":"start_market_feed","payload":{}}

Next: Subscribing & actions for what you can send, and Events for what you receive.