@weaxs/openclaw-stock-analysis 0.2.10 → 0.2.12
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 +11 -51
- package/dist/index.js +3693 -1971
- package/index.ts +74 -1636
- package/openclaw.plugin.json +3 -0
- package/package.json +3 -3
- package/scripts/cli-json.mjs +23 -0
- package/scripts/exec-process.mjs +89 -0
- package/scripts/setup-python.mjs +37 -4
- package/scripts/tool-calls.mjs +489 -0
- package/skills/event-driven/SKILL.md +2 -2
- package/skills/growth-quality/SKILL.md +1 -1
- package/skills/stock-analysis/SKILL.md +12 -7
- package/skills/stock-analysis/references/stateless-analysis.md +9 -1
- package/tools/_cache.py +13 -0
- package/tools/_subproc.py +46 -0
- package/tools/alert_rules.py +65 -16
- package/tools/backtest.py +3 -3
- package/tools/capabilities.py +32 -9
- package/tools/cli_budgets.json +2 -0
- package/tools/diagnostics.py +3 -4
- package/tools/gather.py +2 -6
- package/tools/import_parser.py +3 -4
- package/tools/lixinger.py +119 -27
- package/tools/market_regime.py +5 -2
- package/tools/market_review.py +5 -6
- package/tools/name_resolver.py +82 -21
- package/tools/report_renderer.py +3 -3
- package/tools/requirements.txt +6 -5
- package/tools/risk_screening.py +3 -2
- package/tools/screener.py +6 -4
- package/tools/search_intel.py +81 -12
- package/tools/sec_edgar.py +266 -0
- package/tools/sec_holders.py +6 -0
- package/tools/signal_tracker.py +11 -10
- package/tools/stateless_analysis.py +115 -0
- package/tools/stock_data.py +707 -222
- package/tools/tiaa_disclosures.py +267 -0
- package/tools/tool_schemas.json +2704 -0
- package/tools/trading_calendar.py +65 -30
- package/tools/value_data.py +366 -134
- package/tools/watchlist.py +43 -5
package/tools/_subproc.py
CHANGED
|
@@ -11,6 +11,7 @@ import json
|
|
|
11
11
|
import math
|
|
12
12
|
import os
|
|
13
13
|
import re
|
|
14
|
+
import signal
|
|
14
15
|
import socket
|
|
15
16
|
import subprocess
|
|
16
17
|
import sys
|
|
@@ -64,6 +65,45 @@ def find_python() -> str:
|
|
|
64
65
|
return venv if os.path.exists(venv) else sys.executable
|
|
65
66
|
|
|
66
67
|
|
|
68
|
+
# A CLI that calls sibling CLIs must die with them: each child leads its own process group.
|
|
69
|
+
TREE_POPEN = (
|
|
70
|
+
{"creationflags": subprocess.CREATE_NEW_PROCESS_GROUP} if sys.platform == "win32" else {"start_new_session": True}
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def terminate_tree(proc) -> None:
|
|
75
|
+
"""Kill a TREE_POPEN child and every process it started."""
|
|
76
|
+
if sys.platform == "win32":
|
|
77
|
+
try:
|
|
78
|
+
subprocess.run(
|
|
79
|
+
["taskkill", "/F", "/T", "/PID", str(proc.pid)],
|
|
80
|
+
stdin=subprocess.DEVNULL,
|
|
81
|
+
stdout=subprocess.DEVNULL,
|
|
82
|
+
stderr=subprocess.DEVNULL,
|
|
83
|
+
timeout=10,
|
|
84
|
+
check=False,
|
|
85
|
+
)
|
|
86
|
+
except (OSError, subprocess.TimeoutExpired):
|
|
87
|
+
proc.kill()
|
|
88
|
+
else:
|
|
89
|
+
with contextlib.suppress(ProcessLookupError, PermissionError):
|
|
90
|
+
os.killpg(proc.pid, signal.SIGKILL)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def run_tree(cmd: list, *, timeout: float) -> subprocess.CompletedProcess:
|
|
94
|
+
"""subprocess.run(capture_output, text) whose timeout also kills grandchildren."""
|
|
95
|
+
with subprocess.Popen(
|
|
96
|
+
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, encoding="utf-8", **TREE_POPEN
|
|
97
|
+
) as proc:
|
|
98
|
+
try:
|
|
99
|
+
stdout, stderr = proc.communicate(timeout=timeout)
|
|
100
|
+
except BaseException: # timeout or Ctrl+C: the new session no longer receives terminal signals
|
|
101
|
+
terminate_tree(proc)
|
|
102
|
+
proc.communicate()
|
|
103
|
+
raise
|
|
104
|
+
return subprocess.CompletedProcess(cmd, proc.returncode, stdout, stderr)
|
|
105
|
+
|
|
106
|
+
|
|
67
107
|
def run_tool(
|
|
68
108
|
script: str, args: list, timeout: int | None = None, parse_json: bool = True, preserve_errors: bool = False
|
|
69
109
|
):
|
|
@@ -79,6 +119,7 @@ def run_tool(
|
|
|
79
119
|
return payload if parse_json else json.dumps(payload, ensure_ascii=False)
|
|
80
120
|
|
|
81
121
|
try:
|
|
122
|
+
# Not run_tree: a nested CLI must stay in the caller's process group so the host's tree kill reaches it.
|
|
82
123
|
r = subprocess.run(
|
|
83
124
|
[find_python(), os.path.join(TOOLS_DIR, script), *args],
|
|
84
125
|
capture_output=True,
|
|
@@ -172,6 +213,11 @@ def scrub_secrets(msg: str) -> str:
|
|
|
172
213
|
return _SECRET_PARAM_RE.sub(r"\1***", msg)
|
|
173
214
|
|
|
174
215
|
|
|
216
|
+
def error_message(exc: BaseException, limit: int = 300) -> str:
|
|
217
|
+
"""Exception text for stdout JSON: secrets scrubbed, provider bodies bounded."""
|
|
218
|
+
return scrub_secrets(str(exc))[:limit]
|
|
219
|
+
|
|
220
|
+
|
|
175
221
|
def json_safe(obj):
|
|
176
222
|
"""Replace non-finite floats (inf/-inf/nan, e.g. an all-flat P&L profit_loss_ratio)
|
|
177
223
|
with None so json.dumps never emits the non-standard Infinity/NaN tokens that
|
package/tools/alert_rules.py
CHANGED
|
@@ -13,8 +13,8 @@ from datetime import datetime, timezone
|
|
|
13
13
|
from pathlib import Path
|
|
14
14
|
|
|
15
15
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
16
|
+
from _subproc import error_message, utf8_stdio
|
|
16
17
|
from _subproc import run_tool as _run_json # noqa: E402
|
|
17
|
-
from _subproc import utf8_stdio
|
|
18
18
|
from stock_data import detect_market # noqa: E402
|
|
19
19
|
|
|
20
20
|
RULE_TYPES = {
|
|
@@ -140,28 +140,77 @@ def check_rules(symbol: str, rules: list[dict]) -> dict:
|
|
|
140
140
|
needs_anom = any(r.get("type") == "anomaly" for r in rules)
|
|
141
141
|
needs_risk = any(r.get("type") in ("risk_veto", "risk_level_at_least") for r in rules)
|
|
142
142
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
jobs
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
143
|
+
needs_quote = (
|
|
144
|
+
needs_anom
|
|
145
|
+
or needs_risk
|
|
146
|
+
or any(r.get("type") in ("price_below", "price_above", "change_pct_above", "change_pct_below") for r in rules)
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
# Share one market snapshot while retaining 60-bar alerts and 120-bar risk analysis.
|
|
150
|
+
with ThreadPoolExecutor(max_workers=4) as pool:
|
|
151
|
+
jobs = {}
|
|
152
|
+
if needs_quote:
|
|
153
|
+
quote_job = pool.submit(_run_json, "stock_data.py", ["quote", symbol])
|
|
154
|
+
jobs["quote"] = quote_job
|
|
155
|
+
if needs_tech or needs_anom or needs_risk:
|
|
156
|
+
from technical import analyze_records
|
|
157
|
+
|
|
158
|
+
kline_job = pool.submit(
|
|
159
|
+
_run_json,
|
|
160
|
+
"stock_data.py",
|
|
161
|
+
["kline", symbol, "--period", "daily", "--count", "120" if needs_risk else "60"],
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
def analyze(count):
|
|
165
|
+
records = kline_job.result()
|
|
166
|
+
try:
|
|
167
|
+
return analyze_records(symbol, records[-count:] if isinstance(records, list) else records)
|
|
168
|
+
except Exception as exc:
|
|
169
|
+
return {"error": f"Technical analysis failed ({type(exc).__name__})"}
|
|
170
|
+
|
|
171
|
+
if needs_risk:
|
|
172
|
+
from risk_screening import screen_risk
|
|
173
|
+
|
|
174
|
+
technical_job = pool.submit(analyze, 120)
|
|
175
|
+
|
|
176
|
+
def collect_risk():
|
|
177
|
+
try:
|
|
178
|
+
return screen_risk(
|
|
179
|
+
symbol, inputs={"quote": quote_job, "technical": technical_job}, run=_run_json
|
|
180
|
+
)
|
|
181
|
+
except Exception as exc:
|
|
182
|
+
return {"error": f"Risk analysis failed ({type(exc).__name__})"}
|
|
183
|
+
|
|
184
|
+
jobs["risk"] = pool.submit(collect_risk)
|
|
185
|
+
if needs_tech:
|
|
186
|
+
jobs["tech"] = pool.submit(analyze, 60)
|
|
187
|
+
if needs_anom:
|
|
188
|
+
from anomaly_detect import detect_anomalies
|
|
189
|
+
|
|
190
|
+
def anomalies():
|
|
191
|
+
records = kline_job.result()
|
|
192
|
+
try:
|
|
193
|
+
return detect_anomalies(
|
|
194
|
+
symbol,
|
|
195
|
+
records=records[-60:] if isinstance(records, list) else records,
|
|
196
|
+
quote=quote_job.result(),
|
|
197
|
+
run=_run_json,
|
|
198
|
+
)
|
|
199
|
+
except Exception as exc:
|
|
200
|
+
return {"error": f"Anomaly analysis failed ({type(exc).__name__})"}
|
|
201
|
+
|
|
202
|
+
jobs["anom"] = pool.submit(anomalies)
|
|
203
|
+
fetched = {key: future.result() for key, future in jobs.items()}
|
|
155
204
|
fetched = {
|
|
156
205
|
key: value if isinstance(value, dict) and "error" not in value else None for key, value in fetched.items()
|
|
157
206
|
}
|
|
158
|
-
quote = fetched
|
|
207
|
+
quote = fetched.get("quote") or {}
|
|
159
208
|
tech = fetched.get("tech")
|
|
160
209
|
anom = fetched.get("anom")
|
|
161
210
|
risk = fetched.get("risk")
|
|
162
211
|
|
|
163
212
|
warnings = []
|
|
164
|
-
if not quote or quote.get("price") is None:
|
|
213
|
+
if needs_quote and (not quote or quote.get("price") is None):
|
|
165
214
|
warnings.append("quote fetch failed or missing price — price/change rules cannot fire")
|
|
166
215
|
if needs_tech and not tech:
|
|
167
216
|
warnings.append("technical analysis fetch failed — volume_ratio rules cannot fire")
|
|
@@ -234,7 +283,7 @@ def main():
|
|
|
234
283
|
try:
|
|
235
284
|
rules = json.loads(raw)
|
|
236
285
|
except json.JSONDecodeError as e:
|
|
237
|
-
print(json.dumps({"error": f"invalid rules JSON: {e}"}, ensure_ascii=False))
|
|
286
|
+
print(json.dumps({"error": f"invalid rules JSON: {error_message(e)}"}, ensure_ascii=False))
|
|
238
287
|
sys.exit(1)
|
|
239
288
|
|
|
240
289
|
result = check_rules(args.symbol, rules)
|
package/tools/backtest.py
CHANGED
|
@@ -15,7 +15,7 @@ import yaml
|
|
|
15
15
|
|
|
16
16
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
17
17
|
|
|
18
|
-
from _subproc import json_safe, run_tool, utf8_stdio
|
|
18
|
+
from _subproc import error_message, json_safe, run_tool, utf8_stdio
|
|
19
19
|
from stock_data import detect_market
|
|
20
20
|
from technical import (
|
|
21
21
|
bollinger_series,
|
|
@@ -894,7 +894,7 @@ def _benchmark_comparison(
|
|
|
894
894
|
df = fetch_kline(symbol, start, end)
|
|
895
895
|
_validate_adjustment_basis(df)
|
|
896
896
|
except Exception as e:
|
|
897
|
-
return {"symbol": symbol, "error":
|
|
897
|
+
return {"symbol": symbol, "error": error_message(e)}
|
|
898
898
|
if len(df) < 2:
|
|
899
899
|
return {"symbol": symbol, "error": f"Insufficient benchmark data: {len(df)} rows"}
|
|
900
900
|
dates = pd.to_datetime(df["date"], errors="coerce")
|
|
@@ -1128,7 +1128,7 @@ def main():
|
|
|
1128
1128
|
result = {"error": f"Unknown command: {args.command}"}
|
|
1129
1129
|
except Exception as e:
|
|
1130
1130
|
# A traceback would leave stdout empty; hosts need valid JSON even on failure.
|
|
1131
|
-
print(json.dumps({"error":
|
|
1131
|
+
print(json.dumps({"error": error_message(e)}, ensure_ascii=False))
|
|
1132
1132
|
sys.exit(1)
|
|
1133
1133
|
|
|
1134
1134
|
print(json.dumps(json_safe(result), ensure_ascii=False, default=str))
|
package/tools/capabilities.py
CHANGED
|
@@ -30,21 +30,25 @@ TOOL_MATRIX = {
|
|
|
30
30
|
"analyze_portfolio_risk": {"markets": ALL_MARKETS, "reason": None},
|
|
31
31
|
"plan_portfolio_rebalance": {"markets": ALL_MARKETS, "reason": None},
|
|
32
32
|
"calculate_position_size": {"markets": ALL_MARKETS, "reason": None},
|
|
33
|
+
"calculate_dcf_valuation": {"markets": ALL_MARKETS, "reason": None},
|
|
33
34
|
"analyze_event_study": {"markets": ALL_MARKETS, "reason": None},
|
|
34
35
|
"validate_backtest": {"markets": ALL_MARKETS, "reason": None},
|
|
35
36
|
"compare_analysis_snapshots": {"markets": ALL_MARKETS, "reason": None},
|
|
36
|
-
"get_announcements": {
|
|
37
|
+
"get_announcements": {
|
|
38
|
+
"markets": ["A", "HK", "US"],
|
|
39
|
+
"reason": "cninfo A-share / HKEXnews HK / SEC EDGAR US disclosures only",
|
|
40
|
+
},
|
|
37
41
|
"get_us_holdings": {"markets": ["US"], "reason": "SEC manager holdings or verified issuer-CUSIP reverse lookup"},
|
|
38
42
|
"get_insider_trades": {"markets": ["US"], "reason": "SEC issuer Form 4 only"},
|
|
39
|
-
"get_valuation_history": {"markets": ["A", "HK"], "reason": "A/HK
|
|
43
|
+
"get_valuation_history": {"markets": ["A", "HK", "US"], "reason": "A/HK providers; US derived from SEC filings"},
|
|
40
44
|
"get_index_valuation": {"markets": ["A"], "reason": "Chinese index valuation only"},
|
|
41
45
|
"get_earnings_calendar": {
|
|
42
|
-
"markets": ["A", "HK"],
|
|
43
|
-
"reason": "A-share disclosed calendar / HK single-symbol provider schedule",
|
|
46
|
+
"markets": ["A", "HK", "US", "JP"],
|
|
47
|
+
"reason": "A-share disclosed calendar / HK/US/JP single-symbol provider schedule",
|
|
44
48
|
},
|
|
45
49
|
"get_shareholder_structure": {"markets": ["A", "HK"], "reason": "A-share shareholder reports / HK southbound only"},
|
|
46
50
|
"get_etf_details": {"markets": ["A"], "reason": "A-share ETF portfolio/NAV/size only"},
|
|
47
|
-
"get_dividends": {"markets":
|
|
51
|
+
"get_dividends": {"markets": ALL_MARKETS, "reason": None}, # ranking mode stays A/HK only
|
|
48
52
|
"get_kline": {"markets": ALL_MARKETS, "reason": None},
|
|
49
53
|
"get_quote": {"markets": ALL_MARKETS, "reason": None},
|
|
50
54
|
"get_news": {"markets": ALL_MARKETS, "reason": None},
|
|
@@ -57,16 +61,16 @@ TOOL_MATRIX = {
|
|
|
57
61
|
"detect_anomaly": {"markets": ALL_MARKETS, "reason": None},
|
|
58
62
|
"get_stock_info": {"markets": ALL_MARKETS, "reason": None},
|
|
59
63
|
"resolve_stock_sectors": {"markets": ["A", "HK"], "reason": "A-share and HK only"},
|
|
60
|
-
"detect_market_regime": {"markets":
|
|
64
|
+
"detect_market_regime": {"markets": ALL_MARKETS, "reason": None},
|
|
61
65
|
"get_market_review": {"markets": ["A", "HK", "US"], "reason": "review data coverage is CN/HK/US only"},
|
|
62
66
|
# A-share only
|
|
63
67
|
"get_capital_flow": {"markets": ["A"], "reason": "A-share only (akshare data)"},
|
|
64
68
|
"get_chip_distribution": {"markets": ["A"], "reason": "A-share only"},
|
|
65
|
-
"get_sector_rankings": {"markets": ["A"], "reason": "A-share
|
|
69
|
+
"get_sector_rankings": {"markets": ["A", "US"], "reason": "A-share boards; US sector ETF proxies"},
|
|
66
70
|
"get_sector_constituents": {"markets": ["A"], "reason": "A-share only"},
|
|
67
|
-
"get_market_stats": {"markets": ["A"], "reason": "A-share only"},
|
|
71
|
+
"get_market_stats": {"markets": ["A", "HK", "US"], "reason": "full-market snapshot breadth; limits A-share only"},
|
|
68
72
|
"get_fundamental_context": {"markets": ["A"], "reason": "A-share only"},
|
|
69
|
-
"resolve_stock_name": {"markets": ["A"], "reason": "A
|
|
73
|
+
"resolve_stock_name": {"markets": ["A", "HK", "US"], "reason": "A/HK/US name indexes only"},
|
|
70
74
|
"get_limit_up_pool": {"markets": ["A"], "reason": "A-share only"},
|
|
71
75
|
"get_dragon_tiger": {"markets": ["A"], "reason": "A-share only"},
|
|
72
76
|
"get_hot_stocks": {"markets": ["A"], "reason": "A-share only"},
|
|
@@ -90,6 +94,25 @@ TOOL_MATRIX = {
|
|
|
90
94
|
"record_signal": {"markets": ALL_MARKETS, "reason": None},
|
|
91
95
|
"evaluate_signals": {"markets": ALL_MARKETS, "reason": None},
|
|
92
96
|
"get_signal_summary": {"markets": ALL_MARKETS, "reason": None},
|
|
97
|
+
"evaluate_signal": {"markets": ALL_MARKETS, "reason": None},
|
|
98
|
+
"run_backtest": {"markets": ALL_MARKETS, "reason": None},
|
|
99
|
+
"screen_stocks": {"markets": ["A", "HK", "US"], "reason": "full-market snapshots exist for A/HK/US only"},
|
|
100
|
+
"get_fund_quote": {"markets": ["A"], "reason": "mainland China open-end fund NAV only"},
|
|
101
|
+
# symbol-driven workflows; per-tool market limits above still apply inside them
|
|
102
|
+
"run_watchlist_analysis": {"markets": ALL_MARKETS, "reason": None},
|
|
103
|
+
"build_watchlist_context": {"markets": ALL_MARKETS, "reason": None},
|
|
104
|
+
"analyze_position_context": {"markets": ALL_MARKETS, "reason": None},
|
|
105
|
+
"check_alert_rules": {"markets": ALL_MARKETS, "reason": None},
|
|
106
|
+
"parse_stock_list": {"markets": ALL_MARKETS, "reason": None},
|
|
107
|
+
"get_watchlist": {"markets": ALL_MARKETS, "reason": None},
|
|
108
|
+
"check_trading_day": {"markets": ALL_MARKETS, "reason": None},
|
|
109
|
+
"get_trading_days": {"markets": ALL_MARKETS, "reason": None},
|
|
110
|
+
"get_review_history": {"markets": ["A", "HK", "US"], "reason": "market review archives cover CN/HK/US only"},
|
|
111
|
+
# market-agnostic utilities
|
|
112
|
+
"render_stock_report": {"markets": ALL_MARKETS, "reason": None},
|
|
113
|
+
"render_market_report": {"markets": ["A", "HK", "US"], "reason": "market review reports cover CN/HK/US only"},
|
|
114
|
+
"diagnose_data_sources": {"markets": ALL_MARKETS, "reason": None},
|
|
115
|
+
"get_market_capabilities": {"markets": ALL_MARKETS, "reason": None},
|
|
93
116
|
}
|
|
94
117
|
|
|
95
118
|
|
package/tools/cli_budgets.json
CHANGED
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
"risk_screening.py:screen": 450,
|
|
10
10
|
"signal_tracker.py:evaluate": 300,
|
|
11
11
|
"stock_data.py:market_stats": 240,
|
|
12
|
+
"name_resolver.py:resolve": 360,
|
|
13
|
+
"import_parser.py:parse": 360,
|
|
12
14
|
"market_review.py:review": 300,
|
|
13
15
|
"anomaly_detect.py:detect": 390,
|
|
14
16
|
"position_context.py:analyze": 300,
|
package/tools/diagnostics.py
CHANGED
|
@@ -30,11 +30,11 @@ PROVIDERS = {
|
|
|
30
30
|
"lixinger": (
|
|
31
31
|
"requests",
|
|
32
32
|
["LIXINGER_TOKEN"],
|
|
33
|
-
["A"],
|
|
34
|
-
"optional index
|
|
33
|
+
["A", "HK"],
|
|
34
|
+
"optional index PB, CN/HK company valuation and statements; subscription permissions not probed",
|
|
35
35
|
),
|
|
36
36
|
"efinance": ("efinance", [], ["A"], None),
|
|
37
|
-
"tencent": ("requests", [], ["A"], "qt.gtimg.cn quote/kline, no auth (issue #25)"),
|
|
37
|
+
"tencent": ("requests", [], ["A", "HK", "US"], "qt.gtimg.cn quote/kline, no auth (issue #25)"),
|
|
38
38
|
"sina": (
|
|
39
39
|
"requests",
|
|
40
40
|
[],
|
|
@@ -42,7 +42,6 @@ PROVIDERS = {
|
|
|
42
42
|
"hq.sinajs.cn quote + daily kline, no auth; full-market snapshot via akshare (issue #25)",
|
|
43
43
|
),
|
|
44
44
|
"xueqiu": ("akshare", ["XUEQIU_TOKEN"], ["A"], "industry board fallback via akshare"),
|
|
45
|
-
"pytdx": ("pytdx", [], ["A"], "connects to public Tencent servers"),
|
|
46
45
|
"baostock": ("baostock", [], ["A"], None),
|
|
47
46
|
"yfinance": ("yfinance", [], ["HK", "US", "JP", "KR", "TW"], "quote may be delayed 15-20min"),
|
|
48
47
|
"finnhub": ("finnhub", ["FINNHUB_API_KEY"], ["HK", "US"], None),
|
package/tools/gather.py
CHANGED
|
@@ -106,12 +106,8 @@ def gather_analysis(symbol: str, *, session=None) -> dict:
|
|
|
106
106
|
"news": ("search_intel.py", ["search", _news_query(symbol)]),
|
|
107
107
|
"social_sentiment": ("search_intel.py", ["sentiment", symbol]),
|
|
108
108
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
result = _run_all(tasks, symbol, session=session, with_risk=True)
|
|
112
|
-
if market not in ("A", "HK", "US"):
|
|
113
|
-
result["regime"] = {"error": f"Market regime unavailable for {market}"}
|
|
114
|
-
return result
|
|
109
|
+
tasks["regime"] = ("market_regime.py", ["detect", market])
|
|
110
|
+
return _run_all(tasks, symbol, session=session, with_risk=True)
|
|
115
111
|
|
|
116
112
|
|
|
117
113
|
def gather_technical(symbol: str, kline_count: int = 120, with_quote: bool = False) -> dict:
|
package/tools/import_parser.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
2
|
"""Parse stock list from freeform text / CSV / markdown.
|
|
3
3
|
|
|
4
|
-
Extracts symbols using regex heuristics + name_resolver for A
|
|
4
|
+
Extracts symbols using regex heuristics + name_resolver for A/HK/US Chinese names.
|
|
5
5
|
"""
|
|
6
6
|
|
|
7
7
|
import argparse
|
|
@@ -210,8 +210,7 @@ def parse(text: str) -> dict:
|
|
|
210
210
|
hits = []
|
|
211
211
|
if hits and isinstance(hits, list) and hits[0].get("code"):
|
|
212
212
|
top_hit = hits[0]
|
|
213
|
-
#
|
|
214
|
-
# (e.g. "腾讯" → "拓新药业" via pinyin). Reject low-confidence hits.
|
|
213
|
+
# Reject low-confidence fuzzy matches instead of mapping an unknown name to a stock.
|
|
215
214
|
score = top_hit.get("score")
|
|
216
215
|
hit_name = top_hit.get("name", "")
|
|
217
216
|
is_exact = (name in hit_name) or (hit_name in name)
|
|
@@ -223,7 +222,7 @@ def parse(text: str) -> dict:
|
|
|
223
222
|
items.append(
|
|
224
223
|
{
|
|
225
224
|
"symbol": code,
|
|
226
|
-
"market":
|
|
225
|
+
"market": detect_market(code),
|
|
227
226
|
"name": hit_name,
|
|
228
227
|
"resolved_from": name,
|
|
229
228
|
}
|
package/tools/lixinger.py
CHANGED
|
@@ -1,42 +1,71 @@
|
|
|
1
|
-
"""Optional Lixinger HK non-financial
|
|
1
|
+
"""Optional Lixinger open-API provider: CN/HK non-financial companies and CN indexes.
|
|
2
|
+
|
|
3
|
+
Callers pass a provider market ("cn"/"hk") and the provider's stock code; symbol parsing stays with them."""
|
|
2
4
|
|
|
3
5
|
import os
|
|
4
|
-
from datetime import
|
|
6
|
+
from datetime import timedelta
|
|
5
7
|
|
|
8
|
+
import pandas as pd
|
|
6
9
|
import requests
|
|
7
10
|
from _cache import DAY, cached
|
|
11
|
+
from trading_calendar import market_now
|
|
12
|
+
|
|
13
|
+
API = "https://open.lixinger.com/api"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def post(path, payload):
|
|
17
|
+
response = requests.post(f"{API}/{path}", json={"token": os.environ["LIXINGER_TOKEN"], **payload}, timeout=20)
|
|
18
|
+
response.raise_for_status()
|
|
19
|
+
body = response.json()
|
|
20
|
+
if body.get("code") != 1 or not isinstance(body.get("data"), list):
|
|
21
|
+
raise ValueError(f"Lixinger {path} unavailable; check token permissions/company type")
|
|
22
|
+
return body["data"]
|
|
8
23
|
|
|
9
24
|
|
|
10
|
-
def
|
|
11
|
-
|
|
12
|
-
end = date.today()
|
|
25
|
+
def company_data(market, code, dataset, metrics):
|
|
26
|
+
end = market_now("HK" if market == "hk" else "CN").date()
|
|
13
27
|
try:
|
|
14
28
|
start = end.replace(year=end.year - 10)
|
|
15
29
|
except ValueError:
|
|
16
30
|
start = end.replace(year=end.year - 10, day=28) + timedelta(days=1)
|
|
17
31
|
|
|
18
32
|
def fetch():
|
|
19
|
-
|
|
20
|
-
f"
|
|
21
|
-
|
|
22
|
-
"token": os.environ["LIXINGER_TOKEN"],
|
|
23
|
-
"stockCodes": [code],
|
|
24
|
-
"startDate": start.isoformat(),
|
|
25
|
-
"endDate": end.isoformat(),
|
|
26
|
-
"metricsList": metrics,
|
|
27
|
-
},
|
|
28
|
-
timeout=20,
|
|
33
|
+
data = post(
|
|
34
|
+
f"{market}/company/{dataset}/non_financial",
|
|
35
|
+
{"stockCodes": [code], "startDate": start.isoformat(), "endDate": end.isoformat(), "metricsList": metrics},
|
|
29
36
|
)
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
37
|
+
if not data:
|
|
38
|
+
raise ValueError(f"Lixinger {market} non-financial data unavailable; check token permissions/company type")
|
|
39
|
+
return data
|
|
40
|
+
|
|
41
|
+
return cached(f"lixinger-{market}:{dataset}:{code}:{end}:{','.join(metrics)}", DAY, fetch)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def valuation_history(market, code, years):
|
|
45
|
+
"""Daily PE-TTM/PB within the last `years`; raises when either metric is entirely missing."""
|
|
46
|
+
cutoff = (market_now("HK" if market == "hk" else "CN").date() - pd.DateOffset(years=years)).date().isoformat()
|
|
47
|
+
history = []
|
|
48
|
+
for row in company_data(market, code, "fundamental", ["pe_ttm", "pb"]):
|
|
49
|
+
stamp = str(row.get("date") or "")[:10]
|
|
50
|
+
if stamp >= cutoff:
|
|
51
|
+
history.append({"date": stamp, **{field: number(row.get(field)) for field in ("pe_ttm", "pb")}})
|
|
52
|
+
history.sort(key=lambda r: r["date"])
|
|
53
|
+
if not history or not all(any(r[field] is not None for r in history) for field in ("pe_ttm", "pb")):
|
|
54
|
+
raise ValueError("Lixinger valuation history incomplete")
|
|
55
|
+
return history
|
|
35
56
|
|
|
36
|
-
return cached(f"lixinger-hk:{dataset}:{code}:{end}:{','.join(metrics)}", DAY, fetch)
|
|
37
57
|
|
|
58
|
+
def number(value):
|
|
59
|
+
value = pd.to_numeric(value, errors="coerce")
|
|
60
|
+
return float(value) if pd.notna(value) and abs(value) != float("inf") else None
|
|
38
61
|
|
|
39
|
-
|
|
62
|
+
|
|
63
|
+
def metric(row, field):
|
|
64
|
+
table, name = field.split(".")
|
|
65
|
+
return row.get("q", {}).get(table, {}).get(name, {}).get("t")
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def financials_hk(code, periods, annual):
|
|
40
69
|
fields = {
|
|
41
70
|
"roe": "m.roe",
|
|
42
71
|
"gross_margin": "ps.gp_m",
|
|
@@ -45,12 +74,12 @@ def financials_hk(symbol, periods, annual):
|
|
|
45
74
|
"net_income": "ps.np",
|
|
46
75
|
"operating_cash_flow": "cfs.ncffoa",
|
|
47
76
|
}
|
|
48
|
-
data =
|
|
77
|
+
data = company_data("hk", code, "fs", [f"q.{field}.t" for field in fields.values()])
|
|
49
78
|
history = []
|
|
50
79
|
for row in sorted(data, key=lambda r: r.get("date", ""), reverse=True):
|
|
51
80
|
if annual and row.get("reportType") != "annual_report":
|
|
52
81
|
continue
|
|
53
|
-
if not row.get("date") or row.get("stockCode",
|
|
82
|
+
if not row.get("date") or row.get("stockCode", code) != code:
|
|
54
83
|
continue
|
|
55
84
|
entry = {
|
|
56
85
|
"report_date": row["date"][:10],
|
|
@@ -60,8 +89,7 @@ def financials_hk(symbol, periods, annual):
|
|
|
60
89
|
"source": "lixinger",
|
|
61
90
|
}
|
|
62
91
|
for key, field in fields.items():
|
|
63
|
-
|
|
64
|
-
value = row.get("q", {}).get(table, {}).get(metric, {}).get("t")
|
|
92
|
+
value = metric(row, field)
|
|
65
93
|
entry[key] = (
|
|
66
94
|
value * 100 if value is not None and key in ("roe", "gross_margin", "net_profit_margin") else value
|
|
67
95
|
)
|
|
@@ -74,7 +102,6 @@ def financials_hk(symbol, periods, annual):
|
|
|
74
102
|
result = {
|
|
75
103
|
**dict.fromkeys(("name", "market_cap", "pe", "forward_pe", "pb", "debt_to_equity", "dividend_yield")),
|
|
76
104
|
"snapshot_available": False,
|
|
77
|
-
"symbol": symbol,
|
|
78
105
|
"source": "lixinger",
|
|
79
106
|
"report_date": latest["report_date"],
|
|
80
107
|
"currency": latest["currency"],
|
|
@@ -98,3 +125,68 @@ def financials_hk(symbol, periods, annual):
|
|
|
98
125
|
if len(history) < periods:
|
|
99
126
|
result["warnings"] = ["Requested financial history incomplete; only actual available periods returned"]
|
|
100
127
|
return result
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
# Field codes from https://www.lixinger.com/api/open-api/html-doc/{cn,hk}/company/fs/non_financial.
|
|
131
|
+
# HK has no operating income, capex or D&A; the caller fills those from Yahoo.
|
|
132
|
+
DCF_FIELDS = {
|
|
133
|
+
"cn": {
|
|
134
|
+
"revenue": ("ps.toi",),
|
|
135
|
+
"operating_income": ("ps.op",),
|
|
136
|
+
"pretax_income": ("ps.tp",),
|
|
137
|
+
"income_tax": ("ps.ite",),
|
|
138
|
+
"operating_cash_flow": ("cfs.ncffoa",),
|
|
139
|
+
"capital_expenditure": ("cfs.cpfpfiaolta",),
|
|
140
|
+
"depreciation_amortization": ("cfs.daa",),
|
|
141
|
+
"cash": ("cfs.bocaceatpe",),
|
|
142
|
+
"total_debt": ("bs.stl", "bs.stbp", "bs.ncldwioy", "bs.ltl", "bs.bp"),
|
|
143
|
+
"lease_liabilities": ("bs.ll",),
|
|
144
|
+
"minority_interest": ("bs.etmsh",),
|
|
145
|
+
"shares_outstanding": ("bs.tsc",),
|
|
146
|
+
},
|
|
147
|
+
"hk": {
|
|
148
|
+
"revenue": ("ps.toi",),
|
|
149
|
+
"pretax_income": ("ps.tp",),
|
|
150
|
+
"income_tax": ("ps.ite",),
|
|
151
|
+
"operating_cash_flow": ("cfs.ncffoa",),
|
|
152
|
+
"cash": ("cfs.bocaceatpe",),
|
|
153
|
+
"total_debt": (
|
|
154
|
+
"bs.stl",
|
|
155
|
+
"bs.stpl",
|
|
156
|
+
"bs.stbp",
|
|
157
|
+
"bs.stlcofcb",
|
|
158
|
+
"bs.stsn",
|
|
159
|
+
"bs.ltl",
|
|
160
|
+
"bs.ltpl",
|
|
161
|
+
"bs.sn",
|
|
162
|
+
"bs.bp",
|
|
163
|
+
"bs.ltlcofcb",
|
|
164
|
+
),
|
|
165
|
+
"lease_liabilities": ("bs.stll", "bs.ltll"),
|
|
166
|
+
"minority_interest": ("bs.etmsh",),
|
|
167
|
+
"shares_outstanding": ("bs.tsc",),
|
|
168
|
+
},
|
|
169
|
+
}
|
|
170
|
+
DCF_WARNINGS = {
|
|
171
|
+
"cn": "Lixinger CN statements; operating_income is 营业利润 (includes investment and fair-value gains, not EBIT); "
|
|
172
|
+
"total_debt sums borrowings, bonds and current non-current liabilities "
|
|
173
|
+
"(the latter may include current lease liabilities); long-term leases are in lease_liabilities.",
|
|
174
|
+
"hk": "Lixinger HK statements; total_debt sums borrowings, bonds, notes and convertibles and excludes leases.",
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def dcf_inputs(market, code, periods):
|
|
179
|
+
fields = DCF_FIELDS[market]
|
|
180
|
+
metrics = [f"q.{field}.t" for names in fields.values() for field in names]
|
|
181
|
+
rows = []
|
|
182
|
+
for row in sorted(company_data(market, code, "fs", metrics), key=lambda r: r.get("date", ""), reverse=True):
|
|
183
|
+
if row.get("reportType") != "annual_report" or not row.get("date") or row.get("stockCode", code) != code:
|
|
184
|
+
continue
|
|
185
|
+
entry = {"period_end": row["date"][:10], "currency": row.get("currency")}
|
|
186
|
+
for key, names in fields.items():
|
|
187
|
+
values = [value for name in names if (value := metric(row, name)) is not None]
|
|
188
|
+
entry[key] = sum(values) if values else None
|
|
189
|
+
rows.append(entry)
|
|
190
|
+
if not rows:
|
|
191
|
+
raise ValueError(f"Lixinger returned no annual {market} statements")
|
|
192
|
+
return {"source": "lixinger", "periods": rows[:periods], "warnings": [DCF_WARNINGS[market]]}
|
package/tools/market_regime.py
CHANGED
|
@@ -18,6 +18,9 @@ INDEX_MAP = {
|
|
|
18
18
|
"A": ("000001", "上证综指"),
|
|
19
19
|
"HK": ("^HSI", "恒生指数"),
|
|
20
20
|
"US": ("^GSPC", "标普500"),
|
|
21
|
+
"JP": ("^N225", "日经225"),
|
|
22
|
+
"KR": ("^KS11", "韩国KOSPI"),
|
|
23
|
+
"TW": ("^TWII", "台湾加权指数"),
|
|
21
24
|
}
|
|
22
25
|
|
|
23
26
|
REGIME_CN = {
|
|
@@ -166,7 +169,7 @@ def _detect_sector_heat(market: str) -> bool:
|
|
|
166
169
|
def detect_regime(market: str = "A") -> dict:
|
|
167
170
|
market = market.upper()
|
|
168
171
|
if market not in INDEX_MAP:
|
|
169
|
-
return {"error": f"Unsupported market: {market}. Use
|
|
172
|
+
return {"error": f"Unsupported market: {market}. Use {'/'.join(INDEX_MAP)}"}
|
|
170
173
|
|
|
171
174
|
code, name = INDEX_MAP[market]
|
|
172
175
|
klines = _get_index_kline(market)
|
|
@@ -229,7 +232,7 @@ def main():
|
|
|
229
232
|
sub = parser.add_subparsers(dest="command")
|
|
230
233
|
|
|
231
234
|
p_detect = sub.add_parser("detect")
|
|
232
|
-
p_detect.add_argument("market", nargs="?", default="A", choices=
|
|
235
|
+
p_detect.add_argument("market", nargs="?", default="A", choices=list(INDEX_MAP))
|
|
233
236
|
|
|
234
237
|
args = parser.parse_args()
|
|
235
238
|
if not args.command:
|
package/tools/market_review.py
CHANGED
|
@@ -18,9 +18,10 @@ from pathlib import Path
|
|
|
18
18
|
|
|
19
19
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
20
20
|
|
|
21
|
-
from _subproc import command_timeout, utf8_stdio
|
|
21
|
+
from _subproc import command_timeout, error_message, utf8_stdio
|
|
22
22
|
from _subproc import run_tool as _run_tool
|
|
23
23
|
from signal_tracker import _append_record, _load_records, _store_lock
|
|
24
|
+
from trading_calendar import market_now
|
|
24
25
|
|
|
25
26
|
|
|
26
27
|
def _index_change(row):
|
|
@@ -81,8 +82,6 @@ def _signal_to_stance(signal: str) -> str | None:
|
|
|
81
82
|
|
|
82
83
|
def review_market(market: str = "A", *, run=None) -> dict:
|
|
83
84
|
"""Gather market data and compute review for a single market."""
|
|
84
|
-
from datetime import datetime
|
|
85
|
-
|
|
86
85
|
tasks = {}
|
|
87
86
|
|
|
88
87
|
if market == "A":
|
|
@@ -135,7 +134,7 @@ def review_market(market: str = "A", *, run=None) -> dict:
|
|
|
135
134
|
|
|
136
135
|
output = {
|
|
137
136
|
"market": market,
|
|
138
|
-
"date":
|
|
137
|
+
"date": market_now("CN" if market == "A" else market).strftime("%Y-%m-%d"),
|
|
139
138
|
"indices": results.get("indices"),
|
|
140
139
|
"stats": results.get("stats"),
|
|
141
140
|
"sectors": results.get("sectors"),
|
|
@@ -169,7 +168,7 @@ def save_review(report: dict, store: Path | None = None) -> dict:
|
|
|
169
168
|
with _store_lock(store):
|
|
170
169
|
_append_record(store, report)
|
|
171
170
|
except (OSError, ValueError) as e:
|
|
172
|
-
return {"error":
|
|
171
|
+
return {"error": error_message(e)}
|
|
173
172
|
return {"saved": True, "date": report.get("review_date") or report.get("date")}
|
|
174
173
|
|
|
175
174
|
|
|
@@ -181,7 +180,7 @@ def cmd_history(args) -> dict:
|
|
|
181
180
|
records = _load_records(store)
|
|
182
181
|
except (OSError, UnicodeDecodeError) as e:
|
|
183
182
|
# store is a directory / not UTF-8 — same clean-error contract as save_review
|
|
184
|
-
return {"error":
|
|
183
|
+
return {"error": error_message(e)}
|
|
185
184
|
limit = max(args.limit, 0)
|
|
186
185
|
recent = records[-limit:][::-1] if limit else []
|
|
187
186
|
return {"count": len(recent), "records": recent}
|