通达信TdxAiData:沪深300单票全仓和默认参10只组合回测对比 (opens new window)

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

取沪深300成份股

from tdxaidata import tqs

codes = tqs.get_zzgz_stocklist("000300.SH", 0)
info = tqs.get_stock_info_batch(codes)
print("成份股数量:", len(codes))
for code in codes[:3]:
    print(code, info.get(code, {}).get("Name", ""))
1
2
3
4
5
6
7

单票全仓 本地tq版

import sys
sys.path.append("C:/new_tdx_test2025/PYPlugins/user") #改成自己目录
from tqcenter import tq
tq.initialize(__file__)
r = tq.formula_process_mul_exp(
 formula_name="macd",
 formula_arg="12,26,9",
 stock_list=["600183.SH"],
 stock_period="1d",
 start_time="20260226",
 end_time="20261126",
 count=0,
 dividend_type=1,
 return_count=1,
 return_date=True,
 back_test=True,
 bt_config={
  "max_exposure": 1,
  "max_positions": 1,
  "daily_entry_limit": 1,
  "entry_weight_limit": 1.0,
 },
)
bt = r.get("bt_result", {})
print({k: bt.get(k) for k in ["closed_trades", "win_rate_pct", "total_return_pct", "annual_return_pct", "max_drawdown_pct", "peak_return_pct"]})
tq.close()
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

单票全仓 客户端tqserver版TdxAiData模式

import sys
sys.path.append("C:/new_tdx_test2025/PYPlugins/user")  #改成自己目录
from tqserver import tqs

r = tqs.formula_process_mul_exp(
    formula_name="MACD",
    formula_arg="12,26,9",
    stock_list=["600183.SH"],
    stock_period="1d",
    start_time="20260226",
    end_time="20261126",
    count=0,
    dividend_type=1,
    return_count=1,
    return_date=True,
    back_test=True,
    bt_config={
        "max_exposure": 1,
        "max_positions": 1,
        "daily_entry_limit": 1,
        "entry_weight_limit": 1.0,
    },
)
bt = r.get("bt_result", {})
print(
    {
        k: bt.get(k)
        for k in [
            "closed_trades",
            "win_rate_pct",
            "total_return_pct",
            "annual_return_pct",
            "max_drawdown_pct",
            "peak_return_pct",
        ]
    }
)
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

默认 bt_config={} 下,最多同时持 10 只,单股开仓比例 0.1,每日最多开 10 仓。

import sys
sys.path.append("C:/new_tdx_test2025/PYPlugins/user") #改成自己目录
from tdxaidata import tqs
from tqcenter import tq
codes = tqs.get_zzgz_stocklist("000300.SH", 0)
tq.initialize(__file__)
r = tq.formula_process_mul_exp(
 formula_name="macd",
 formula_arg="12,26,9",
 stock_list=codes,
 stock_period="1d",
 start_time="20260226",
 end_time="20261126",
 count=0,
 dividend_type=1,
 return_count=1,
 return_date=True,
 back_test=True,
 bt_config={},
)
bt = r.get("bt_result", {})
print({k: bt.get(k) for k in ["closed_trades", "win_rate_pct", "total_return_pct", "annual_return_pct", "max_drawdown_pct", "peak_return_pct"]})
tq.close()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23