# 撮合

### 撮合（Matching）是交易所將買單和賣單配對成交的機制。當有人想以 $100 買入，同時有人想以 $100 賣出，撮合引擎就把這兩筆訂單「撮合」在一起，完成交易。這是所有交易所的核心功能。

***

## 撮合的基本原理

```
買方：「我想以 $100 買 1 BTC」
賣方：「我想以 $100 賣 1 BTC」

撮合引擎：「配對！成交！」

買方獲得 1 BTC
賣方獲得 $100
```

{% hint style="info" %}
**價格優先、時間優先**

撮合通常遵循兩個原則：

1. **價格優先**：出價高的買單先成交，出價低的賣單先成交
2. **時間優先**：相同價格時，先下單的先成交
   {% endhint %}

***

## 訂單簿

撮合引擎使用「訂單簿」（Order Book）來管理所有未成交的訂單：

```
         賣單
  $102  |  2 BTC
  $101  |  5 BTC
  $100  |  3 BTC  ← 最低賣價（Ask）
  ─────────────────
  $99   |  4 BTC  ← 最高買價（Bid）
  $98   |  6 BTC
  $97   |  1 BTC
         買單
```

**買賣價差（Spread）**= 最低賣價 - 最高買價 = $100 - $99 = $1

***

## 訂單類型

### 1. 限價單（Limit Order）

指定價格，只有達到或優於此價格才成交。

```
「我只願意以 $100 或更低的價格買入」
```

### 2. 市價單（Market Order）

立即以當前最佳價格成交。

```
「我現在就要買，不管什麼價格」
```

### 3. 止損單（Stop Order）

當價格達到觸發點時，自動下單。

```
「如果價格跌到 $90，幫我賣出」
```

***

## 撮合引擎的效能

頂級交易所的撮合引擎需要：

| 指標   | 說明             |
| ---- | -------------- |
| 低延遲  | 毫秒級處理速度        |
| 高吞吐量 | 每秒處理數萬筆訂單      |
| 高可用性 | 99.99%+ 正常運行時間 |
| 公平性  | 嚴格按照規則撮合       |

{% hint style="success" %}
**幣安的撮合引擎**

幣安聲稱其撮合引擎每秒可處理 140 萬筆訂單。這種效能是 DEX 目前難以匹敵的。
{% endhint %}

***

## CEX vs DEX 的撮合

| 比較   | CEX   | DEX（AMM）     |
| ---- | ----- | ------------ |
| 撮合方式 | 訂單簿配對 | 數學公式（x\*y=k） |
| 速度   | 毫秒級   | 秒級（需等區塊）     |
| 流動性  | 依賴交易者 | 依賴流動性池       |
| 價格發現 | 市場供需  | 公式計算         |

***

## 撮合中的常見問題

### 滑點（Slippage）

大額訂單可能無法以預期價格全部成交，需要「吃掉」多個價位的訂單。

### 深度不足

流動性差的市場，買賣價差大，大單難以成交。

### 閃崩（Flash Crash）

在流動性枯竭時，價格可能瞬間暴跌，觸發連環清算。

{% hint style="warning" %}
**市價單的風險**

在流動性差的市場使用市價單很危險——你可能以非常差的價格成交。建議使用限價單來控制成交價格。
{% endhint %}

***

## 延伸閱讀

* [中心化交易所](/mai-mai-shu-wei-zi-chan-de-chang-suo/zhong-xin-hua-jiao-yi-suo.md) — 撮合的發生地
* [去中心化交易所](/mai-mai-shu-wei-zi-chan-de-chang-suo/qu-zhong-xin-hua-jiao-yi-suo.md) — 不同的撮合方式
* [場外交易](/jiao-yi-cao-zuo-1/wai-jiao-yi.md) — 不經過撮合的交易
* [滑點](https://github.com/dAAAb/Blockpedia/blob/master/defi/hua-dian.md) — 撮合中的價格偏差

***

#### 參考資料

* [Order Matching Algorithms](https://www.investopedia.com/terms/m/matchingorders.asp)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.0x1.academy/jiao-yi-cao-zuo-1/cuo-he-jiao-yi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
