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

# 取消全部订单

通过 **WebSocket** 的 `cancel_all_orders` 消息，可取消账户的**所有有效订单**，也可选择按**交易品种**筛选。

## 请求示例

```json theme={null}
{
  "type": "cancel_all_orders",
  "params": {
    "symbol": "AAPL-USD"
  }
}
```

## 示例代码

```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..." }}})
    # Cancel all orders
    send(ws, {"type": "cancel_all_orders", "params": {}})

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", () => {
  // Authenticate
  ws.send(JSON.stringify({ type: "auth", params: { api_key: API_KEY } }));
  // Cancel all orders
  ws.send(JSON.stringify({ type: "cancel_all_orders", params: {} }));
});

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

	// Cancel all orders
	mustWriteJSON(c, map[string]any{
		"type": "cancel_all_orders",
		"params": map[string]any{},
	})
}
```

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

public class CancelAllOrdersWs {
  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) {
        // Authenticate
        ws.send("{\"type\": \"auth\", \"params\": { \"hmac\": { \"public_key\": \"qfex_pub_xxxxx\", \"nonce\": \"c0ffee...\..\", \"unix_ts\": 1760545414, \"signature\": \"5f2e...\" }}}");
        // Cancel all orders
        ws.send("{\"type\":\"cancel_all_orders\",\"params\":{}}");
      }
      @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) {}
  }
}
```

## 响应示例

每笔被取消的订单都会生成状态为 `CANCELLED` 的 **`order_response`**。

```json theme={null}
{
  "order_response": {
    "order_id": "5b309929-206f-40ec-804d-cbe46e81afc1",
    "symbol": "AAPL-USD",
    "status": "CANCELLED",
    "quantity": 1.0,
    "price": 200.0,
    "side": "BUY",
    "type": "LIMIT",
    "time_in_force": "GTC",
    "user_id": "0020ce8e-eaee-480e-8d7f-b9241d756ee5",
    "client_order_id": "my-client-oid",
    "quantity_remaining": 1.0,
    "trade_id": null
  }
}
```

> [!NOTE]
>
> 除非指定 `symbol`，否则会取消已认证账户在**所有品种上的全部未完成订单**。如需按 ID 取消指定订单，请改用[取消订单](https://docs.qfex.com/cn/websocket/channels/trade/cancel_order.md)。


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