> ## 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.

# 获取用户成交

可查询已认证账户最近的**已执行交易（成交）**。

## 请求示例

```json theme={null}
{
  "type": "get_user_trades",
  "params": {
    "limit": 10,
    "offset": 0,
    "order_id": "5b309929-206f-40ec-804d-cbe46e81afc1",
    "start_ts": 1746900000,
    "end_ts": 1746908400
  }
}
```

* `limit`（可选）：返回成交的最大数量，默认为 **1000**。
* `offset`（可选）：分页偏移量，默认为 **0**。
* `order_id`（可选）：按指定的交易所订单 ID 筛选成交。
* `start_ts`（可选）：筛选不早于此 Unix 时间戳的成交，包含边界。
* `end_ts`（可选）：筛选不晚于此 Unix 时间戳的成交，包含边界。

## 示例代码

```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):
    # Authenticate
    # 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..." }}})
    # Request user trades with optional filters
    send(ws, {
        "type": "get_user_trades",
        "params": {
            "limit": 10,
            "offset": 0,
            "start_ts": 1746900000,
            "end_ts": 1746908400
        }
    })

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 ws = new WebSocket("wss://trade.qfex.com?api_key=YOUR_API_KEY");
const API_KEY = "YOUR_API_KEY";

ws.on("open", () => {
  ws.send(JSON.stringify({ type: "auth", params: { api_key: API_KEY } }));
  ws.send(
    JSON.stringify({
      type: "get_user_trades",
      params: {
        limit: 10,
        offset: 0,
        start_ts: 1746900000,
        end_ts: 1746908400,
      },
    })
  );
});

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()

	// Authenticate
	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..."}}
	})

	// Get user trades with optional filters
	mustWriteJSON(c, map[string]any{
		"type": "get_user_trades",
		"params": map[string]any{
			"limit":    10,
			"offset":   0,
			"start_ts": 1746900000,
			"end_ts":   1746908400,
		},
	})
}
```

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

public class GetUserTradesWs {
  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\":\"get_user_trades\",\"params\":{\"limit\":10,\"offset\":0,\"start_ts\":1746900000,\"end_ts\":1746908400}}");
      }
      @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) {}
  }
}
```

## 响应示例

```json theme={null}
{
  "user_trades": [
    {
      "trade_id": "92adf4db-1e48-40cf-9e2a-b0ecf079a16a",
      "symbol": "AAPL-USD",
      "price": 200.0,
      "quantity": 1.0,
      "aggressor_side": "BUY",
      "order_id": "5b309929-206f-40ec-804d-cbe46e81afc1",
      "client_order_id": "my-client-oid",
      "fee": 0.002,
      "timestamp": 1746908400
    }
  ],
  "count": 1
}
```

> [!NOTE]
>
> 仅返回**已执行交易（成交）**，不包含尚未成交的未完成订单。


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