feat: add technical indicator calculation (30 indicators via MyTT), bump to 1.4.0

Integrate MyTT library to provide 30 technical indicators (MACD, KDJ, RSI,
BOLL, DMI, ATR, etc.) accessible via API and CLI with automatic EMA warm-up.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
GitHub
2026-05-28 16:05:40 +08:00
co-authored by Claude Opus 4.7
parent 280af9ecf5
commit bcddf5a052
13 changed files with 1396 additions and 3 deletions
+78
View File
@@ -313,3 +313,81 @@ echo "=== 23. 获取扩展市场分时图(港股腾讯)==="
# 09:34:00 00:00 532.80 532.56 4100
# 09:35:00 00:00 533.20 532.84 3500
# ...(共约330条)
echo "=== 24. 列出可用技术指标 ==="
# 列出所有支持的技术指标名称、输入需求和输出列。
# easy-tdx indicator-list --table
# 输出:
# name description inputs outputs default_params
# MACD MACD 指数平滑异同移动平均线 ['close'] ['MACD_DIF', 'MACD_DEA', ...] {'SHORT': 12, 'LONG': 26, 'M': 9}
# KDJ KDJ 随机指标 ['close', 'high', ...] ['KDJ_K', 'KDJ_D', 'KDJ_J'] {'N': 9, 'M1': 3, 'M2': 3}
# RSI RSI 相对强弱指标 ['close'] ['RSI'] {'N': 24}
# BOLL BOLL 布林带 ['close'] ['BOLL_UPPER', 'BOLL_MID'...] {'N': 20, 'P': 2}
# ...(共30个指标)
echo "=== 25. 计算单个技术指标(MACD==="
# 计算单只股票的技术指标。默认前复权(QFQ),返回最近 30 条。
# 参数: <指标名> -m <市场> -c <代码> --count N --table
# 返回列: datetime, open, high, low, close, vol, amount + 指标列
# easy-tdx indicator MACD -m SH -c 600519 --table
# 输出:
# datetime open high low close vol amount MACD_DIF MACD_DEA MACD_HIST
# 2025-05-06 00:00:00 1498.00 1518.00 1492.00 1510.00 16540 2500000000 -4.56 -2.94 -3.24
# 2025-05-07 00:00:00 1505.00 1516.00 1490.00 1498.00 14280 2150000000 -3.12 -3.18 0.11
# 2025-05-08 00:00:00 1492.00 1510.00 1485.00 1505.00 15670 2350000000 -2.45 -3.03 1.16
# 2025-05-09 00:00:00 1498.00 1516.00 1490.00 1498.00 14280 2150000000 -1.89 -2.80 1.82
# 2025-05-12 00:00:00 1505.00 1516.00 1490.00 1498.00 14280 2150000000 -1.78 -2.60 1.64
# ...(默认30条)
echo "=== 26. 同时计算多个指标 ==="
# 用逗号分隔多个指标名称(不区分大小写)。
# easy-tdx indicator MACD,KDJ,RSI,BOLL -m SH -c 600519 --count 5 --table
# 输出:
# datetime close MACD_DIF MACD_DEA MACD_HIST KDJ_K KDJ_D KDJ_J RSI BOLL_UPPER BOLL_MID BOLL_LOWER
# 2025-05-09 00:00 1505.00 -1.89 -2.80 1.82 45.23 52.34 31.01 55.6 1530.45 1500.12 1469.79
# 2025-05-12 00:00 1498.00 -1.78 -2.60 1.64 38.56 48.89 17.90 48.2 1528.90 1498.56 1468.22
# 2025-05-13 00:00 1510.00 -0.89 -2.26 2.74 62.34 52.17 82.68 56.8 1527.34 1497.00 1466.66
# 2025-05-14 00:00 1509.00 -0.12 -1.83 3.42 58.12 53.56 67.24 52.3 1525.78 1495.44 1465.10
# 2025-05-15 00:00 1521.00 1.23 -1.22 4.90 78.45 59.74 115.87 65.1 1524.22 1493.88 1463.54
echo "=== 27. 自定义指标参数 ==="
# 通过 --params 覆盖默认参数。格式: KEY=VALUE 或 INDICATOR.KEY=VALUE
# 修改 MACD 短周期为 10,长周期为 22
# easy-tdx indicator MACD -m SH -c 600519 --params SHORT=10,LONG=22 --table
#
# 同时计算 MACD 和 KDJ,分别为它们设置不同参数:
# easy-tdx indicator MACD,KDJ -m SH -c 600519 --params MACD.SHORT=10,KDJ.N=14 --table
echo "=== 28. 仅输出指标值(不含 OHLCV==="
# 加 --no-ohlcv 隐藏原始 K 线列,仅显示时间 + 指标值。
# easy-tdx indicator RSI -m SZ -c 000001 --no-ohlcv --count 5 --table
# 输出:
# datetime RSI
# 2025-05-09 00:00:00 52.34
# 2025-05-12 00:00:00 48.67
# 2025-05-13 00:00:00 56.12
# 2025-05-14 00:00:00 51.89
# 2025-05-15 00:00:00 63.45
echo "=== 29. 分钟 K 线技术指标 ==="
# 使用 --period 指定分钟周期,与 K 线命令相同。
# easy-tdx indicator MACD -m SH -c 600519 --period 5MIN --count 10 --table
# 输出:
# datetime close MACD_DIF MACD_DEA MACD_HIST
# 2025-05-15 14:10 1520.50 0.34 0.28 0.12
# 2025-05-15 14:15 1518.20 0.21 0.27 -0.11
# 2025-05-15 14:20 1519.80 0.18 0.25 -0.15
# 2025-05-15 14:25 1521.00 0.23 0.25 -0.04
# ...(共10条)
echo "=== 30. 常用指标快速参考 ==="
# MACD: easy-tdx indicator MACD -m SH -c 600519 --table
# KDJ: easy-tdx indicator KDJ -m SZ -c 000001 --table
# RSI: easy-tdx indicator RSI -m SH -c 600519 --table
# BOLL: easy-tdx indicator BOLL -m SH -c 600519 --table
# DMI: easy-tdx indicator DMI -m SH -c 600519 --table
# ATR: easy-tdx indicator ATR -m SH -c 600519 --table
# WR: easy-tdx indicator WR -m SH -c 600519 --table
# CCI: easy-tdx indicator CCI -m SH -c 600519 --table
# BIAS: easy-tdx indicator BIAS -m SZ -c 000001 --table
# OBV: easy-tdx indicator OBV -m SZ -c 000001 --table
+142
View File
@@ -0,0 +1,142 @@
"""演示:技术指标计算。
通过 MacClient 的 get_stock_kline_with_indicators() 获取 K 线并直接计算技术指标。
内部自动获取 200+ 条历史数据进行 EMA 预热,仅返回最后 count 条结果。
也可单独使用 compute_indicators() 对已有的 K 线 DataFrame 计算指标。
支持的指标(30 个):
MACD KDJ RSI BOLL DMI ATR WR CCI BIAS OBV
VR EMV MFI BRAR ASI TRIX DPO MTM ROC EXPMA
BBI PSY DFMA CR KTN XSII MASS TAQ
参数:
market -- 市场代码(Market.SH=1, Market.SZ=0
code -- 股票代码
indicators -- 指标名称列表(不区分大小写),如 ["MACD", "KDJ"]
count -- 返回条数(默认 30)
adjust -- 复权方式(默认 QFQ 前复权,技术分析推荐前复权)
params -- 可选参数覆盖,如 {"MACD": {"SHORT": 10}}
返回 DataFrame 列说明(以 MACD 为例):
datetime datetime K 线时间
open float 开盘价
high float 最高价
low float 最低价
close float 收盘价
vol float 成交量
amount float 成交额
MACD_DIF float MACD 的 DIF 线
MACD_DEA float MACD 的 DEA 线
MACD_HIST float MACD 柱状图((DIF-DEA)*2
"""
from easy_tdx import Adjust, MacClient, Market, Period
with MacClient.from_best_host() as c:
# --- MACD(贵州茅台,日线,前复权)---
print("=== MACD(贵州茅台 600519===")
df = c.get_stock_kline_with_indicators(
Market.SH,
"600519",
indicators=["MACD"],
count=10,
)
print(df[["datetime", "close", "MACD_DIF", "MACD_DEA", "MACD_HIST"]].to_string(index=False))
# --- KDJ(平安银行)---
print("\n=== KDJ(平安银行 000001===")
df = c.get_stock_kline_with_indicators(
Market.SZ,
"000001",
indicators=["KDJ"],
count=10,
)
print(df[["datetime", "close", "KDJ_K", "KDJ_D", "KDJ_J"]].to_string(index=False))
# --- RSI(贵州茅台)---
print("\n=== RSI(贵州茅台 600519N=6 短周期)===")
df = c.get_stock_kline_with_indicators(
Market.SH,
"600519",
indicators=["RSI"],
count=10,
params={"RSI": {"N": 6}},
)
print(df[["datetime", "close", "RSI"]].to_string(index=False))
# --- BOLL 布林带 ---
print("\n=== BOLL 布林带(贵州茅台 600519===")
df = c.get_stock_kline_with_indicators(
Market.SH,
"600519",
indicators=["BOLL"],
count=10,
)
print(df[["datetime", "close", "BOLL_UPPER", "BOLL_MID", "BOLL_LOWER"]].to_string(index=False))
# --- 多指标同时计算 ---
print("\n=== MACD + KDJ + RSI + BOLL 联合计算 ===")
df = c.get_stock_kline_with_indicators(
Market.SH,
"600519",
indicators=["MACD", "KDJ", "RSI", "BOLL"],
count=5,
)
cols = ["datetime", "close", "MACD_DIF", "KDJ_K", "RSI", "BOLL_UPPER", "BOLL_LOWER"]
print(df[cols].to_string(index=False))
# --- 仅输出指标列(不含 OHLCV---
print("\n=== 仅指标值(--no-ohlcv 模式)===")
df = c.get_stock_kline_with_indicators(
Market.SZ,
"000001",
indicators=["MACD", "RSI"],
count=5,
)
indicator_cols = [
c for c in df.columns if c not in ("open", "high", "low", "close", "vol", "amount")
]
print(df[indicator_cols].to_string(index=False))
# --- 分钟 K 线 + 指标 ---
print("\n=== 5 分钟线 MACD(贵州茅台 600519===")
df = c.get_stock_kline_with_indicators(
Market.SH,
"600519",
indicators=["MACD"],
period=Period.MIN_5,
count=5,
)
print(df[["datetime", "close", "MACD_DIF", "MACD_DEA", "MACD_HIST"]].to_string(index=False))
# --- 使用 compute_indicators 独立计算 ---
print("\n=== 独立使用 compute_indicators ===")
from easy_tdx.indicator import compute_indicators
raw_df = c.get_stock_kline(Market.SH, "600519", Period.DAILY, count=200, adjust=Adjust.QFQ)
result = compute_indicators(raw_df, ["ATR", "CCI", "WR"], tail=5)
print(result[["datetime", "close", "ATR", "CCI", "WR1", "WR2"]].to_string(index=False))
# 运行结果(示例):
# === MACD(贵州茅台 600519===
# datetime close MACD_DIF MACD_DEA MACD_HIST
# 2025-05-02 00:00:00 1492.00 -4.12 -1.56 -5.12
# 2025-05-05 00:00:00 1485.00 -5.23 -2.29 -5.88
# 2025-05-06 00:00:00 1498.00 -4.56 -2.94 -3.24
# 2025-05-07 00:00:00 1510.00 -3.12 -3.18 0.11
# 2025-05-08 00:00:00 1505.00 -2.45 -3.03 1.16
# 2025-05-09 00:00:00 1505.00 -1.89 -2.80 1.82
# 2025-05-12 00:00:00 1498.00 -1.78 -2.60 1.64
# 2025-05-13 00:00:00 1510.00 -0.89 -2.26 2.74
# 2025-05-14 00:00:00 1509.00 -0.12 -1.83 3.42
# 2025-05-15 00:00:00 1521.00 1.23 -1.22 4.90
#
# === KDJ(平安银行 000001===
# datetime close KDJ_K KDJ_D KDJ_J
# 2025-05-02 00:00:00 12.45 65.32 58.76 78.44
# 2025-05-05 00:00:00 12.30 42.15 53.23 19.98
# 2025-05-06 00:00:00 12.58 71.23 59.23 95.24
# 2025-05-07 00:00:00 12.72 82.45 65.47 116.41
# 2025-05-08 00:00:00 12.65 74.56 67.29 89.11
# ...
+50
View File
@@ -0,0 +1,50 @@
"""演示:列出所有可用技术指标及其参数。
使用 list_indicators() 查看所有支持的指标名称、输入需求、输出列和默认参数。
无需网络连接。
"""
from easy_tdx.indicator import list_indicators
indicators = list_indicators()
print(f"{len(indicators)} 个技术指标\n")
# 按所需输入列分组展示
groups: dict[str, list[dict]] = {}
for info in indicators:
key = "+".join(info["inputs"])
groups.setdefault(key, []).append(info)
for inputs, items in groups.items():
print(f"── 输入: {inputs} {'' * 50}")
for item in items:
params_str = (
", ".join(f"{k}={v}" for k, v in item["default_params"].items())
if item["default_params"]
else ""
)
outputs_str = ", ".join(item["outputs"])
line = f" {item['name']:<8} {item['description']}"
if params_str:
line += f" (默认: {params_str})"
print(line)
print(f" 输出: {outputs_str}")
print()
# 运行结果:
# 共 30 个技术指标
#
# ── 输入: close ──────────────────────────────────────────────────────────
# MACD MACD 指数平滑异同移动平均线 (默认: SHORT=12, LONG=26, M=9)
# 输出: MACD_DIF, MACD_DEA, MACD_HIST
# RSI RSI 相对强弱指标 (默认: N=24)
# 输出: RSI
# BOLL BOLL 布林带 (默认: N=20, P=2)
# 输出: BOLL_UPPER, BOLL_MID, BOLL_LOWER
# ...
#
# ── 输入: close+high+low ────────────────────────────────────────────────
# KDJ KDJ 随机指标 (默认: N=9, M1=3, M2=3)
# 输出: KDJ_K, KDJ_D, KDJ_J
# ...