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

# 交易日曆

> 查詢各市場的交易日與非交易日。

## 取得交易行事曆

### 方法名稱：get\_trading\_calendar

### 入參

| 欄位               | 類型                                 | 說明                           | 是否必填 |
| :--------------- | :--------------------------------- | :--------------------------- | :--- |
| start\_date      | Optional\[str]                     | 開始日期，格式為 YYYYMMDD            | 非必填  |
| end\_date        | Optional\[str]                     | 結束日期，格式為 YYYYMMDD            | 非必填  |
| market           | Optional\[str]                     | 交易所代號，預設為 "hk"，目前支援"hk"和"us" | 非必填  |
| is\_trading\_day | Optional\[int]                     | 是否為交易日，1=交易日，0=非交易日，None=全部  | 非必填  |
| fields           | Optional\[Union\[str, List\[str]]] | 需要傳回的欄位清單                    | 非必填  |

### 回應參數

| 字段                | 類型  | 描述                    |
| :---------------- | :-- | :-------------------- |
| nature\_date      | int | 日期，格式為YYYYMMDD        |
| exchange          | str | 交易所代號                 |
| is\_trade         | int | 是否為交易日，1表示交易日，0表示非交易日 |
| pretrade\_date    | str | 目前日期前一個交易日            |
| next\_trade\_date | str | 目前日期後一個交易日            |

### 使用範例

#### 取得一段時間內港股交易日曆

```python theme={null}
import tqx_data
result = tqx_data.get_trading_calendar(
    start_date="20250101",
    end_date="20250115",
    market="hk",
    is_trading_day=None,
    fields=[]
)
print(result)
```

**回應範例**

```text theme={null}
nature_date exchange is_trade next_trade_date pretrade_date
0 20250101 HK 0 20250102 20241230
1 20250102 HK 1 20250103 20241231
2 20250103 HK 1 20250106 20250102
3 20250104 HK 0 20250106 20250102
4 20250105 HK 0 20250106 20250102
5 20250106 HK 1 20250107 20250103
6 20250107 HK 1 20250108 20250106
7 20250108 HK 1 20250109 20250107
8 20250109 HK 1 20250110 20250108
9 20250110 HK 1 20250113 20250109
10 20250111 HK 0 20250113 20250109
11 20250112 HK 0 20250113 20250109
12 20250113 HK 1 20250114 20250110
13 20250114 HK 1 20250115 20250113
14 20250115 HK 1 20250116 20250114
```

#### 取得一段時間內美股非交易日且使用fields

```python theme={null}
import tqx_data
result = tqx_data.get_trading_calendar(
    start_date="20241215",
    end_date="20250110",
    market="us",
    is_trading_day=0,
    fields=["nature_date", "is_trade", "next_trade_date", "pretrade_date"]
)
print(result)
```

**回應範例**

```text theme={null}
nature_date is_trade next_trade_date pretrade_date
0 20241215 0 20241216 20241212
1 20241221 0 20241223 20241219
2 20241222 0 20241223 20241219
3 20241225 0 20241226 20241223
4 20241228 0 20241230 20241226
5 20241229 0 20241230 20241226
6 20250101 0 20250102 20241230
7 20250104 0 20250106 20250102
8 20250105 0 20250106 20250102
9 20250109 0 20250110 20250108
```

#### 取得一段時間內港股交易日

```python theme={null}
import tqx_data
result = tqx_data.get_trading_calendar(
    start_date="20250101",
    end_date="20250120",
    market="hk",
    is_trading_day=1,
    fields=[]
)
print(result)
```

**回應範例**

```text theme={null}
nature_date exchange is_trade next_trade_date pretrade_date
0 20250102 HK 1 20250103 20241231
1 20250103 HK 1 20250106 20250102
2 20250106 HK 1 20250107 20250103
3 20250107 HK 1 20250108 20250106
4 20250108 HK 1 20250109 20250107
5 20250109 HK 1 20250110 20250108
6 20250110 HK 1 20250113 20250109
7 20250113 HK 1 20250114 20250110
8 20250114 HK 1 20250115 20250113
9 20250115 HK 1 20250116 20250114
10 20250116 HK 1 20250117 20250115
11 20250117 HK 1 20250120 20250116
12 20250120 HK 1 20250121 20250117
```
