# 股票K线

- 接口：stock_candlesticks
- 路径：/api/v1/market/data/stock-candlesticks
- 方法：GET
- 描述：获取标的日、周、月、年历史 K 线。当天日 K 在交易日收盘后（15:30 起）更新，不提供盘中实时日 K。响应使用统一 `code/message/data` 壳，`data` 为 K 线数组（非分页）。
- 数据范围：按请求时间范围查询；实际起始日期以行情数据源覆盖为准
- 单次限量：无分页，由 `limit` 控制；单次时间范围最多查询 12 个自然月
- 提示：
  - `symbol`、`interval_unit`、`until_ts_millis` 必填；`since_ts_millis` 与 `limit` 至少填一个。
  - `interval_unit` 接受 `day` / `week` / `month` / `year`（大小写不敏感）。
  - `symbol` 格式为 `{代码}.{市场}`，支持 `.XSHG`/`.SH`、`.XSHE`/`.SZ`、`.BJSE`/`.BJ`；例如 `600519.XSHG` 与 `600519.SH` 等价。
  - since/until 是**毫秒时间戳**；开始时间不得晚于结束时间，超出查询范围返回参数错误。
  - `adjust_kind` 默认 `none`；可设 `forward` 前复权或 `backward` 后复权。
  - 交易日 15:30 前查询当天范围时，结果不包含当日日 K；如需当日日 K，请在 15:30 后查询。
  - 该接口保持原有通用语义，不校验股票、ETF、可转债或指数类别。

## 输入参数

| 名称 | 类型 | 必选 | 描述 |
|------|------|------|------|
| symbol | string | Y | 标的代码，如 `000001.SZ`、`600519.XSHG`；长短市场后缀均支持 |
| interval_unit | string | Y | `day` / `week` / `month` / `year`，大小写不敏感 |
| adjust_kind | string | N | `none`（默认）/`forward`（前复权）/`backward`（后复权） |
| since_ts_millis | string | N | 开始时间戳，单位毫秒；与 `limit` 至少填一个，都不传时返回参数错误 |
| until_ts_millis | string | Y | 结束时间戳，单位毫秒 |
| limit | integer | N | 返回条数上限；不传时返回请求时间范围内的全部数据 |

## 输出参数

成功响应外层固定为 `code`、`message`、`data`：

| 名称 | 类型 | 默认显示 | 描述 |
|------|------|---------|------|
| code | integer | Y | 状态码：200 成功 |
| message | string | Y | 状态信息 |
| data | array | Y | K 线列表 |

data 数组：

| 名称 | 类型 | 默认显示 | 描述 |
|------|------|---------|------|
| open | string | Y | 开盘价，单位元 |
| high | string | Y | 最高价，单位元 |
| low | string | Y | 最低价，单位元 |
| close | string | Y | 收盘价，单位元 |
| ts_millis | integer | Y | 收盘时间戳，单位毫秒 |
| ts_millis_open | integer | Y | 开盘时间戳，单位毫秒 |
| turnover | string | Y | 成交额，单位元 |
| volume | integer | Y | 成交量 |
| turnover_rate | number | Y | 换手率 |

## 接口示例

```bash
curl -sS "https://market.ft.tech/gateway/api/v1/market/data/stock-candlesticks?symbol=000001.SZ&interval_unit=day&since_ts_millis=1756431000000&until_ts_millis=1756710000000&limit=5"
```

Python（[`ftshare`](https://github.com/FTShare-Lab/FTShare-python-sdk.git) SDK，方法 `market.stock_candlesticks`，默认返回 pandas `DataFrame`）：

```python
import time
import ftshare as ft

market = ft.market_api()

# 默认返回 DataFrame（日 K，until 必填）
until = int(time.time() * 1000)
since = until - 3 * 24 * 3600 * 1000
df = market.stock_candlesticks(symbol="000001.SZ", interval_unit="Day",
                               since_ts_millis=since, until_ts_millis=until)
print(df)
#    close   high    low    open      ts_millis  ts_millis_open       turnover     volume  turnover_rate
# 0  10.65  10.67  10.42  10.52  1782111600000   1782091800000  2669536105.56  253063344  0.0130
# 1  10.71  10.91  10.63  10.65  1782198000000   1782178200000  2570720800.76  238120814  0.0123
# 2  10.51  10.78  10.51  10.68  1782284400000   1782264600000  1193370303.56  112584114  0.0058

# 返回 list（每行一个 dict，as_dataframe=False）
rows = market.stock_candlesticks(symbol="000001.SZ", interval_unit="Day",
                                 since_ts_millis=since, until_ts_millis=until, as_dataframe=False)
# rows == [{"close": "10.65", "high": "10.67", "low": "10.42", "open": "10.52", "ts_millis": 1782111600000, ...}]
```

## 数据样例

贵州茅台 `600519.SH` 近 5 个交易日日 K：

| open | high | low | close | ts_millis | ts_millis_open | turnover | volume | turnover_rate |
|------|------|-----|-------|-----------|----------------|----------|--------|---------------|
| 1490.32 | 1494.23 | 1480.01 | 1481.61 | 1756191600000 | 1756171800000 | 5883917000.27 | 3960213 | 0.0032 |
| 1481.88 | 1484.93 | 1448 | 1448 | 1756278000000 | 1756258200000 | 8184169112.68 | 5600609 | 0.0045 |
| 1447.97 | 1456.1 | 1438.77 | 1446.1 | 1756364400000 | 1756344600000 | 5683673273.48 | 3928177 | 0.0031 |
| 1453 | 1482.58 | 1452 | 1480 | 1756450800000 | 1756431000000 | 9159701454.16 | 6225648 | 0.0050 |
| 1482.2 | 1488 | 1465.7 | 1476.1 | 1756710000000 | 1756690200000 | 6658918555.35 | 4512338 | 0.0036 |
