> ## Documentation Index
> Fetch the complete documentation index at: https://docs.qfex.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Subscribe — Positions

Subscribe to the `positions` channel to receive position snapshots, one
`position_response` per symbol. Periodic snapshots are published every 60 seconds,
with additional event-driven updates after trades, funding, margin changes,
and order-reservation changes.

## Update timing and initial snapshot

After a new subscription, the gateway sends `{"subscribed":"positions"}` and
immediately queues one `position_response` for each cached position. You do not
wait for the next periodic refresh. If no positions are cached, only the
subscription acknowledgement is sent until position data becomes available.

Order-reservation updates are coalesced per account on an approximately
one-second cadence; other account events can trigger an earlier snapshot.
Delivery is asynchronous, with no one-message-per-order guarantee.
The 60-second interval is the periodic refresh cadence, not a maximum delivery
latency guarantee.

## 1) Authenticate

```json theme={null}
{
  "type": "auth",
  "params": {
    "hmac": {
      "public_key": "qfex_pub_xxxxx",
      "nonce": "c0ffee...",
      "unix_ts": 1760545414,
      "signature": "5f2e..."
    }
  }
}
```

## 2) Subscribe

```json theme={null}
{ "type": "subscribe", "params": { "channels": ["positions"] } }
```

## Sample Code

```python python theme={null}
# Python (websocket-client)
import json, websocket

API_KEY = "YOUR_API_KEY"

def send(ws, obj): ws.send(json.dumps(obj))

def on_open(ws):
    # https://docs.qfex.com/websocket/channels/trade/authenticate#how-it-works
    send(ws, {"type": "auth", "params": { "hmac": { "public_key": "qfex_pub_xxxxx", "nonce": "c0ffee...", "unix_ts": 1760545414, "signature": "5f2e..." }}})
    send(ws, {"type": "subscribe", "params": {"channels": ["positions"]}})

ws = websocket.WebSocketApp(
    "wss://trade.qfex.com?api_key=YOUR_API_KEY",
    on_open=on_open,
    on_message=lambda _, m: print("Message:", m),
)
ws.run_forever()
```

```javascript node theme={null}
// Node.js (ws)
import WebSocket from "ws";
const API_KEY = "YOUR_API_KEY";
const ws = new WebSocket("wss://trade.qfex.com?api_key=YOUR_API_KEY");

ws.on("open", () => {
  ws.send(JSON.stringify({ type: "auth", params: { api_key: API_KEY } }));
  ws.send(
    JSON.stringify({ type: "subscribe", params: { channels: ["positions"] } })
  );
});

ws.on("message", (msg) => console.log("Message:", msg.toString()));
```

```go go theme={null}
// Go (gorilla/websocket)
package main

import (
	"log"
	"github.com/gorilla/websocket"
)

func mustWriteJSON(c *websocket.Conn, v any) {
	if err := c.WriteJSON(v); err != nil { log.Fatal("write:", err) }
}

func main() {
	c, _, err := websocket.DefaultDialer.Dial("wss://trade.qfex.com?api_key=YOUR_API_KEY", nil)
	if err != nil { log.Fatal("dial:", err) }
	defer c.Close()

	mustWriteJSON(c, map[string]any{
		"type": "auth",
		"params": map[string]any{"hmac": map[string]any{"public_key": "qfex_pub_xxxxx", "nonce": "c0ffee...", "unix_ts": 1760545414, "signature": "5f2e..."}}
	})

	mustWriteJSON(c, map[string]any{
		"type": "subscribe",
		"params": map[string]any{"channels": []string{"positions"}},
	})

	for {
		_, data, err := c.ReadMessage()
		if err != nil { log.Fatal("read:", err) }
		log.Println(string(data))
	}
}
```

```java java theme={null}
// Java (OkHttp WebSocket)
import java.util.concurrent.TimeUnit;
import okhttp3.*;

public class PositionsSub {
  public static void main(String[] args) {
    OkHttpClient client = new OkHttpClient.Builder()
        .pingInterval(20, TimeUnit.SECONDS).build();

    Request req = new Request.Builder().url("wss://trade.qfex.com?api_key=YOUR_API_KEY").build();

    WebSocketListener li = new WebSocketListener() {
      @Override public void onOpen(WebSocket ws, Response r) {
        ws.send("{\"type\": \"auth\", \"params\": { \"hmac\": { \"public_key\": \"qfex_pub_xxxxx\", \"nonce\": \"c0ffee...\..\", \"unix_ts\": 1760545414, \"signature\": \"5f2e...\" }}}");
        ws.send("{\"type\":\"subscribe\",\"params\":{\"channels\":[\"positions\"]}}");
      }
      @Override public void onMessage(WebSocket ws, String text) {
        System.out.println(text);
      }
    };

    client.newWebSocket(req, li);
    try { Thread.sleep(Long.MAX_VALUE); } catch (InterruptedException ignored) {}
  }
}
```

## Example Responses

```json theme={null}
{ "subscribed": "positions" }
```

```json theme={null}
{
  "position_response": {
    "id": "c003ad60-ded4-401b-9b4c-e255a53d32ea",
    "symbol": "AAPL-USD",
    "position": 0.0,
    "margin_alloc": 0.0,
    "realised_pnl": 0.0,
    "unrealised_pnl": 0.0,
    "net_funding": 0.0,
    "open_orders": 0.0,
    "open_quantity": 0.0,
    "leverage": 20.0,
    "initial_margin": 0.05,
    "maintenance_margin": 0.03333333333333333,
    "average_price": 0.0
  }
}
```

> [!NOTE]
>
> Initial, periodic, and event-driven updates all use `position_response`.
> Each message replaces the state for that symbol; it is not a delta or a
> complete list of all positions. The legacy `position_update` / `v4_positions`
> envelope does not apply to `wss://trade.qfex.com`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.