# 通达信TdxAiData:手搓股软,板块云图+连板天梯 (opens new window)

通达信TdxAiData:Python独立数据接口,无需启动客户端 (opens new window)

附件下载
1119板块云图连板天梯-项目_1795398020113.zip (opens new window)

注意
示例代码的入参尤其是开始日期,截止日期,根据需要可以做更改适应当前交易日情形。
例1

"""例1:取概念板块涨幅排名(get_domain_sorthq,含流通市值/总市值)"""

from tdxaidata import tqs

rank = tqs.get_domain_sorthq(
    domain=10004,
    colname="ZAF",
    sorttype=1,
    wantcol=["Amount", "fHSL,Ltsz", "Zsz"],
    startxh=0,
    wantnum=5,
)
print(rank)
1
2
3
4
5
6
7
8
9
10
11
12
13

例2

# -*- coding: utf-8 -*-
"""例2:取一个板块的全部成份股(formula_by_file + domain=20000+880后3位)"""

from tdxaidata import tqs

FORMULA = r"C:\new_tdx_test2025\T0001\export\gs_txt\cur_0_恒成立.txt"
res = tqs.formula_by_file(
    formula_file=FORMULA,
    domain=20491,
    stock_period="1d",
    count=0,
    return_count=0,
    start_time="20261120",
    end_time="20261120",
    return_date=True,
    xsflag=3,
    dividend_type=0,
    formula_type=1,
)
codes = sorted(res.keys())
print("半导体(880491)成份股数:", len(codes))
print("前10只:", codes[:10])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

例3


# -*- coding: utf-8 -*-
"""例3:涨停判定 + 连板数(只要日线收盘价,不依赖任何上文变量)"""
from tdxaidata import tqs
 
DATE, START = "20261120", "20261017"
CODES = ["301391.SZ", "603030.SH", "603324.SH", "000001.SZ"]
 
close = tqs.get_market_data(
    field_list=["close"], stock_list=CODES, period="1d",
    start_time=START, end_time=DATE, count=-1, dividend_type="none",
)["Close"]
 
ST_LIMIT_DATE = "20260706"
 
 
def limit_rate(code, name="", date=None):
    date = date or DATE
    n = code[:6]
    if n.startswith(("30", "688")):
        return 0.20
    if n.startswith(("8", "4", "9")):
        return 0.30
    if "ST" in (name or "").upper() and date < ST_LIMIT_DATE:
        return 0.05
    return 0.10
 
for code in CODES:
    rate = limit_rate(code)
    limit_price = (close[code].shift(1) * (1 + rate)).round(2)
    is_up = close[code] >= limit_price - 0.005
    boards = 0
    for flag in reversed(is_up.tolist()):
        if flag:
            boards += 1
        else:
            break
    print("%s  涨停价阈值=%.2f  最新收盘=%.2f  连板=%d" %
          (code, limit_price.iloc[-1], close[code].iloc[-1], boards))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

例4

# -*- coding: utf-8 -*-
"""例4:按板块聚合涨停股数 / 最高连板 / 10cm·20cm 涨停个数"""
from tdxaidata import tqs
 
FORMULA = r"C:\new_tdx_test2025\T0001\export\gs_txt\cur_0_恒成立.txt"
DATE, START = "20261120", "20261017"
SECTORS = [("光刻机", 20716), ("先进封装", 20635), ("存储芯片", 20672)]
 
ST_LIMIT_DATE = "20260706"
 
 
def limit_rate(code, name="", date=None):
    date = date or DATE
    n = code[:6]
    if n.startswith(("30", "688")):
        return 0.20
    if n.startswith(("8", "4", "9")):
        return 0.30
    if "ST" in (name or "").upper() and date < ST_LIMIT_DATE:
        return 0.05
    return 0.10
 
all_codes = []
for _, dom in SECTORS:
    res = tqs.formula_by_file(
        formula_file=FORMULA, domain=dom, stock_period="1d",
        count=0, return_count=0, start_time=DATE, end_time=DATE,
        return_date=True, xsflag=3, dividend_type=0, formula_type=1,
    )
    all_codes.extend(res.keys())
all_codes = sorted(set(all_codes))
 
close = tqs.get_market_data(
    field_list=["close"], stock_list=all_codes, period="1d",
    start_time=START, end_time=DATE, count=-1, dividend_type="none",
)["Close"]
 
boards = {}
for code in all_codes:
    lim = (close[code].shift(1) * (1 + limit_rate(code))).round(2)
    up = close[code] >= lim - 0.005
    n = 0
    for flag in reversed(up.tolist()):
        if flag:
            n += 1
        else:
            break
    boards[code] = n
 
for name, dom in SECTORS:
    res = tqs.formula_by_file(
        formula_file=FORMULA, domain=dom, stock_period="1d",
        count=0, return_count=0, start_time=DATE, end_time=DATE,
        return_date=True, xsflag=3, dividend_type=0, formula_type=1,
    )
    ms = [c for c in res if c in boards]
    zt = [c for c in ms if boards[c] >= 1]
    c10 = sum(1 for c in zt if limit_rate(c) == 0.10)
    c20 = sum(1 for c in zt if limit_rate(c) == 0.20)
    print("%s:成份 %d,涨停 %d,最高连板 %d,10cm %d,20cm %d" %
          (name, len(ms), len(zt), max([boards[c] for c in ms] + [0]), c10, c20))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

例5

# -*- coding: utf-8 -*-
"""例5:涨停高度曲线(样本池=3 个概念板块成份并集;全市场把 domain 换成 6)"""
from tdxaidata import tqs
 
FORMULA = r"C:\new_tdx_test2025\T0001\export\gs_txt\cur_0_恒成立.txt"
DATE, START = "20261120", "20261017"
DOMAINS = [20716, 20635, 20672]
 
codes = []
for dom in DOMAINS:
    res = tqs.formula_by_file(
        formula_file=FORMULA, domain=dom, stock_period="1d",
        count=0, return_count=0, start_time=DATE, end_time=DATE,
        return_date=True, xsflag=3, dividend_type=0, formula_type=1,
    )
    codes.extend(res.keys())
codes = sorted(set(codes))
 
close = tqs.get_market_data(
    field_list=["close"], stock_list=codes, period="1d",
    start_time=START, end_time=DATE, count=-1, dividend_type="none",
)["Close"]
 
ST_LIMIT_DATE = "20260706"
 
 
def limit_rate(code, name="", date=None):
    date = date or DATE
    n = code[:6]
    if n.startswith(("30", "688")):
        return 0.20
    if n.startswith(("8", "4", "9")):
        return 0.30
    if "ST" in (name or "").upper() and date < ST_LIMIT_DATE:
        return 0.05
    return 0.10
 
flag = {}
for c in codes:
    lim = (close[c].shift(1) * (1 + limit_rate(c))).round(2)
    flag[c] = (close[c] >= lim - 0.005).tolist()
 
for i in range(1, len(close.index)):
    day = close.index[i].strftime("%Y-%m-%d")
    zt = sum(1 for c in codes if flag[c][i])
    hs = []
    for c in codes:
        n = 0
        for v in reversed(flag[c][:i + 1]):
            if v:
                n += 1
            else:
                break
        hs.append(n)
    print("%s  涨停 %3d  最高连板 %d  连板>=2 %2d" %
          (day, zt, max(hs), sum(1 for h in hs if h >= 2)))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

例6

# -*- coding: utf-8 -*-
"""例6:个股所属板块反查,按板块涨幅排序取前10(get_relation + get_domain_sorthq + get_more_info)"""

from tdxaidata import tqs

STOCK = "688318.SH"
DOMAINS = (10000, 10002, 10005)


def sector_zaf_map():
    out = {}
    for domain in DOMAINS:
        start = 0
        while True:
            df = tqs.get_domain_sorthq(
                domain=domain,
                colname="ZAF",
                sorttype=1,
                wantcol=["ZAF"],
                startxh=start,
                wantnum=200,
            )
            if df is None or getattr(df, "empty", True):
                break
            for _, row in df.iterrows():
                code = str(row.get("Code", "")).strip()
                if code:
                    out.setdefault(code, float(row.get("ZAF") or 0))
            if len(df) < 200:
                break
            start += 200
    return out


zaf_map = sector_zaf_map()
blocks = []
for item in tqs.get_relation(stock_code=STOCK) or []:
    code = str(item.get("BlockCode", "")).strip()
    zaf = zaf_map.get(code)
    if zaf is None:
        more = tqs.get_more_info(stock_code=code, field_list=["ZAF"]) or {}
        zaf = float(more.get("ZAF") or 0)
    blocks.append(
        {
            "code": code,
            "name": item.get("BlockName", ""),
            "type": item.get("BlockType", ""),
            "zaf": zaf,
        }
    )

blocks.sort(key=lambda row: -row["zaf"])
print(STOCK, "所属板块涨幅前10")
for row in blocks[:10]:
    print(row["code"], row["name"], row["type"], row["zaf"])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

例7

# -*- coding: utf-8 -*-
"""例7:竞价末价反推涨停(get_call_auction_batch + get_market_data)"""

from tdxaidata import tqs

DATE = "20261120"
PREV_END = "20261119"
STOCKS = {
    "002771.SZ": "真视通",
    "603030.SH": "全筑股份",
    "000001.SZ": "平安银行",
}


def limit_rate(code, name):
    if code.startswith(("300", "301", "688")):
        return 0.20
    if code.startswith(("43", "83", "87", "92")):
        return 0.30
    if "ST" in name.upper():
        return 0.10 if DATE >= "20260706" else 0.05
    return 0.10


def prev_close(code):
    raw = tqs.get_market_data(
        field_list=["close"],
        stock_list=[code],
        period="1d",
        start_time="20261117",
        end_time=PREV_END,
        count=-1,
        dividend_type="none",
    )
    close = raw["Close"][code].iloc[-1]
    return float(close)


auction = (
    tqs.get_call_auction_batch(
        stock_list=list(STOCKS),
        field_list=["Time", "Price", "Volume", "LeaveQty"],
        type=1,
        date="",
        return_df=False,
    )
    or {}
)

print(DATE, "竞价末价涨停判定")
for code, name in STOCKS.items():
    rec = auction.get(code) or {}
    prices = [float(p) for p in (rec.get("Price") or []) if float(p) > 0]
    times = [str(t) for t in (rec.get("Time") or [])]
    if not prices:
        print(code, name, "无竞价数据")
        continue
    price = prices[-1]
    last = prev_close(code)
    rate = limit_rate(code, name)
    limit_price = round(last * (1 + rate), 2)
    is_limit = price >= limit_price - 0.005
    print(
        code,
        name,
        "末价",
        price,
        "昨收",
        last,
        "涨停价",
        limit_price,
        "竞价涨停",
        is_limit,
        "最后时点",
        times[-1] if times else "",
    )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

例8 在提醒例2也可以这样儿后补充的示例代码

# -*- coding: utf-8 -*-
"""例8:formula_by_src 用一行 XG:1 取 domain 全部成份股"""
from tdxaidata import tqs
SRC = "XG:1;"
res = tqs.formula_by_src(
    formula_src=SRC,
    stock_list=[],
    stock_period="1d",
    start_time="20261120",
    end_time="20261120",
    count=0,
    return_count=0,
    return_date=True,
    xsflag=3,
    dividend_type=0,
    formula_type=1,
    domain=20491,
)
codes = sorted(res.keys())
print("XG:1 半导体(880491)成份股数:", len(codes))
print("前10只:", codes[:10])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

例9

# -*- coding: utf-8 -*-
from tdxaidata import tqs

rank = tqs.get_domain_sorthq(
    domain=6,
    colname="ZTDateNum",
    sorttype=1,
    wantcol=["Amount", "fHSL", "ZTDateNum"],
    startxh=0,
    wantnum=200,
)
rank["ZTDateNum"] = rank["ZTDateNum"].astype(float)
zt = rank[rank["ZTDateNum"] >= 1]

print("返回行数", len(rank))
print(">=1 行数", len(zt))
print(
    zt[["Code", "Name", "Now", "ZAF", "ZTDateNum", "Amount", "fHSL"]]
    .head(15)
    .to_string(index=False)
)
print(zt["ZTDateNum"].value_counts().sort_index().to_string())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

例10


# -*- coding: utf-8 -*-
from tdxaidata import tqs
 
stocks = tqs.get_stock_list(market="5", list_type=1)
print("全A股票数", len(stocks))
print(stocks[:10])
1
2
3
4
5
6
7