@weaxs/openclaw-stock-analysis 0.2.6 → 0.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.js +268 -18
- package/index.ts +197 -11
- package/openclaw.plugin.json +8 -1
- package/package.json +1 -1
- package/schemas/market_review_schema.json +12 -5
- package/schemas/report_schema.json +65 -1
- package/scripts/cli-timeout.mjs +27 -0
- package/scripts/json-input.mjs +26 -0
- package/skills/event-driven/SKILL.md +4 -0
- package/skills/growth-quality/SKILL.md +4 -0
- package/skills/market-review/SKILL.md +2 -0
- package/skills/stock-analysis/SKILL.md +17 -3
- package/skills/stock-analysis/references/stateless-analysis.md +66 -0
- package/skills/stock-screener/SKILL.md +5 -1
- package/skills/strategy-backtest/SKILL.md +9 -0
- package/templates/market_review/full.md.j2 +7 -0
- package/templates/stock_analysis/_evidence.md.j2 +11 -0
- package/templates/stock_analysis/brief.md.j2 +2 -0
- package/templates/stock_analysis/full.md.j2 +2 -0
- package/tools/_subproc.py +66 -11
- package/tools/backtest.py +73 -14
- package/tools/capabilities.py +8 -0
- package/tools/cli_budgets.json +26 -0
- package/tools/gather.py +4 -6
- package/tools/market_review.py +30 -9
- package/tools/screener.py +57 -15
- package/tools/signal_tracker.py +77 -6
- package/tools/stateless_analysis.py +630 -0
- package/tools/stock_data.py +90 -15
- package/tools/watchlist_context.py +18 -7
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# 无状态研究工具
|
|
2
|
+
|
|
3
|
+
这些工具在一次调用中计算,不维护自选股、账户、规则或历史。宿主/用户显式提供本次持仓、历史快照与正文。采集继续用已有行情、财务、公告工具。MUST 从原始结果取值、保留来源和时点;MUST NOT 补造缺失值、股票池或持仓。缺少必需输入时只索取该输入。
|
|
4
|
+
|
|
5
|
+
## 同行比较:compare_stocks
|
|
6
|
+
|
|
7
|
+
1. 确定用户提供或有明确行业依据的可比股票,调用 get_financials/get_quote/get_stock_info;必要时 resolve_stock_sectors。不要用全市场估值直接代替同行。
|
|
8
|
+
2. 传 `companies`(2..30项),每项 `{symbol, period, metrics}`。`metrics` 每项 `{name, value, unit, basis, source?}`,缺失 value=null。`period` 是指标的共同报告期或估值日;同一公司不同期间的指标应分批比较。
|
|
9
|
+
3. 核实单位。A 股 roe/net_profit_margin/gross_margin 可能是百分数,而 Yahoo roe/profit_margin 是小数;只有确定源口径时才换算并记录依据。金额单位必须含币种。不同年度、TTM 与单季、合并与母公司报表分开。
|
|
10
|
+
4. 工具只有 unit/basis/period 完全一致且至少两项有效数值时返回 ascending。它表示升序,MUST NOT 称为买入排名。亏损 PE、负 PB 和行业特有口径需另行说明,不按“越低越好”解释。
|
|
11
|
+
|
|
12
|
+
## 公告财报比较:analyze_disclosure_changes
|
|
13
|
+
|
|
14
|
+
传 `previous/current`,每项 `{text, source, period, metrics}`;正文上限200000字符,metrics 形状同上(可为空),`max_changes` 默认30、上限100。
|
|
15
|
+
|
|
16
|
+
先查询原始公告;HTML 可用 extract_article,但它默认截断正文,不能将其结果自动视为全文。PDF/OCR 由宿主文档读取能力或用户提供,MUST NOT 把标题、链接或搜索摘要冒充全文。缺少正文时仍可分析已核实的财务指标,但不要调用要求正文的比较工具。
|
|
17
|
+
|
|
18
|
+
工具输出精确到传入文本行号的变化片段、来源、截断标志和指标差异。文本差异本身不等于经营变化;Agent 要阅读上下文,说明发生了什么、与原预期的关系和可能反证,并引用原文。金额单位和 basis 一致才计算 delta;前期值大于0才计算普通增长率,亏损转盈另述。报告期可不同,但必须核实是可比同长期间(年度/同比单季等),累计季度不能直接当单季。
|
|
19
|
+
|
|
20
|
+
## 组合风险:analyze_portfolio_risk
|
|
21
|
+
|
|
22
|
+
输入 `base_currency`、基准币种 `cash>=0`、`positions`(1..100项)。持仓 `{symbol, quantity, price, currency, cost?, stop_price?, sector?, as_of?}`,现货多头;重复标的先汇总。价格/数量为正,单位由调用方确认。
|
|
23
|
+
|
|
24
|
+
可选 `fx_rates:[{currency,rate}]`,rate 表示一单位非基准币种兑换多少基准币种;基准币种隐含1,禁止重复。缺汇率不能部分汇总冒充总资产。相同币种现金先换成基准币种再输入;其他币种现金可按现金类头寸单独注明,不能当股票计算相关性。
|
|
25
|
+
|
|
26
|
+
输出总权益、现金权重、单股权重、行业/币种敞口和持仓 HHI(不含现金项)。`price_pnl_base` 是按当前汇率折算的本币价格盈亏,MUST NOT 称为包含汇兑损益的账户真实收益;缺任何成本时为null。`stop_loss_risk_base` 是从现价到止损的损失情景,缺任何止损时为null,不保证成交。
|
|
27
|
+
|
|
28
|
+
可选 `histories:[{symbol,prices:[{date,close}]}]`,工具按每对标的共同日期计算收益相关性;至少4个共同价格点、存在波动才有结果。不是组合收益率或汇率调整后的相关性;少样本、跨时区非同步收盘和缺交易日都应说明,不从相关性推断因果。缺历史时明确相关性未计算。
|
|
29
|
+
|
|
30
|
+
## 仓位预算:calculate_position_size
|
|
31
|
+
|
|
32
|
+
必需 `equity,cash,entry_price,stop_price,risk_fraction,lot_size`。风险比例0..1,止损正且低于入场价,现金0..equity,交易单位正整数。所有金额同币种。
|
|
33
|
+
|
|
34
|
+
可选 `max_position_fraction=1`(0..1)、`fee_rate=0` 与 `slippage_rate=0`(每边比例0..0.1)。买入现金按 entry×(1+slippage)×(1+fee),止损回收按 stop×(1-slippage)×(1-fee);每股差额计入风险预算。现金、风险和本次最大买入资金分别向下取整到lot,取最小数量。已有同股持仓时,先扣减用户允许的新增仓位和风险额度,不能把本次额度当成最终账户仓位限制。
|
|
35
|
+
|
|
36
|
+
lot_size 来自用户或可核实交易规则,尤其港股不能统一猜100股。工具不覆盖券商最低收费、未提供税费、跳空和流动性风险。quantity=0 表示当前约束下不足一个交易单位,MUST NOT 向上凑整。
|
|
37
|
+
|
|
38
|
+
## 事件研究:analyze_event_study
|
|
39
|
+
|
|
40
|
+
通常直接传 `symbol,benchmark,start_date,end_date,event_dates`,由共享 Python 核心取数,避免模型复述整段K线。股票与基准应属于同一市场;代码模式输出实际覆盖区间和提供方复权标记,没有标记时明确未指定,不用调用方猜测覆盖。开始日期须覆盖事件前一交易日、结束日期须覆盖最长事件窗口。也可使用下面的显式价格模式,两种模式互斥。
|
|
41
|
+
|
|
42
|
+
传 `prices,benchmark_prices`(各2..20000条 `{date:"YYYY-MM-DD",close}`)、`event_dates`(1..100个唯一日期)、`price_basis`(明确股票与基准各自复权口径)。可选 `horizons`(1..20个0..250整数,默认0/1/5/10/20)。复用 get_kline,先核实数据覆盖事件前一交易日至最大窗口结束。
|
|
43
|
+
|
|
44
|
+
事件日映射到股票序列中不早于指定日期的首日;盘后事件应显式使用下一交易日。基点是该日前一个股票交易日收盘;horizon=0表示事件当日收盘。基准必须同市场,并在两个端点都有数据,缺失时不移动事件日期。收益差是股票与基准各自买入持有收益的差(百分点),不等于因果影响。
|
|
45
|
+
|
|
46
|
+
MUST 核实完整交易日、复权、币种和股息处理。工具无法从缺行判断是停牌、节假日还是数据故障。重叠事件标识意味着样本不独立;少样本均值不能作为稳定预测。
|
|
47
|
+
|
|
48
|
+
## 回测稳健性:validate_backtest
|
|
49
|
+
|
|
50
|
+
通常传 `symbols:[代码],start_date,end_date,strategies`,Python 直接复用现有 K 线获取;不要让模型抄写数千条行情。也可使用下面的显式 datasets 模式,两种模式互斥。读取每个标的 data_coverage,实际覆盖不足时缩小结论范围;提供方未标记价格口径时须说明。代码模式共享CLI预算660秒、事件研究300秒,宿主另留退出余量。
|
|
51
|
+
|
|
52
|
+
传 `datasets`(1..5项 `{symbol,price_basis,bars}`,bars 是按日期升序的 date/open/high/low/close/volume,每组最多2000条)、`strategies`(1..5项 `{name,strategy}`,完整既有DSL的JSON对象)。可选 `train_bars=126,test_bars=63`(各30..1000)、`capital=1000000`。每组至少训练+测试长度。
|
|
53
|
+
|
|
54
|
+
通过现有 get_kline 取完整数据,读取用户策略后转换为JSON;parameters 引用复用现有引擎解析。候选应事先确定,只在用户要求优化时扩展候选,MUST NOT 看测试窗口收益后反复改参数并仍称样本外。
|
|
55
|
+
|
|
56
|
+
每个标的独立滚动训练,按训练总收益选候选(并列按输入顺序),在其后的非重叠完整测试窗口评估。返回每个候选的训练结果、选中者及测试结果;全区间 sensitivity 是样本内比较。尾部不足完整测试窗口会报告unused_tail_bars。
|
|
57
|
+
|
|
58
|
+
选择使用相同初始资金下的实际 `final_equity`,不使用展示精度舍入后的收益。所有入场/退出条件先校验,未触发的条件也不能绕过检查;坏候选单独返回 error,不中断合法候选。无法定义的 `profit_loss_ratio` 为null,不能当作0;其余数值溢出或无效Unicode输入返回JSON错误。
|
|
59
|
+
|
|
60
|
+
每个窗口从现金和重新初始化的指标开始,不拼成连续组合;MUST NOT 把各窗口年化收益平均当成实际年化。只允许next_open。关注候选错误、交易数和未平仓;收益含未平仓市值,交易统计仅含已平仓。不同标的和不同参数的收益分散程度是稳健性证据,不是自动策略认证。缺少历史股票池、退市样本、公司行为和限价数据时保留原回测限制。
|
|
61
|
+
|
|
62
|
+
## 快照比较:compare_analysis_snapshots
|
|
63
|
+
|
|
64
|
+
传 `previous/current` JSON对象;嵌套字典按JSON Pointer输出变化,数组按整体比较。批量股票应先以symbol构造字典,时间戳/采集耗时等机械字段先按需求排除,避免无意义变化。
|
|
65
|
+
|
|
66
|
+
状态为added/removed/changed/unavailable/recovered。null 或包含error的对象表示不可用;removed只表示键从输入消失,MUST NOT 自动解释为退市、卖出或业务事件。缺少历史输入时仅做当前分析;持久化、调度及通知由宿主负责。
|
|
@@ -66,7 +66,9 @@ python3 tools/screener.py screen --market A --top 20
|
|
|
66
66
|
- 建议结合个股深度分析后再做决策
|
|
67
67
|
```
|
|
68
68
|
|
|
69
|
-
|
|
69
|
+
每个候选必须读取 `factor_coverage.available/missing/ratio/effective_weights`。ratio 是已覆盖因子的原始权重之和;L1 与 L2 的 effective_weights 均按可用因子重归一化,缺失或非有限值不按零分计,排序前先按实际可用因子计算综合分,effective_scores 保留实际参与计算的各组分数;用 sum(effective_scores[g] × effective_weights[g]) × market_sentiment.multiplier 核对 scores.final(四舍五入至一位)。scores.momentum 仍保留 L1 参考值,L2 贡献以 effective_scores.momentum 为准。相同分数但缺失因子不同的候选不能视为同等证据,报告缺失项和覆盖度,不以 `enriched=true` 代替覆盖判断。`scores.final=null` 表示没有足够因子计算,不能作为高分候选推荐。
|
|
70
|
+
|
|
71
|
+
完成标准:列出实际生效的市场、过滤条件、排序口径和候选数量,且推荐理由只使用返回数据,并说明因子覆盖。
|
|
70
72
|
|
|
71
73
|
## 策略模板
|
|
72
74
|
|
|
@@ -93,3 +95,5 @@ dividend_report_date: "2025-12-31"
|
|
|
93
95
|
### 补充数据覆盖
|
|
94
96
|
|
|
95
97
|
股息筛选支持 A/HK:A 股为报告期已实施方案股息率,HK 为 HKEX/LSEG 年度股息率。两者都不是当前价 TTM;使用结果的 yield_basis 和 report_date,缺失不当零。
|
|
98
|
+
|
|
99
|
+
选股的 top 必须为正整数;超时预算按候选数量与采集批次计算。
|
|
@@ -9,6 +9,10 @@ description: 策略回测:定义或读取 AlphaEvo YAML 策略,回测收益
|
|
|
9
9
|
|
|
10
10
|
解读回测时,`period` 是实际取得的数据期间,`requested_period` 保留用户请求,`warnings` 提示两者差异。年化收益按实际期间计算;覆盖不完整时明确说明,不能声称完成了完整请求期间的验证。买入预算包含佣金,`position.size` 为 0..1 的资金比例。
|
|
11
11
|
|
|
12
|
+
## 无状态分析扩展
|
|
13
|
+
|
|
14
|
+
滚动样本外、参数敏感性或跨标的验证时,MUST 读取[无状态研究工具](../stock-analysis/references/stateless-analysis.md)的 validate_backtest 分支。复用 get_kline,传完整OHLCV和事先确定的DSL候选;样本内sensitivity不等于样本外验证。
|
|
15
|
+
|
|
12
16
|
## 工作流程
|
|
13
17
|
|
|
14
18
|
### 第一步:策略定义
|
|
@@ -42,6 +46,7 @@ exit:
|
|
|
42
46
|
take_profit: 0.15 # 止盈比例
|
|
43
47
|
|
|
44
48
|
simulation:
|
|
49
|
+
execution: next_open # 默认:前日收盘信号,次日开盘成交
|
|
45
50
|
slippage: 0.001 # 滑点(买卖双边),默认 0.001
|
|
46
51
|
commission: 0.00025 # 佣金(买卖双边,券商自定;A股常见万2.5),默认 0.0015
|
|
47
52
|
stamp_tax: 0.0005 # 印花税(仅卖出收;A股 0.05%,其他市场按当下实际费率,无则省略),默认 0
|
|
@@ -70,6 +75,10 @@ python3 tools/backtest.py run <path.yaml> <symbol> --start <date> --end <date>
|
|
|
70
75
|
- `--start` / `--end`:回测时间范围(可选,默认近一年)
|
|
71
76
|
- 初始资金默认 100 万,可用 `--capital` 调整
|
|
72
77
|
|
|
78
|
+
默认 `simulation.execution=next_open`:信号在前一交易日收盘确定,下一有效交易日按开盘价加滑点成交;交易同时保留 `signal_date` 与实际 `date`。最后一天新信号没有后续开盘时不能成交。需要复现旧结果时显式使用 `same_close`,并说明它按信号日收盘成交,可能高估可执行收益。零/缺失成交量(已有 volume 字段时)阻止成交;一字涨跌停仅在输入提供同口径历史 `limit_up/limit_down` 时保守阻止,不猜 ST、IPO 或 ETF 限价。
|
|
79
|
+
|
|
80
|
+
读取 `execution.model/blocked_orders/unfilled_order/limitations` 并说明未成交情况。止损、止盈和移动止损均是收盘信号,不承诺盘中按阈值成交;次日跳空按实际开盘价计算。没有历史限价数据时,不能声称已验证涨跌停可成交性。
|
|
81
|
+
|
|
73
82
|
### 第三步:分析结果
|
|
74
83
|
|
|
75
84
|
回测完成后,按共享框架的风险单位和回测约束解释原始结果。针对本工具:
|
|
@@ -4,6 +4,13 @@
|
|
|
4
4
|
{% set t = temperature or {} -%}
|
|
5
5
|
**温度:** {{ m.signal_emoji(t.signal) }} {{ m.fmt_num(t.score, 1) }}/100 | **信号灯:** {{ m.signal_label(t.signal) }} | **策略姿态:** {{ m.stance_label(strategy_stance) }}
|
|
6
6
|
|
|
7
|
+
{% if data_quality and data_quality.limitations -%}
|
|
8
|
+
**数据限制:**
|
|
9
|
+
{% for limitation in data_quality.limitations %}
|
|
10
|
+
- {{ limitation }}
|
|
11
|
+
{% endfor %}
|
|
12
|
+
{%- endif %}
|
|
13
|
+
|
|
7
14
|
{% if regime and (regime.regime_cn or regime.regime) -%}
|
|
8
15
|
> **市场阶段:** {{ regime.regime_cn or regime.regime }}{% if regime.confidence is not none %}(置信度 {{ m.fmt_num(regime.confidence) }}){% endif %}{% if regime.recommended_skills %} | **推荐策略:** {{ regime.recommended_skills | join('、') }}{% endif %}
|
|
9
16
|
{%- endif %}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{% if data_quality or evidence %}
|
|
2
|
+
|
|
3
|
+
### 本次数据与限制
|
|
4
|
+
|
|
5
|
+
{% for item in evidence or [] %}
|
|
6
|
+
- **{{ item.label or '数据依据' }}:** {{ item.value if item.value is not none else '未知' }}(来源:{{ item.source or '未标明' }};数据时间:{{ item.as_of or '未知' }})
|
|
7
|
+
{% endfor %}
|
|
8
|
+
{% for limitation in (data_quality or {}).limitations or [] %}
|
|
9
|
+
- {{ limitation }}
|
|
10
|
+
{% endfor %}
|
|
11
|
+
{% endif %}
|
package/tools/_subproc.py
CHANGED
|
@@ -14,10 +14,44 @@ import re
|
|
|
14
14
|
import socket
|
|
15
15
|
import subprocess
|
|
16
16
|
import sys
|
|
17
|
+
from pathlib import Path
|
|
17
18
|
|
|
18
19
|
TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
19
20
|
|
|
20
21
|
|
|
22
|
+
COMMAND_BUDGETS = json.loads((Path(TOOLS_DIR) / "cli_budgets.json").read_text(encoding="utf-8"))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def command_timeout(script: str, args: list) -> int:
|
|
26
|
+
key = f"{Path(script).name}:{args[0] if args else ''}"
|
|
27
|
+
budget = COMMAND_BUDGETS.get(key, COMMAND_BUDGETS["default"])
|
|
28
|
+
|
|
29
|
+
def option(name, default):
|
|
30
|
+
return args[args.index(name) + 1] if name in args and args.index(name) + 1 < len(args) else default
|
|
31
|
+
|
|
32
|
+
def positive_int(value, default):
|
|
33
|
+
return int(value) if str(value).isdigit() and int(value) > 0 else default
|
|
34
|
+
|
|
35
|
+
if key == "screener.py:screen":
|
|
36
|
+
top = positive_int(option("--top", 20), 20)
|
|
37
|
+
workers = COMMAND_BUDGETS["l2_workers"]
|
|
38
|
+
budget += math.ceil(top / workers) * COMMAND_BUDGETS["l2_batch"]
|
|
39
|
+
elif key in ("watchlist.py:analyze", "watchlist_context.py:build"):
|
|
40
|
+
count = len([s for s in str(args[1] if len(args) > 1 else "").split(",") if s.strip()])
|
|
41
|
+
workers = positive_int(option("--workers", 3), 3)
|
|
42
|
+
budget += math.ceil(count / workers) * COMMAND_BUDGETS["watchlist_item"]
|
|
43
|
+
if key == "watchlist_context.py:build":
|
|
44
|
+
budget += math.ceil(count / min(workers * 2, 6)) * COMMAND_BUDGETS["anomaly_detect.py:detect"]
|
|
45
|
+
if "--include-market-review" in args:
|
|
46
|
+
budget += 3 * COMMAND_BUDGETS["market_review.py:review"] + 30
|
|
47
|
+
elif key == "market_review.py:review" and option("--market", "A") == "all":
|
|
48
|
+
budget = 3 * budget + 30
|
|
49
|
+
elif key == "technical.py:analyze" and "--periods" in args:
|
|
50
|
+
periods = {p.strip() for p in str(option("--periods", "daily")).split(",") if p.strip()}
|
|
51
|
+
budget *= len(periods | {option("--period", "daily")})
|
|
52
|
+
return budget
|
|
53
|
+
|
|
54
|
+
|
|
21
55
|
def find_python() -> str:
|
|
22
56
|
"""Prefer the repo .venv interpreter; fall back to the current one.
|
|
23
57
|
|
|
@@ -29,12 +63,20 @@ def find_python() -> str:
|
|
|
29
63
|
return venv if os.path.exists(venv) else sys.executable
|
|
30
64
|
|
|
31
65
|
|
|
32
|
-
def run_tool(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
66
|
+
def run_tool(
|
|
67
|
+
script: str, args: list, timeout: int | None = None, parse_json: bool = True, preserve_errors: bool = False
|
|
68
|
+
):
|
|
69
|
+
"""Run a sibling CLI; opt in to safe error data, otherwise keep the None sentinel."""
|
|
70
|
+
|
|
71
|
+
if timeout is None:
|
|
72
|
+
timeout = command_timeout(script, args)
|
|
73
|
+
|
|
74
|
+
def failure(message, code):
|
|
75
|
+
if not preserve_errors:
|
|
76
|
+
return None
|
|
77
|
+
payload = {"error": scrub_secrets(message), "error_code": code}
|
|
78
|
+
return payload if parse_json else json.dumps(payload, ensure_ascii=False)
|
|
79
|
+
|
|
38
80
|
try:
|
|
39
81
|
r = subprocess.run(
|
|
40
82
|
[find_python(), os.path.join(TOOLS_DIR, script), *args],
|
|
@@ -43,11 +85,24 @@ def run_tool(script: str, args: list, timeout: int = 30, parse_json: bool = True
|
|
|
43
85
|
encoding="utf-8",
|
|
44
86
|
timeout=timeout,
|
|
45
87
|
)
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
88
|
+
raw = r.stdout.strip()
|
|
89
|
+
if not raw:
|
|
90
|
+
return failure("Python command returned no JSON", "empty_output")
|
|
91
|
+
if not preserve_errors:
|
|
92
|
+
return (json.loads(raw) if parse_json else raw) if r.returncode == 0 else None
|
|
93
|
+
try:
|
|
94
|
+
payload = json.loads(raw)
|
|
95
|
+
except ValueError:
|
|
96
|
+
return failure("Python command returned invalid JSON", "invalid_json")
|
|
97
|
+
if isinstance(payload, dict) and isinstance(payload.get("error"), str):
|
|
98
|
+
return failure(payload["error"], payload.get("error_code", "tool_error"))
|
|
99
|
+
if r.returncode != 0:
|
|
100
|
+
return failure(f"Python command failed (exit {r.returncode})", "process_failed")
|
|
101
|
+
return payload if parse_json else raw
|
|
102
|
+
except subprocess.TimeoutExpired:
|
|
103
|
+
return failure(f"Command timed out after {timeout}s", "timeout")
|
|
104
|
+
except Exception as exc:
|
|
105
|
+
return failure(f"Python command failed ({type(exc).__name__})", "process_failed")
|
|
51
106
|
|
|
52
107
|
|
|
53
108
|
def utf8_stdio() -> None:
|
package/tools/backtest.py
CHANGED
|
@@ -70,9 +70,9 @@ def fetch_kline(symbol: str, start: str | None, end: str | None) -> pd.DataFrame
|
|
|
70
70
|
d1 = datetime.strptime(end, "%Y-%m-%d")
|
|
71
71
|
count = max((datetime.now() - d0).days + 60, (d1 - d0).days + 60, 300)
|
|
72
72
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
73
|
+
data = run_tool(
|
|
74
|
+
"stock_data.py", ["kline", symbol, "--period", "daily", "--count", str(count)], preserve_errors=True
|
|
75
|
+
)
|
|
76
76
|
if data is None:
|
|
77
77
|
raise RuntimeError(f"Failed to fetch kline for {symbol}")
|
|
78
78
|
if isinstance(data, dict) and "error" in data:
|
|
@@ -191,7 +191,7 @@ def get_indicator(
|
|
|
191
191
|
|
|
192
192
|
|
|
193
193
|
def check_condition(series: pd.Series, operator: str, value: float, idx: int) -> bool:
|
|
194
|
-
if idx <
|
|
194
|
+
if idx < 0 or idx >= len(series):
|
|
195
195
|
return False
|
|
196
196
|
cur = float(series.iloc[idx])
|
|
197
197
|
ops = {
|
|
@@ -203,6 +203,8 @@ def check_condition(series: pd.Series, operator: str, value: float, idx: int) ->
|
|
|
203
203
|
}
|
|
204
204
|
if operator in ops:
|
|
205
205
|
return ops[operator]
|
|
206
|
+
if idx == 0:
|
|
207
|
+
return False
|
|
206
208
|
prev = float(series.iloc[idx - 1])
|
|
207
209
|
if operator == "cross_above":
|
|
208
210
|
return prev <= value and cur > value
|
|
@@ -256,9 +258,17 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
|
|
|
256
258
|
raise ValueError("capital must be finite and positive")
|
|
257
259
|
if not np.isfinite(df["close"]).all() or (df["close"] <= 0).any():
|
|
258
260
|
raise ValueError("backtest prices must be finite and positive")
|
|
261
|
+
dates = pd.to_datetime(df["date"], errors="coerce")
|
|
262
|
+
if dates.isna().any() or dates.duplicated().any() or not dates.is_monotonic_increasing:
|
|
263
|
+
raise ValueError("backtest dates must be valid, unique and increasing")
|
|
259
264
|
market = detect_market(symbol)
|
|
260
265
|
lot_size = 100 if market == "A" else 1
|
|
261
266
|
sim_cfg = strategy.get("simulation", {})
|
|
267
|
+
execution = sim_cfg.get("execution", "next_open")
|
|
268
|
+
if execution not in ("next_open", "same_close"):
|
|
269
|
+
raise ValueError("simulation.execution must be next_open or same_close")
|
|
270
|
+
if execution == "next_open" and ("open" not in df or not np.isfinite(df["open"]).all() or (df["open"] <= 0).any()):
|
|
271
|
+
raise ValueError("next_open requires finite positive open prices")
|
|
262
272
|
slippage = float(sim_cfg.get("slippage", 0.001))
|
|
263
273
|
commission = float(sim_cfg.get("commission", 0.0015))
|
|
264
274
|
tax_raw = sim_cfg.get("stamp_tax") # sell-side only (e.g. A股印花税); absent/empty = 0 (disabled)
|
|
@@ -293,19 +303,43 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
|
|
|
293
303
|
trade_id = 0
|
|
294
304
|
commission_paid = 0.0
|
|
295
305
|
stamp_tax_paid = 0.0
|
|
306
|
+
pending = None
|
|
307
|
+
blocked_orders = 0
|
|
308
|
+
|
|
309
|
+
def tradable(row, side):
|
|
310
|
+
if "volume" in row and (not pd.notna(row["volume"]) or row["volume"] <= 0):
|
|
311
|
+
return False
|
|
312
|
+
limit = row.get("limit_up" if side == "buy" else "limit_down")
|
|
313
|
+
# shortcut: limits need supplied historical prices; add verified limit series before enforcing provider bars.
|
|
314
|
+
return not (
|
|
315
|
+
limit is not None
|
|
316
|
+
and pd.notna(limit)
|
|
317
|
+
and row.get("high") == row.get("low")
|
|
318
|
+
and abs(float(row["high"]) - float(limit)) < 0.005
|
|
319
|
+
)
|
|
296
320
|
|
|
297
321
|
for i in range(1, len(df)):
|
|
298
322
|
row = df.iloc[i]
|
|
299
323
|
date_str = str(row["date"].date()) if hasattr(row["date"], "date") else str(row["date"])
|
|
300
|
-
|
|
324
|
+
signal_idx = i if execution == "same_close" else i - 1
|
|
325
|
+
price = float(df["close"].iloc[signal_idx])
|
|
326
|
+
fill_price = float(row["close"] if execution == "same_close" else row["open"])
|
|
327
|
+
signal_date = str(pd.Timestamp(df["date"].iloc[signal_idx]).date())
|
|
301
328
|
|
|
302
329
|
if holding is None:
|
|
303
|
-
hit, reason = evaluate_conditions(df, entry_conditions, entry_logic,
|
|
304
|
-
if
|
|
305
|
-
|
|
330
|
+
hit, reason = evaluate_conditions(df, entry_conditions, entry_logic, signal_idx, cache)
|
|
331
|
+
if pending:
|
|
332
|
+
hit, reason, signal_date = True, pending[0], pending[1]
|
|
333
|
+
if hit and not tradable(row, "buy"):
|
|
334
|
+
pending = (reason, signal_date)
|
|
335
|
+
blocked_orders += 1
|
|
336
|
+
if hit and tradable(row, "buy"):
|
|
337
|
+
pending = None
|
|
338
|
+
buy_price = fill_price * (1 + slippage)
|
|
306
339
|
buy_amount = cash * size_frac
|
|
307
340
|
shares = int(buy_amount / (buy_price * (1 + commission)) / lot_size) * lot_size
|
|
308
341
|
if shares <= 0:
|
|
342
|
+
equity_curve.append({"date": date_str, "equity": round(cash, 2)})
|
|
309
343
|
continue
|
|
310
344
|
cost = shares * buy_price * (1 + commission)
|
|
311
345
|
commission_paid += shares * buy_price * commission
|
|
@@ -317,13 +351,14 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
|
|
|
317
351
|
"buy_price": round(buy_price, 4),
|
|
318
352
|
"shares": shares,
|
|
319
353
|
"cost": cost,
|
|
320
|
-
"peak_close":
|
|
354
|
+
"peak_close": float(row["close"]),
|
|
321
355
|
}
|
|
322
356
|
trades.append(
|
|
323
357
|
{
|
|
324
358
|
"id": trade_id,
|
|
325
359
|
"type": "buy",
|
|
326
360
|
"date": date_str,
|
|
361
|
+
"signal_date": signal_date,
|
|
327
362
|
"price": round(buy_price, 2),
|
|
328
363
|
"shares": shares,
|
|
329
364
|
"amount": round(cost, 2),
|
|
@@ -348,10 +383,16 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
|
|
|
348
383
|
elif pnl_pct >= take_profit:
|
|
349
384
|
sell, reason = True, f"止盈触发 ({round(pnl_pct * 100, 1)}% >= {round(take_profit * 100, 1)}%)"
|
|
350
385
|
elif exit_conditions:
|
|
351
|
-
sell, reason = evaluate_conditions(df, exit_conditions, exit_logic,
|
|
352
|
-
|
|
353
|
-
if
|
|
354
|
-
|
|
386
|
+
sell, reason = evaluate_conditions(df, exit_conditions, exit_logic, signal_idx, cache)
|
|
387
|
+
|
|
388
|
+
if pending:
|
|
389
|
+
sell, reason, signal_date = True, pending[0], pending[1]
|
|
390
|
+
if sell and not tradable(row, "sell"):
|
|
391
|
+
pending = (reason, signal_date)
|
|
392
|
+
blocked_orders += 1
|
|
393
|
+
if sell and tradable(row, "sell"):
|
|
394
|
+
pending = None
|
|
395
|
+
sell_price = fill_price * (1 - slippage)
|
|
355
396
|
gross = holding["shares"] * sell_price
|
|
356
397
|
proceeds = gross * (1 - commission - stamp_tax)
|
|
357
398
|
commission_paid += gross * commission
|
|
@@ -371,6 +412,7 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
|
|
|
371
412
|
"id": trade_id_sell,
|
|
372
413
|
"type": "sell",
|
|
373
414
|
"date": date_str,
|
|
415
|
+
"signal_date": signal_date,
|
|
374
416
|
"price": round(sell_price, 2),
|
|
375
417
|
"shares": holding["shares"],
|
|
376
418
|
"amount": round(proceeds, 2),
|
|
@@ -384,7 +426,7 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
|
|
|
384
426
|
|
|
385
427
|
equity = cash
|
|
386
428
|
if holding:
|
|
387
|
-
equity += holding["shares"] *
|
|
429
|
+
equity += holding["shares"] * float(row["close"])
|
|
388
430
|
equity_curve.append({"date": date_str, "equity": round(equity, 2)})
|
|
389
431
|
|
|
390
432
|
if holding:
|
|
@@ -396,6 +438,22 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
|
|
|
396
438
|
"trades": trades,
|
|
397
439
|
"equity_curve": equity_curve,
|
|
398
440
|
"final_equity": equity_curve[-1]["equity"] if equity_curve else capital,
|
|
441
|
+
"execution": {
|
|
442
|
+
"model": execution,
|
|
443
|
+
"blocked_orders": blocked_orders,
|
|
444
|
+
"unfilled_order": pending is not None,
|
|
445
|
+
"limitations": [
|
|
446
|
+
"Stops are close signals, not intraday guaranteed stop prices",
|
|
447
|
+
"Historical price limits are enforced only when limit_up/limit_down are supplied",
|
|
448
|
+
"Queue priority, partial fills, market impact and settlement are not simulated",
|
|
449
|
+
"Adjusted OHLC are synthetic prices; corporate actions and cash dividends are not simulated separately",
|
|
450
|
+
]
|
|
451
|
+
+ (
|
|
452
|
+
["same_close uses the signal day's close and can overstate executable returns"]
|
|
453
|
+
if execution == "same_close"
|
|
454
|
+
else []
|
|
455
|
+
),
|
|
456
|
+
},
|
|
399
457
|
"costs": {
|
|
400
458
|
"slippage": slippage,
|
|
401
459
|
"commission": commission,
|
|
@@ -680,6 +738,7 @@ def run_backtest(strategy_path: str, symbol: str, start: str | None, end: str |
|
|
|
680
738
|
"initial_capital": capital,
|
|
681
739
|
"final_capital": final,
|
|
682
740
|
"costs": sim["costs"],
|
|
741
|
+
"execution": sim["execution"],
|
|
683
742
|
"metrics": metrics,
|
|
684
743
|
"trades": sim["trades"],
|
|
685
744
|
"equity_curve": sample_curve(sim["equity_curve"]),
|
package/tools/capabilities.py
CHANGED
|
@@ -18,6 +18,14 @@ ALL_MARKETS = ["A", "HK", "US", "JP", "KR", "TW"]
|
|
|
18
18
|
|
|
19
19
|
# tool_name -> {markets: [supported markets], reason: str for unsupported}
|
|
20
20
|
TOOL_MATRIX = {
|
|
21
|
+
# Explicit-input analytics; data acquisition retains each provider's market limits.
|
|
22
|
+
"compare_stocks": {"markets": ALL_MARKETS, "reason": None},
|
|
23
|
+
"analyze_disclosure_changes": {"markets": ALL_MARKETS, "reason": None},
|
|
24
|
+
"analyze_portfolio_risk": {"markets": ALL_MARKETS, "reason": None},
|
|
25
|
+
"calculate_position_size": {"markets": ALL_MARKETS, "reason": None},
|
|
26
|
+
"analyze_event_study": {"markets": ALL_MARKETS, "reason": None},
|
|
27
|
+
"validate_backtest": {"markets": ALL_MARKETS, "reason": None},
|
|
28
|
+
"compare_analysis_snapshots": {"markets": ALL_MARKETS, "reason": None},
|
|
21
29
|
"get_announcements": {"markets": ["A"], "reason": "cninfo A-share disclosures only"},
|
|
22
30
|
"get_us_holdings": {"markets": ["US"], "reason": "SEC manager holdings or verified issuer-CUSIP reverse lookup"},
|
|
23
31
|
"get_insider_trades": {"markets": ["US"], "reason": "SEC issuer Form 4 only"},
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"default": 120,
|
|
3
|
+
"stock_data.py:market_snapshot": 240,
|
|
4
|
+
"screener.py:screen": 390,
|
|
5
|
+
"sec_edgar.py:holdings": 300,
|
|
6
|
+
"technical.py:analyze": 150,
|
|
7
|
+
"market_regime.py:detect": 270,
|
|
8
|
+
"backtest.py:run": 270,
|
|
9
|
+
"risk_screening.py:screen": 450,
|
|
10
|
+
"signal_tracker.py:evaluate": 300,
|
|
11
|
+
"stock_data.py:market_stats": 240,
|
|
12
|
+
"market_review.py:review": 300,
|
|
13
|
+
"anomaly_detect.py:detect": 390,
|
|
14
|
+
"position_context.py:analyze": 300,
|
|
15
|
+
"alert_rules.py:check": 480,
|
|
16
|
+
"technical.py:calculate_ma": 150,
|
|
17
|
+
"value_data.py:dividends": 300,
|
|
18
|
+
"value_data.py:etf_details": 450,
|
|
19
|
+
"watchlist.py:analyze": 30,
|
|
20
|
+
"watchlist_context.py:build": 30,
|
|
21
|
+
"l2_batch": 360,
|
|
22
|
+
"l2_workers": 6,
|
|
23
|
+
"watchlist_item": 480,
|
|
24
|
+
"stateless_analysis.py:event_study": 300,
|
|
25
|
+
"stateless_analysis.py:robustness": 660
|
|
26
|
+
}
|
package/tools/gather.py
CHANGED
|
@@ -8,15 +8,13 @@ from concurrent.futures import ThreadPoolExecutor
|
|
|
8
8
|
from pathlib import Path
|
|
9
9
|
|
|
10
10
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
11
|
-
from _subproc import json_safe, run_tool, utf8_stdio # noqa: E402
|
|
11
|
+
from _subproc import command_timeout, json_safe, run_tool, utf8_stdio # noqa: E402
|
|
12
12
|
from stock_data import detect_market # noqa: E402
|
|
13
13
|
from technical import analyze_records # noqa: E402
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
def _run(script, args, timeout=60):
|
|
19
|
-
return run_tool(script, args, timeout=timeout, parse_json=False)
|
|
16
|
+
def _run(script, args, timeout=None):
|
|
17
|
+
return run_tool(script, args, timeout=timeout, parse_json=False, preserve_errors=True)
|
|
20
18
|
|
|
21
19
|
|
|
22
20
|
def _news_query(symbol: str) -> str:
|
|
@@ -74,7 +72,7 @@ def gather_screen(market: str = "A", top: int = 20, config: str | None = None) -
|
|
|
74
72
|
args = ["screen", "--market", market, "--top", str(top)]
|
|
75
73
|
if config:
|
|
76
74
|
args += ["--config", config]
|
|
77
|
-
raw = _run("screener.py", args, timeout=
|
|
75
|
+
raw = _run("screener.py", args, timeout=command_timeout("screener.py", args))
|
|
78
76
|
return {"screen_results": _parse_json(raw)}
|
|
79
77
|
|
|
80
78
|
|
package/tools/market_review.py
CHANGED
|
@@ -10,6 +10,7 @@ tmp_path).
|
|
|
10
10
|
|
|
11
11
|
import argparse
|
|
12
12
|
import json
|
|
13
|
+
import math
|
|
13
14
|
import os
|
|
14
15
|
import sys
|
|
15
16
|
from concurrent.futures import ThreadPoolExecutor
|
|
@@ -17,11 +18,16 @@ from pathlib import Path
|
|
|
17
18
|
|
|
18
19
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
19
20
|
|
|
21
|
+
from _subproc import command_timeout, utf8_stdio
|
|
20
22
|
from _subproc import run_tool as _run_tool
|
|
21
|
-
from _subproc import utf8_stdio
|
|
22
23
|
from signal_tracker import _append_record, _load_records, _store_lock
|
|
23
24
|
|
|
24
25
|
|
|
26
|
+
def _index_change(row):
|
|
27
|
+
value = row.get("change_pct") if isinstance(row, dict) else None
|
|
28
|
+
return value if isinstance(value, (int, float)) and not isinstance(value, bool) and math.isfinite(value) else None
|
|
29
|
+
|
|
30
|
+
|
|
25
31
|
def calc_temperature(stats: dict, indices: list) -> dict:
|
|
26
32
|
"""Calculate market temperature score from breadth, index change, and limit data."""
|
|
27
33
|
scores = []
|
|
@@ -37,7 +43,7 @@ def calc_temperature(stats: dict, indices: list) -> dict:
|
|
|
37
43
|
weights.append(0.45)
|
|
38
44
|
|
|
39
45
|
if indices and isinstance(indices, list):
|
|
40
|
-
changes = [
|
|
46
|
+
changes = [value for row in indices if (value := _index_change(row)) is not None]
|
|
41
47
|
if changes:
|
|
42
48
|
avg_change = sum(changes) / len(changes)
|
|
43
49
|
index_score = max(0, min(100, 50 + avg_change * 12))
|
|
@@ -54,7 +60,7 @@ def calc_temperature(stats: dict, indices: list) -> dict:
|
|
|
54
60
|
weights.append(0.20)
|
|
55
61
|
|
|
56
62
|
if not scores:
|
|
57
|
-
return {"score":
|
|
63
|
+
return {"score": None, "level": "unknown", "signal": "unknown"}
|
|
58
64
|
|
|
59
65
|
total_weight = sum(weights)
|
|
60
66
|
temperature = sum(s * w for s, w in zip(scores, weights)) / total_weight
|
|
@@ -69,8 +75,8 @@ def calc_temperature(stats: dict, indices: list) -> dict:
|
|
|
69
75
|
return {"score": round(temperature, 1), "level": level, "signal": signal}
|
|
70
76
|
|
|
71
77
|
|
|
72
|
-
def _signal_to_stance(signal: str) -> str:
|
|
73
|
-
return {"green": "offensive", "yellow": "balanced", "red": "defensive"}.get(signal
|
|
78
|
+
def _signal_to_stance(signal: str) -> str | None:
|
|
79
|
+
return {"green": "offensive", "yellow": "balanced", "red": "defensive"}.get(signal)
|
|
74
80
|
|
|
75
81
|
|
|
76
82
|
def review_market(market: str = "A") -> dict:
|
|
@@ -100,10 +106,7 @@ def review_market(market: str = "A") -> dict:
|
|
|
100
106
|
results = {}
|
|
101
107
|
with ThreadPoolExecutor(max_workers=5) as executor:
|
|
102
108
|
futures = {
|
|
103
|
-
|
|
104
|
-
# (screener.fetch_snapshot); the default 30s would silently degrade the
|
|
105
|
-
# temperature to the neutral 50 fallback on a weak network.
|
|
106
|
-
key: executor.submit(_run_tool, script, args, 240 if key == "stats" else 30)
|
|
109
|
+
key: executor.submit(_run_tool, script, args, command_timeout(script, args))
|
|
107
110
|
for key, (script, args) in tasks.items()
|
|
108
111
|
}
|
|
109
112
|
for key, future in futures.items():
|
|
@@ -113,6 +116,22 @@ def review_market(market: str = "A") -> dict:
|
|
|
113
116
|
results[key] = None
|
|
114
117
|
|
|
115
118
|
temperature = calc_temperature(results.get("stats"), results.get("indices"))
|
|
119
|
+
warnings = [
|
|
120
|
+
f"{key} data unavailable"
|
|
121
|
+
for key, value in results.items()
|
|
122
|
+
if not value
|
|
123
|
+
or (isinstance(value, dict) and "error" in value)
|
|
124
|
+
or (key == "indices" and isinstance(value, list) and any(_index_change(row) is None for row in value))
|
|
125
|
+
or (key == "sectors" and isinstance(value, dict) and not value.get("top") and not value.get("bottom"))
|
|
126
|
+
or (
|
|
127
|
+
isinstance(value, list)
|
|
128
|
+
and any(
|
|
129
|
+
isinstance(row, dict) and ("error" in row or row.get("note") == "data unavailable") for row in value
|
|
130
|
+
)
|
|
131
|
+
)
|
|
132
|
+
]
|
|
133
|
+
if temperature["score"] is None:
|
|
134
|
+
warnings.append("Insufficient market data to determine temperature or strategy stance")
|
|
116
135
|
|
|
117
136
|
output = {
|
|
118
137
|
"market": market,
|
|
@@ -124,6 +143,8 @@ def review_market(market: str = "A") -> dict:
|
|
|
124
143
|
"regime": results.get("regime"),
|
|
125
144
|
"temperature": temperature,
|
|
126
145
|
"strategy_stance": _signal_to_stance(temperature["signal"]),
|
|
146
|
+
"warnings": warnings,
|
|
147
|
+
"data_quality": {"flags": ["partial_data"] if warnings else [], "limitations": warnings},
|
|
127
148
|
}
|
|
128
149
|
return output
|
|
129
150
|
|