dsh-stock-analysis 0.2.5 → 0.2.6

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.
@@ -7,6 +7,8 @@ description: 策略回测:定义或读取 AlphaEvo YAML 策略,回测收益
7
7
 
8
8
  开始前读取[短线交易决策框架](../stock-analysis/references/short-term-trading.md)的风险单位和回测约束。回测用于反证策略,不把样本内收益包装成预测。
9
9
 
10
+ 解读回测时,`period` 是实际取得的数据期间,`requested_period` 保留用户请求,`warnings` 提示两者差异。年化收益按实际期间计算;覆盖不完整时明确说明,不能声称完成了完整请求期间的验证。买入预算包含佣金,`position.size` 为 0..1 的资金比例。
11
+
10
12
  ## 工作流程
11
13
 
12
14
  ### 第一步:策略定义
@@ -77,6 +79,8 @@ python3 tools/backtest.py run <path.yaml> <symbol> --start <date> --end <date>
77
79
  - 使用用户指定的互斥日期区间分别运行样本内和样本外回测。
78
80
  - 成本假设引用结果中的 `costs` 字段(实际生效的佣金/滑点/印花税费率与实付费用合计),未模拟的费用与成交约束列入限制。
79
81
 
82
+ 趋势策略或用户要求肥尾收益诊断时,读取 [肥尾度量与趋势收益诊断](../bull-trend/references/fat-tails.md)的盈利集中度部分,基于平仓 `pnl` 计算大赢家贡献和删除敏感性。只有用户询问市场收益分布时才进入 κ 分支。
83
+
80
84
  未模拟的结算、涨跌停、复权或特定市场费用列入限制,结论限定为该模拟条件下的历史表现。
81
85
 
82
86
  ### 第四步:诊断
@@ -7,6 +7,8 @@ description: 智堡投研检索:查找机构研报、财报公告、电话会
7
7
 
8
8
  智堡 MCP 涵盖海外投行研报、国内券商研究、资管报告、央行/政府文献、电话会纪要和财报公告。首先检查这些 MCP 工具是否可用;不可用时直接改用 `search_comprehensive_intel`。
9
9
 
10
+ 港股估值功课调用 `get_valuation_history(symbol, years=10)`;配置 `LIXINGER_TOKEN` 时优先理杏仁非金融公司每日 PE-TTM/PB,失败回退百度。以返回的实际窗口和正值样本计算分位及 Band,不混接同一指标的不同来源,不将缺失解释为低估。
11
+
10
12
  ## 可用工具
11
13
 
12
14
  智堡 MCP 提供以下工具(通过 `mcp__wisburg-mcp-server__` 前缀调用):
@@ -7,11 +7,14 @@
7
7
  {% if risk_screening and risk_screening.veto_buy %}
8
8
  ⚠️ **一票否决** — 风险筛查触发,建议谨慎。
9
9
  {% endif %}
10
+ {% if risk_screening and (risk_screening.data_complete is sameas false or risk_screening.risk_level == 'unknown') %}
11
+ ⚠️ **数据不完整** — 当前风险筛查不能判定为低风险。
12
+ {% endif %}
10
13
 
11
14
  {% set dp = data_perspective or {} -%}
12
15
  {% set pp = dp.price_position or {} -%}
13
16
  {% set ts = dp.trend_status or {} -%}
14
- - 📈 **趋势:** {{ ts.ma_alignment or '-' }}{% if ts.is_bullish is not none %}({{ '看多' if ts.is_bullish else '看空' }}){% endif %}
17
+ - 📈 **趋势:** {{ ts.ma_alignment or '-' }}{% if ts.is_bullish is boolean %}({{ '看多' if ts.is_bullish else '看空' }}){% endif %}
15
18
  - 💰 **价位:** 现价 {{ m.fmt_num(pp.current_price) }} | 支撑 {{ m.fmt_num(pp.support_level) }} | 压力 {{ m.fmt_num(pp.resistance_level) }}
16
19
 
17
20
  {% set bp = battle_plan or {} -%}
@@ -26,12 +26,15 @@
26
26
  ## 风险筛查
27
27
 
28
28
  {% if risk_screening -%}
29
- - **风险等级:** {{ m.risk_emoji(risk_screening.risk_level) }} {{ risk_screening.risk_level or '-' }} | **风险分:** {{ risk_screening.risk_score }}/100
29
+ - **风险等级:** {{ m.risk_emoji(risk_screening.risk_level) }} {{ risk_screening.risk_level or '-' }} | **风险分:** {{ m.fmt_num(risk_screening.risk_score, 0) }}/100
30
+ {% if risk_screening.data_complete is sameas false or risk_screening.risk_level == 'unknown' -%}
31
+ - **数据不完整:** {{ risk_screening.warnings | join(';') }},当前结果不能判定为低风险。
32
+ {% endif %}
30
33
  {% if risk_screening.flags -%}
31
34
  - **风险标记:**
32
35
  {% for f in risk_screening.flags %} - [{{ f.severity }}] {{ f.category }}:{{ f.description }}
33
36
  {% endfor %}
34
- {%- else -%}
37
+ {%- elif risk_screening.data_complete is not sameas false and risk_screening.risk_level != 'unknown' -%}
35
38
  - **风险标记:** 无显著风险标记
36
39
  {%- endif %}
37
40
  {%- else -%}
@@ -45,7 +48,7 @@
45
48
  {% set pp = dp.price_position or {} -%}
46
49
  {% set vol = dp.volume_analysis or {} -%}
47
50
  {% set chip = dp.chip_structure or {} -%}
48
- - **趋势:** {{ ts.ma_alignment or '-' }}{% if ts.is_bullish is not none %}({{ '看多' if ts.is_bullish else '看空' }}){% endif %}{% if ts.trend_score is not none %} 趋势分 {{ m.fmt_num(ts.trend_score) }}{% endif %}
51
+ - **趋势:** {{ ts.ma_alignment or '-' }}{% if ts.is_bullish is boolean %}({{ '看多' if ts.is_bullish else '看空' }}){% endif %}{% if ts.trend_score is not none %} 趋势分 {{ m.fmt_num(ts.trend_score) }}{% endif %}
49
52
  - **价格结构:** 现价 {{ m.fmt_num(pp.current_price) }} | 偏离 MA5 {{ m.change_pct(pp.bias_ma5) }} | 支撑 {{ m.fmt_num(pp.support_level) }} | 压力 {{ m.fmt_num(pp.resistance_level) }}
50
53
  - **量价:** 量比 {{ m.fmt_num(vol.volume_ratio) }}{% if vol.volume_status %}({{ vol.volume_status }}){% endif %}{% if vol.turnover_rate is not none %} | 换手率 {{ m.change_pct(vol.turnover_rate) }}{% endif %}
51
54
  {% if chip and (chip.profit_ratio is not none or chip.avg_cost is not none) -%}
@@ -6,6 +6,7 @@ Does NOT store history, does NOT push, does NOT schedule. Host agent decides whe
6
6
 
7
7
  import argparse
8
8
  import json
9
+ import math
9
10
  import sys
10
11
  from concurrent.futures import ThreadPoolExecutor
11
12
  from datetime import datetime, timezone
@@ -31,8 +32,14 @@ RULE_TYPES = {
31
32
  def _check_rule(rule: dict, quote: dict, tech: dict, anom: dict, risk: dict) -> dict | None:
32
33
  rtype = rule.get("type")
33
34
  value = rule.get("value")
34
- if rtype not in RULE_TYPES:
35
+ if not isinstance(rtype, str) or rtype not in RULE_TYPES:
35
36
  return {"rule": rtype, "triggered": False, "error": f"unknown rule type: {rtype}"}
37
+ if rtype in ("price_below", "price_above", "change_pct_above", "change_pct_below", "volume_ratio_above"):
38
+ try:
39
+ if isinstance(value, bool) or not math.isfinite(float(value)):
40
+ raise ValueError
41
+ except (ValueError, TypeError):
42
+ return {"rule": rtype, "triggered": False, "error": "Rule value must be a finite number"}
36
43
 
37
44
  price = quote.get("price")
38
45
  change_pct = quote.get("change_pct")
@@ -90,6 +97,8 @@ def _check_rule(rule: dict, quote: dict, tech: dict, anom: dict, risk: dict) ->
90
97
  "message": f"量比 {ratio} 超过 {value}",
91
98
  }
92
99
  elif rtype == "anomaly":
100
+ if not isinstance(value, str) or not value:
101
+ return {"rule": rtype, "triggered": False, "error": "Anomaly type must be a nonempty string"}
93
102
  # value is an anomaly type string, e.g. "macd_golden_cross"
94
103
  anomalies = (anom or {}).get("anomalies", [])
95
104
  for a in anomalies:
@@ -106,6 +115,8 @@ def _check_rule(rule: dict, quote: dict, tech: dict, anom: dict, risk: dict) ->
106
115
  return {"rule": rtype, "triggered": True, "severity": "high", "message": "风险筛查触发一票否决"}
107
116
  elif rtype == "risk_level_at_least":
108
117
  order = {"low": 0, "medium": 1, "high": 2}
118
+ if not isinstance(value, str) or value not in order:
119
+ return {"rule": rtype, "triggered": False, "error": "Risk threshold must be low/medium/high"}
109
120
  cur = order.get((risk or {}).get("risk_level"))
110
121
  want = order.get(value)
111
122
  if cur is not None and want is not None and cur >= want:
@@ -122,7 +133,7 @@ def _check_rule(rule: dict, quote: dict, tech: dict, anom: dict, risk: dict) ->
122
133
 
123
134
 
124
135
  def check_rules(symbol: str, rules: list[dict]) -> dict:
125
- if not rules:
136
+ if not isinstance(rules, list) or not rules or any(not isinstance(rule, dict) for rule in rules):
126
137
  return {"symbol": symbol, "error": "no rules provided"}
127
138
 
128
139
  needs_tech = any(r.get("type") == "volume_ratio_above" for r in rules)
@@ -141,6 +152,9 @@ def check_rules(symbol: str, rules: list[dict]) -> dict:
141
152
  with ThreadPoolExecutor(max_workers=len(jobs)) as pool:
142
153
  futures = {key: pool.submit(_run_json, script, args) for key, (script, args) in jobs.items()}
143
154
  fetched = {key: future.result() for key, future in futures.items()}
155
+ fetched = {
156
+ key: value if isinstance(value, dict) and "error" not in value else None for key, value in fetched.items()
157
+ }
144
158
  quote = fetched["quote"] or {}
145
159
  tech = fetched.get("tech")
146
160
  anom = fetched.get("anom")
@@ -153,13 +167,25 @@ def check_rules(symbol: str, rules: list[dict]) -> dict:
153
167
  warnings.append("technical analysis fetch failed — volume_ratio rules cannot fire")
154
168
  if needs_anom and not anom:
155
169
  warnings.append("anomaly fetch failed — anomaly rules cannot fire")
156
- if needs_risk and not risk:
170
+ if needs_risk and (not risk or risk.get("data_complete") is False or risk.get("risk_level") == "unknown"):
157
171
  warnings.append("risk screening fetch failed — risk rules cannot fire")
158
172
 
159
173
  hits = []
160
174
  evaluated = []
161
175
  for rule in rules:
162
- result = _check_rule(rule, quote, tech, anom, risk)
176
+ rtype = rule.get("type")
177
+ missing = (
178
+ (rtype in ("price_below", "price_above") and quote.get("price") is None)
179
+ or (rtype in ("change_pct_below", "change_pct_above") and quote.get("change_pct") is None)
180
+ or (rtype == "volume_ratio_above" and (tech or {}).get("volume", {}).get("volume_ratio") is None)
181
+ or (rtype == "anomaly" and not isinstance((anom or {}).get("anomalies"), list))
182
+ or (rtype == "risk_veto" and (risk or {}).get("veto_buy") is None)
183
+ or (rtype == "risk_level_at_least" and (risk or {}).get("risk_level") not in ("low", "medium", "high"))
184
+ )
185
+ if missing:
186
+ result = {"rule": rtype, "triggered": False, "error": "Required source data unavailable"}
187
+ else:
188
+ result = _check_rule(rule, quote, tech, anom, risk)
163
189
  if result is None:
164
190
  evaluated.append({"rule": rule.get("type"), "triggered": False})
165
191
  else:
@@ -214,6 +240,8 @@ def main():
214
240
  result = check_rules(args.symbol, rules)
215
241
  json.dump(result, sys.stdout, ensure_ascii=False, indent=2, default=str)
216
242
  print()
243
+ if "error" in result:
244
+ sys.exit(1)
217
245
 
218
246
 
219
247
  if __name__ == "__main__":
package/tools/backtest.py CHANGED
@@ -16,6 +16,7 @@ import yaml
16
16
  sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
17
17
 
18
18
  from _subproc import json_safe, run_tool, utf8_stdio
19
+ from stock_data import detect_market
19
20
  from technical import bollinger_series, ema_series, ma_series, macd_series, rsi_series
20
21
 
21
22
  # --------------- Strategy Loading ---------------
@@ -67,7 +68,7 @@ def fetch_kline(symbol: str, start: str | None, end: str | None) -> pd.DataFrame
67
68
  if start and end:
68
69
  d0 = datetime.strptime(start, "%Y-%m-%d")
69
70
  d1 = datetime.strptime(end, "%Y-%m-%d")
70
- count = max((d1 - d0).days + 60, 300)
71
+ count = max((datetime.now() - d0).days + 60, (d1 - d0).days + 60, 300)
71
72
 
72
73
  # run_tool gives venv-python + a bounded timeout + None on any failure
73
74
  # (non-zero exit, empty/invalid stdout); callers turn that into clean JSON.
@@ -251,7 +252,11 @@ def evaluate_conditions(df: pd.DataFrame, conditions: list, logic: str, idx: int
251
252
 
252
253
 
253
254
  def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> dict:
254
- market = "A" if re.match(r"^\d{6}$", symbol) else "OTHER"
255
+ if not math.isfinite(capital) or capital <= 0:
256
+ raise ValueError("capital must be finite and positive")
257
+ if not np.isfinite(df["close"]).all() or (df["close"] <= 0).any():
258
+ raise ValueError("backtest prices must be finite and positive")
259
+ market = detect_market(symbol)
255
260
  lot_size = 100 if market == "A" else 1
256
261
  sim_cfg = strategy.get("simulation", {})
257
262
  slippage = float(sim_cfg.get("slippage", 0.001))
@@ -268,6 +273,8 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
268
273
  exit_ = strategy.get("exit", {})
269
274
  position_cfg = strategy.get("position", {})
270
275
  size_frac = float(position_cfg.get("size", 1.0))
276
+ if not 0 <= size_frac <= 1:
277
+ raise ValueError("position.size must be a fraction in [0, 1]")
271
278
  stop_loss = float(exit_.get("stop_loss", -0.10))
272
279
  take_profit = float(exit_.get("take_profit", 0.50))
273
280
  ts_val = exit_.get("trailing_stop")
@@ -297,7 +304,7 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
297
304
  if hit:
298
305
  buy_price = price * (1 + slippage)
299
306
  buy_amount = cash * size_frac
300
- shares = int(buy_amount / buy_price / lot_size) * lot_size
307
+ shares = int(buy_amount / (buy_price * (1 + commission)) / lot_size) * lot_size
301
308
  if shares <= 0:
302
309
  continue
303
310
  cost = shares * buy_price * (1 + commission)
@@ -417,7 +424,7 @@ def compute_metrics(trades: list, equity_curve: list, initial: float, final: flo
417
424
  years = 1
418
425
  annual_return = (1 + total_return) ** (1 / max(years, 0.01)) - 1 if total_return > -1 else total_return
419
426
 
420
- equities = pd.Series([e["equity"] for e in equity_curve], dtype=float)
427
+ equities = pd.Series([initial, *[e["equity"] for e in equity_curve]], dtype=float)
421
428
  peak = equities.cummax()
422
429
  dd = (equities - peak) / peak
423
430
  max_drawdown = float(dd.min()) if len(dd) > 0 else 0
@@ -653,6 +660,13 @@ def run_backtest(strategy_path: str, symbol: str, start: str | None, end: str |
653
660
  if len(df) < 30:
654
661
  return {"error": f"Insufficient data: {len(df)} rows (need at least 30)"}
655
662
 
663
+ requested_period = f"{start} to {end}"
664
+ actual_start = df["date"].iloc[0].strftime("%Y-%m-%d")
665
+ actual_end = df["date"].iloc[-1].strftime("%Y-%m-%d")
666
+ warnings = []
667
+ if actual_start != start or actual_end != end:
668
+ warnings.append(f"Actual data covers {actual_start} to {actual_end}; requested {requested_period}")
669
+ start, end = actual_start, actual_end
656
670
  sim = simulate(df, strategy, capital, symbol)
657
671
  final = sim["final_equity"]
658
672
  metrics = compute_metrics(sim["trades"], sim["equity_curve"], capital, final, start, end)
@@ -661,6 +675,8 @@ def run_backtest(strategy_path: str, symbol: str, start: str | None, end: str |
661
675
  "strategy": strategy.get("name", "unnamed"),
662
676
  "symbol": symbol,
663
677
  "period": f"{start} to {end}",
678
+ "requested_period": requested_period,
679
+ "warnings": warnings,
664
680
  "initial_capital": capital,
665
681
  "final_capital": final,
666
682
  "costs": sim["costs"],
@@ -837,6 +853,8 @@ def main():
837
853
  sys.exit(1)
838
854
 
839
855
  print(json.dumps(json_safe(result), ensure_ascii=False, default=str))
856
+ if "error" in result:
857
+ sys.exit(1)
840
858
 
841
859
 
842
860
  if __name__ == "__main__":
@@ -18,6 +18,9 @@ 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
+ "get_announcements": {"markets": ["A"], "reason": "cninfo A-share disclosures only"},
22
+ "get_us_holdings": {"markets": ["US"], "reason": "SEC manager holdings or verified issuer-CUSIP reverse lookup"},
23
+ "get_insider_trades": {"markets": ["US"], "reason": "SEC issuer Form 4 only"},
21
24
  "get_valuation_history": {"markets": ["A", "HK"], "reason": "A/HK historical valuation providers only"},
22
25
  "get_index_valuation": {"markets": ["A"], "reason": "Chinese index valuation only"},
23
26
  "get_earnings_calendar": {
package/tools/gather.py CHANGED
@@ -10,6 +10,7 @@ from pathlib import Path
10
10
  sys.path.insert(0, str(Path(__file__).resolve().parent))
11
11
  from _subproc import json_safe, run_tool, utf8_stdio # noqa: E402
12
12
  from stock_data import detect_market # noqa: E402
13
+ from technical import analyze_records # noqa: E402
13
14
 
14
15
 
15
16
  # gather fans out the heavier CLIs, so the default timeout stays 60s; raw stdout
@@ -32,36 +33,41 @@ def _parse_json(raw: str | None):
32
33
  return raw
33
34
 
34
35
 
35
- def _run_all(tasks: dict) -> dict:
36
+ def _run_all(tasks: dict, symbol: str) -> dict:
36
37
  with ThreadPoolExecutor(max_workers=len(tasks)) as pool:
37
38
  futures = {pool.submit(_run, script, args): key for key, (script, args) in tasks.items()}
38
- return {key: _parse_json(f.result()) for f, key in futures.items()}
39
+ result = {key: _parse_json(f.result()) for f, key in futures.items()}
40
+ result["technical"] = analyze_records(symbol, result["kline"]) if result["kline"] is not None else None
41
+ return result
39
42
 
40
43
 
41
44
  def gather_analysis(symbol: str) -> dict:
45
+ market = detect_market(symbol)
42
46
  tasks = {
43
47
  "quote": ("stock_data.py", ["quote", symbol]),
44
48
  "kline": ("stock_data.py", ["kline", symbol, "--period", "daily", "--count", "120"]),
45
- "technical": ("technical.py", ["analyze", symbol, "--period", "daily", "--count", "120"]),
46
49
  "financials": ("stock_data.py", ["financials", symbol]),
47
50
  "capital_flow": ("stock_data.py", ["capital_flow", symbol]),
48
51
  "chip_distribution": ("stock_data.py", ["chip_distribution", symbol]),
49
52
  "news": ("search_intel.py", ["search", _news_query(symbol)]),
50
53
  "social_sentiment": ("search_intel.py", ["sentiment", symbol]),
51
54
  "risk": ("risk_screening.py", ["screen", symbol]),
52
- "regime": ("market_regime.py", ["detect"]),
53
55
  }
54
- return _run_all(tasks)
56
+ if market in ("A", "HK", "US"):
57
+ tasks["regime"] = ("market_regime.py", ["detect", market])
58
+ result = _run_all(tasks, symbol)
59
+ if market not in ("A", "HK", "US"):
60
+ result["regime"] = {"error": f"Market regime unavailable for {market}"}
61
+ return result
55
62
 
56
63
 
57
64
  def gather_technical(symbol: str, kline_count: int = 120, with_quote: bool = False) -> dict:
58
65
  tasks = {
59
66
  "kline": ("stock_data.py", ["kline", symbol, "--period", "daily", "--count", str(kline_count)]),
60
- "technical": ("technical.py", ["analyze", symbol, "--period", "daily", "--count", str(kline_count)]),
61
67
  }
62
68
  if with_quote:
63
69
  tasks["quote"] = ("stock_data.py", ["quote", symbol])
64
- return _run_all(tasks)
70
+ return _run_all(tasks, symbol)
65
71
 
66
72
 
67
73
  def gather_screen(market: str = "A", top: int = 20, config: str | None = None) -> dict:
@@ -73,16 +79,20 @@ def gather_screen(market: str = "A", top: int = 20, config: str | None = None) -
73
79
 
74
80
 
75
81
  def gather_fundamental(symbol: str) -> dict:
82
+ market = detect_market(symbol)
76
83
  tasks = {
77
84
  "quote": ("stock_data.py", ["quote", symbol]),
78
85
  "kline": ("stock_data.py", ["kline", symbol, "--period", "daily", "--count", "60"]),
79
- "technical": ("technical.py", ["analyze", symbol, "--period", "daily", "--count", "60"]),
80
86
  "financials": ("stock_data.py", ["financials", symbol]),
81
87
  "news": ("search_intel.py", ["search", _news_query(symbol)]),
82
88
  "stock_info": ("stock_data.py", ["stock_info", symbol]),
83
- "sector_rankings": ("stock_data.py", ["sector_rankings", "--top", "5", "--direction", "both"]),
84
89
  }
85
- return _run_all(tasks)
90
+ if market == "A":
91
+ tasks["sector_rankings"] = ("stock_data.py", ["sector_rankings", "--top", "5", "--direction", "both"])
92
+ result = _run_all(tasks, symbol)
93
+ if market != "A":
94
+ result["sector_rankings"] = {"error": f"Sector rankings unavailable for {market}"}
95
+ return result
86
96
 
87
97
 
88
98
  def main():
@@ -0,0 +1,100 @@
1
+ """Optional Lixinger HK non-financial datasets (same env token as index data)."""
2
+
3
+ import os
4
+ from datetime import date, timedelta
5
+
6
+ import requests
7
+ from _cache import DAY, cached
8
+
9
+
10
+ def hk_data(symbol, dataset, metrics):
11
+ code = symbol.split(".")[0].zfill(5)
12
+ end = date.today()
13
+ try:
14
+ start = end.replace(year=end.year - 10)
15
+ except ValueError:
16
+ start = end.replace(year=end.year - 10, day=28) + timedelta(days=1)
17
+
18
+ def fetch():
19
+ response = requests.post(
20
+ f"https://open.lixinger.com/api/hk/company/{dataset}/non_financial",
21
+ json={
22
+ "token": os.environ["LIXINGER_TOKEN"],
23
+ "stockCodes": [code],
24
+ "startDate": start.isoformat(),
25
+ "endDate": end.isoformat(),
26
+ "metricsList": metrics,
27
+ },
28
+ timeout=20,
29
+ )
30
+ response.raise_for_status()
31
+ payload = response.json()
32
+ if payload.get("code") != 1 or not isinstance(payload.get("data"), list) or not payload["data"]:
33
+ raise ValueError("Lixinger HK non-financial data unavailable; check token permissions/company type")
34
+ return payload["data"]
35
+
36
+ return cached(f"lixinger-hk:{dataset}:{code}:{end}:{','.join(metrics)}", DAY, fetch)
37
+
38
+
39
+ def financials_hk(symbol, periods, annual):
40
+ fields = {
41
+ "roe": "m.roe",
42
+ "gross_margin": "ps.gp_m",
43
+ "net_profit_margin": "ps.np_s_r",
44
+ "total_revenue": "ps.toi",
45
+ "net_income": "ps.np",
46
+ "operating_cash_flow": "cfs.ncffoa",
47
+ }
48
+ data = hk_data(symbol, "fs", [f"q.{field}.t" for field in fields.values()])
49
+ history = []
50
+ for row in sorted(data, key=lambda r: r.get("date", ""), reverse=True):
51
+ if annual and row.get("reportType") != "annual_report":
52
+ continue
53
+ if not row.get("date") or row.get("stockCode", symbol.split(".")[0].zfill(5)) != symbol.split(".")[0].zfill(5):
54
+ continue
55
+ entry = {
56
+ "report_date": row["date"][:10],
57
+ "disclosure_date": (row.get("reportDate") or "")[:10] or None,
58
+ "report_type": row.get("reportType"),
59
+ "currency": row.get("currency"),
60
+ "source": "lixinger",
61
+ }
62
+ for key, field in fields.items():
63
+ table, metric = field.split(".")
64
+ value = row.get("q", {}).get(table, {}).get(metric, {}).get("t")
65
+ entry[key] = (
66
+ value * 100 if value is not None and key in ("roe", "gross_margin", "net_profit_margin") else value
67
+ )
68
+ if any(entry.get(key) is not None for key in fields):
69
+ history.append(entry)
70
+ if not history:
71
+ raise ValueError("Lixinger returned no matching financial periods")
72
+ history = history[:periods]
73
+ latest = history[0]
74
+ result = {
75
+ **dict.fromkeys(("name", "market_cap", "pe", "forward_pe", "pb", "debt_to_equity", "dividend_yield")),
76
+ "snapshot_available": False,
77
+ "symbol": symbol,
78
+ "source": "lixinger",
79
+ "report_date": latest["report_date"],
80
+ "currency": latest["currency"],
81
+ "total_revenue": latest["total_revenue"],
82
+ "net_income": latest["net_income"],
83
+ "roe": latest["roe"] / 100 if latest["roe"] is not None else None,
84
+ "profit_margin": latest["net_profit_margin"] / 100 if latest["net_profit_margin"] is not None else None,
85
+ "periods": history,
86
+ "requested_periods": periods,
87
+ "available_periods": len(history),
88
+ "history_units": {
89
+ "roe": "percent",
90
+ "gross_margin": "percent",
91
+ "net_profit_margin": "percent",
92
+ "total_revenue": "reported currency",
93
+ "net_income": "reported currency",
94
+ "operating_cash_flow": "reported currency",
95
+ },
96
+ "accounting_basis": "provider-reported HK financial statements; not normalized to A-share accounting",
97
+ }
98
+ if len(history) < periods:
99
+ result["warnings"] = ["Requested financial history incomplete; only actual available periods returned"]
100
+ return result
@@ -54,7 +54,9 @@ def _get_index_kline(market: str) -> list:
54
54
  def _compute_indicators(klines: list) -> dict:
55
55
  import numpy as np
56
56
 
57
- closes = np.array([float(k["close"]) for k in klines if k.get("close") is not None])
57
+ df = pd.DataFrame(klines).reindex(columns=["close", "high", "low"]).apply(pd.to_numeric, errors="coerce")
58
+ df = df[np.isfinite(df).all(axis=1) & (df > 0).all(axis=1)]
59
+ closes = df["close"].to_numpy()
58
60
  if len(closes) < 20:
59
61
  return {}
60
62
 
@@ -65,8 +67,8 @@ def _compute_indicators(klines: list) -> dict:
65
67
  ma60 = float(ma_series(close_s, 60).iloc[-1]) if len(closes) >= 60 else float(np.mean(closes))
66
68
  close = float(closes[-1])
67
69
 
68
- highs = np.array([float(k["high"]) for k in klines if k.get("high") is not None])
69
- lows = np.array([float(k["low"]) for k in klines if k.get("low") is not None])
70
+ highs = df["high"].to_numpy()
71
+ lows = df["low"].to_numpy()
70
72
 
71
73
  tr_values = []
72
74
  for i in range(-min(14, len(closes) - 1), 0):
@@ -226,6 +228,8 @@ def main():
226
228
  if args.command == "detect":
227
229
  result = detect_regime(args.market)
228
230
  print(json.dumps(result, ensure_ascii=False, default=str))
231
+ if "error" in result:
232
+ sys.exit(1)
229
233
 
230
234
 
231
235
  if __name__ == "__main__":
@@ -19,7 +19,7 @@ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
19
19
 
20
20
  from _subproc import run_tool as _run_tool
21
21
  from _subproc import utf8_stdio
22
- from signal_tracker import _load_records
22
+ from signal_tracker import _append_record, _load_records, _store_lock
23
23
 
24
24
 
25
25
  def calc_temperature(stats: dict, indices: list) -> dict:
@@ -145,10 +145,9 @@ def save_review(report: dict, store: Path | None = None) -> dict:
145
145
  """Append one review report object to the JSONL archive (side effect of render --save)."""
146
146
  store = store or _default_store()
147
147
  try:
148
- store.parent.mkdir(parents=True, exist_ok=True)
149
- with store.open("a", encoding="utf-8") as f:
150
- f.write(json.dumps(report, ensure_ascii=False, default=str) + "\n")
151
- except OSError as e:
148
+ with _store_lock(store):
149
+ _append_record(store, report)
150
+ except (OSError, ValueError) as e:
152
151
  return {"error": str(e)}
153
152
  return {"saved": True, "date": report.get("review_date") or report.get("date")}
154
153
 
@@ -156,7 +155,9 @@ def save_review(report: dict, store: Path | None = None) -> dict:
156
155
  def cmd_history(args) -> dict:
157
156
  """Recent archived reviews, newest first. Empty store -> clean empty list."""
158
157
  try:
159
- records = _load_records(Path(args.store) if args.store else _default_store())
158
+ store = Path(args.store) if args.store else _default_store()
159
+ with _store_lock(store):
160
+ records = _load_records(store)
160
161
  except (OSError, UnicodeDecodeError) as e:
161
162
  # store is a directory / not UTF-8 — same clean-error contract as save_review
162
163
  return {"error": str(e)}
@@ -6,6 +6,7 @@ Not a portfolio system. Stateless: user passes cost/quantity/stops each call.
6
6
 
7
7
  import argparse
8
8
  import json
9
+ import math
9
10
  import sys
10
11
  from datetime import datetime, timezone
11
12
  from pathlib import Path
@@ -48,13 +49,28 @@ def analyze_position(
48
49
  stop_loss: float | None = None,
49
50
  take_profit: float | None = None,
50
51
  ) -> dict:
52
+ for field, value in (
53
+ ("cost", cost),
54
+ ("quantity", quantity),
55
+ ("stop_loss", stop_loss),
56
+ ("take_profit", take_profit),
57
+ ):
58
+ if value is not None and (not isinstance(value, (int, float)) or not math.isfinite(value) or value <= 0):
59
+ return {"symbol": symbol, "error": f"{field} must be finite and positive"}
51
60
  quote = _run_json("stock_data.py", ["quote", symbol]) or {}
52
61
  tech = _run_json("technical.py", ["analyze", symbol, "--period", "daily", "--count", "60"]) or {}
53
62
 
54
- price = quote.get("price")
63
+ price = quote.get("price") if isinstance(quote, dict) and "error" not in quote else None
55
64
  if price is None:
56
65
  return {"symbol": symbol, "error": "cannot fetch current price"}
57
- price = float(price)
66
+ try:
67
+ price = float(price)
68
+ except (TypeError, ValueError):
69
+ return {"symbol": symbol, "error": "current price must be finite and positive"}
70
+ if not math.isfinite(price) or price <= 0:
71
+ return {"symbol": symbol, "error": "current price must be finite and positive"}
72
+ if not isinstance(tech, dict):
73
+ tech = {}
58
74
 
59
75
  pnl_per_share = price - cost
60
76
  pnl_pct = (pnl_per_share / cost * 100) if cost else 0.0
@@ -133,6 +149,8 @@ def main():
133
149
  )
134
150
  json.dump(result, sys.stdout, ensure_ascii=False, indent=2, default=str)
135
151
  print()
152
+ if "error" in result:
153
+ sys.exit(1)
136
154
 
137
155
 
138
156
  if __name__ == "__main__":
@@ -112,6 +112,8 @@ def main():
112
112
  result["archived"] = save_review(report)
113
113
  json.dump(result, sys.stdout, ensure_ascii=False, indent=2, default=str)
114
114
  print()
115
+ if "error" in result:
116
+ sys.exit(1)
115
117
 
116
118
 
117
119
  if __name__ == "__main__":