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

# 示例代码

> QuantDash Python SDK 实用代码示例

## 日内 VWAP 均线

计算当日成交量加权平均价（VWAP），常用于日内交易判断买卖点：

```python theme={null}
from quantdash import QuantDash

qd = QuantDash(api_key="your-api-key")

df = qd.klines.intraday("600519.SH", to_dataframe=True)
df["vwap"] = (df["amount"].cumsum() / (df["volume"].cumsum() * 100)).round(2)
print(df[["trade_time", "close", "volume", "vwap"]].tail(5).to_string(index=False))
```

```
         trade_time   close  volume    vwap
2026-06-18 14:56:00 1223.50     273 1221.07
2026-06-18 14:57:00 1223.88     261 1221.08
2026-06-18 14:58:00 1223.26       2 1221.08
2026-06-18 14:59:00 1223.26       0 1221.08
2026-06-18 15:00:00 1215.00    1788 1220.89
```

## RSI 相对强弱指标

计算 14 日 RSI 指标，判断超买超卖：

```python theme={null}
from quantdash import QuantDash

qd = QuantDash(api_key="your-api-key")

df = qd.klines.get("600519.SH", period="1d", count=60, to_dataframe=True)
delta = df["close"].diff()
gain = delta.clip(lower=0)
loss = (-delta.clip(upper=0))
avg_gain = gain.rolling(14).mean()
avg_loss = loss.rolling(14).mean()
rs = avg_gain / avg_loss
df["rsi"] = (100 - 100 / (1 + rs)).round(2)
print(df[["trade_date", "close", "rsi"]].tail(5).to_string(index=False))
```

```
trade_date   close   rsi
2026-06-12 1291.91 51.28
2026-06-15 1271.10 49.53
2026-06-16 1255.67 39.66
2026-06-17 1240.00 41.73
2026-06-18 1215.00 21.17
```

## 布林带

计算 20 日布林带上下轨，判断价格偏离度：

```python theme={null}
from quantdash import QuantDash

qd = QuantDash(api_key="your-api-key")

df = qd.klines.get("600519.SH", period="1d", count=40, to_dataframe=True)
df["ma20"] = df["close"].rolling(20).mean().round(2)
df["std20"] = df["close"].rolling(20).std().round(2)
df["upper"] = (df["ma20"] + 2 * df["std20"]).round(2)
df["lower"] = (df["ma20"] - 2 * df["std20"]).round(2)
print(df[["trade_date", "close", "ma20", "upper", "lower"]].tail(5).to_string(index=False))
```

```
trade_date   close    ma20   upper   lower
2026-06-12 1291.91 1291.66 1335.10 1248.22
2026-06-15 1271.10 1289.06 1330.78 1247.34
2026-06-16 1255.67 1285.63 1326.43 1244.83
2026-06-17 1240.00 1281.88 1325.04 1238.72
2026-06-18 1215.00 1277.08 1327.36 1226.80
```

## 成交量异常检测

找出近期量比最高的交易日，辅助判断资金异动：

```python theme={null}
from quantdash import QuantDash

qd = QuantDash(api_key="your-api-key")

df = qd.klines.get("000001.SZ", period="1d", count=30, to_dataframe=True)
df["vol_ma20"] = df["volume"].rolling(20).mean().round(0)
df["vol_ratio"] = (df["volume"] / df["vol_ma20"]).round(2)
top = df.nlargest(3, "vol_ratio")
print("量比最高的 3 天:")
print(top[["trade_date", "close", "volume", "vol_ma20", "vol_ratio"]].to_string(index=False))
```

```
量比最高的 3 天:
trade_date     close  volume  vol_ma20  vol_ratio
2026-06-12 11.240000 2032355 1030628.0       1.97
2026-06-10 10.960008 1543176  959655.0       1.61
2026-06-15 11.060000 1541305 1064874.0       1.45
```

## 涨跌停检测

通过标的信息获取精确涨跌停价（精度 1e-3），再结合五档盘口确认封板状态：

```python theme={null}
from quantdash import QuantDash

qd = QuantDash(api_key="your-api-key")

# 1. 获取全 A 行情
quotes_df = qd.quotes.get(universes=["CN_Stock"], to_dataframe=True)

# 2. 获取标的信息（含今日涨跌停价，SDK 自动分批）
insts = qd.instruments.batch(quotes_df["symbol"].tolist())
inst_map = {x["symbol"]: x for x in insts if x.get("ext", {}).get("limit_up") is not None}

# 3. 价格初筛（允许 1e-3 误差）
def match_limit(row, key):
    inst = inst_map.get(row["symbol"])
    if not inst:
        return False
    price = inst["ext"].get(key)
    return price is not None and abs(row["last_price"] - price) < 1e-3

quotes_df["is_limit_up"] = quotes_df.apply(lambda r: match_limit(r, "limit_up"), axis=1)
quotes_df["is_limit_down"] = quotes_df.apply(lambda r: match_limit(r, "limit_down"), axis=1)

up_candidates = quotes_df[quotes_df["is_limit_up"]]
down_candidates = quotes_df[quotes_df["is_limit_down"]]

# 4. 盘口确认：卖1量为0=涨停封板，买1量为0=跌停封板（SDK 自动分批）
depths_up = qd.depth.batch(up_candidates["symbol"].tolist())
confirmed_up = [sym for sym, d in depths_up.items() if d["ask_volumes"][0] == 0]

depths_down = qd.depth.batch(down_candidates["symbol"].tolist())
confirmed_down = [sym for sym, d in depths_down.items() if d["bid_volumes"][0] == 0]

up_final = up_candidates[up_candidates["symbol"].isin(confirmed_up)]
down_final = down_candidates[down_candidates["symbol"].isin(confirmed_down)]

print(f"涨停封板: {len(up_final)} 只")
print(up_final[["symbol", "ext.name", "last_price"]].head(5).to_string(index=False))
print(f"\n跌停封板: {len(down_final)} 只")
print(down_final[["symbol", "ext.name", "last_price"]].head(5).to_string(index=False))
```

```
涨停封板: 100 只
   symbol ext.name  last_price
301580.SZ      爱迪特       63.17
002159.SZ     三特索道       14.37
002859.SZ     洁美科技       99.73
000889.SZ     中嘉博创        4.02
603956.SH      威派格        4.98

跌停封板: 34 只
   symbol ext.name  last_price
002323.SZ    *ST雅博        1.30
600539.SH     狮头股份       14.44
002568.SZ     百润股份       16.54
600537.SH    *ST亿晶        2.84
601010.SH     ST文峰        1.47
```
