Closing a position is performed over WebSocket.
This command instructs the engine to place a LIMIT IOC order on the opposite side of your current position, pegged near the mark price to maximise the chance of a complete close-out.
The engine automatically sizes the order to match your open position, so you always close in full.
- Endpoint:
wss://trade.qfex.com?api_key=YOUR_API_KEY
- Authenticate within 1 minute of connecting.
1) Authenticate
Send this immediately after you connect:
{ "type": "auth", "params": { "api_key": "YOUR_API_KEY" } }
2) Close a Position
{
"type": "close_position",
"params": {
"symbol": "AAPL-USD"
}
}
Parameters
| Field | Type | Required | Description |
|---|
symbol | string | ✅ | Market symbol whose position you want to close (e.g. AAPL-USD). |
⚠️ Notes:
- The generated order is always
LIMIT + IOC, reduce-only, and inherits the appropriate closing side.
- Subscribe to
order_responses to receive fills and final status updates.
Sample Code
# Python (websocket-client)
# pip install websocket-client
import json
import websocket
API_KEY = "YOUR_API_KEY"
def send(ws, obj):
ws.send(json.dumps(obj))
def on_open(ws):
# 1) Authenticate
send(ws, {"type": "auth", "params": {"api_key": API_KEY}})
# 2) Close position
send(ws, {
"type": "close_position",
"params": {
"symbol": "AAPL-USD",
},
})
ws = websocket.WebSocketApp(
"wss://trade.qfex.com?api_key=YOUR_API_KEY",
on_open=on_open,
on_message=lambda _, m: print(m),
on_error=lambda _, e: print("Error:", e),
on_close=lambda *_: print("Closed"),
)
ws.run_forever()
Example Responses
You will receive an order_response acknowledgement immediately, followed by fill updates (if any).
{
"order_response": {
"order_id": "5b309929-206f-40ec-804d-cbe46e81afc1",
"symbol": "AAPL-USD",
"status": "ACK",
"quantity": 2.0,
"price": 199.8,
"take_profit": 0.0,
"stop_loss": 0.0,
"side": "SELL",
"type": "LIMIT",
"time_in_force": "IOC",
"user_id": "0020ce8e-eaee-480e-8d7f-b9241d756ee5",
"client_order_id": "",
"quantity_remaining": 2.0
}
}
{
"order_response": {
"order_id": "5b309929-206f-40ec-804d-cbe46e81afc1",
"symbol": "AAPL-USD",
"status": "FILLED",
"quantity": 2.0,
"price": 199.75,
"take_profit": 0.0,
"stop_loss": 0.0,
"side": "SELL",
"type": "LIMIT",
"time_in_force": "IOC",
"user_id": "0020ce8e-eaee-480e-8d7f-b9241d756ee5",
"client_order_id": "",
"quantity_remaining": 0.0
}
}
Any unfilled remainder is cancelled automatically because the engine always uses
IOC. If you have no open position for the symbol, the command is rejected.