stock-analysis-plugin 0.2.11 → 0.2.13
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 +234 -382
- package/docs/mcp.md +60 -0
- package/hermes/__init__.py +35 -0
- package/hermes/plugin.yaml +132 -0
- package/hermes/schemas.py +6 -0
- package/hermes/tools.py +647 -0
- package/mcp_host/__init__.py +1 -0
- package/mcp_host/__main__.py +3 -0
- package/mcp_host/server.py +222 -0
- package/package.json +7 -1
- package/pi/index.ts +52 -2906
- 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 +493 -0
- package/skills/bull-trend/SKILL.md +2 -2
- package/skills/bull-trend/references/fat-tails.md +76 -3
- package/skills/bull-trend/scripts/tail_analysis.py +92 -3
- package/skills/event-driven/SKILL.md +2 -2
- package/skills/growth-quality/SKILL.md +1 -1
- package/skills/market-review/SKILL.md +1 -0
- package/skills/stock-analysis/SKILL.md +12 -7
- package/skills/stock-analysis/references/stateless-analysis.md +9 -1
- package/skills/stock-screener/SKILL.md +1 -1
- package/skills/strategy-backtest/SKILL.md +1 -1
- package/tools/_cache.py +13 -0
- package/tools/_subproc.py +46 -0
- package/tools/alert_rules.py +2 -2
- package/tools/backtest.py +3 -3
- package/tools/capabilities.py +33 -9
- package/tools/cli_budgets.json +1 -0
- package/tools/credit_data.py +293 -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 +45 -33
- package/tools/report_renderer.py +3 -3
- package/tools/requirements.txt +5 -4
- package/tools/risk_screening.py +3 -2
- package/tools/screener.py +41 -30
- package/tools/search_intel.py +79 -10
- package/tools/sec_edgar.py +268 -1
- package/tools/sec_holders.py +6 -0
- package/tools/signal_tracker.py +5 -5
- package/tools/stateless_analysis.py +132 -19
- package/tools/stock_data.py +1020 -817
- package/tools/tiaa_disclosures.py +4 -0
- package/tools/tool_schemas.json +2734 -0
- package/tools/trading_calendar.py +65 -30
- package/tools/value_data.py +423 -175
- package/tools/watchlist.py +43 -5
package/tools/capabilities.py
CHANGED
|
@@ -30,21 +30,26 @@ 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"},
|
|
45
|
+
"get_cds_index": {"markets": ["A", "US"], "reason": "Market-level CN onshore CDS indices / CDX-iTraxx only"},
|
|
41
46
|
"get_earnings_calendar": {
|
|
42
|
-
"markets": ["A", "HK"],
|
|
43
|
-
"reason": "A-share disclosed calendar / HK single-symbol provider schedule",
|
|
47
|
+
"markets": ["A", "HK", "US", "JP"],
|
|
48
|
+
"reason": "A-share disclosed calendar / HK/US/JP single-symbol provider schedule",
|
|
44
49
|
},
|
|
45
50
|
"get_shareholder_structure": {"markets": ["A", "HK"], "reason": "A-share shareholder reports / HK southbound only"},
|
|
46
51
|
"get_etf_details": {"markets": ["A"], "reason": "A-share ETF portfolio/NAV/size only"},
|
|
47
|
-
"get_dividends": {"markets":
|
|
52
|
+
"get_dividends": {"markets": ALL_MARKETS, "reason": None}, # ranking mode stays A/HK only
|
|
48
53
|
"get_kline": {"markets": ALL_MARKETS, "reason": None},
|
|
49
54
|
"get_quote": {"markets": ALL_MARKETS, "reason": None},
|
|
50
55
|
"get_news": {"markets": ALL_MARKETS, "reason": None},
|
|
@@ -57,16 +62,16 @@ TOOL_MATRIX = {
|
|
|
57
62
|
"detect_anomaly": {"markets": ALL_MARKETS, "reason": None},
|
|
58
63
|
"get_stock_info": {"markets": ALL_MARKETS, "reason": None},
|
|
59
64
|
"resolve_stock_sectors": {"markets": ["A", "HK"], "reason": "A-share and HK only"},
|
|
60
|
-
"detect_market_regime": {"markets":
|
|
65
|
+
"detect_market_regime": {"markets": ALL_MARKETS, "reason": None},
|
|
61
66
|
"get_market_review": {"markets": ["A", "HK", "US"], "reason": "review data coverage is CN/HK/US only"},
|
|
62
67
|
# A-share only
|
|
63
68
|
"get_capital_flow": {"markets": ["A"], "reason": "A-share only (akshare data)"},
|
|
64
69
|
"get_chip_distribution": {"markets": ["A"], "reason": "A-share only"},
|
|
65
|
-
"get_sector_rankings": {"markets": ["A"], "reason": "A-share
|
|
70
|
+
"get_sector_rankings": {"markets": ["A", "US"], "reason": "A-share boards; US sector ETF proxies"},
|
|
66
71
|
"get_sector_constituents": {"markets": ["A"], "reason": "A-share only"},
|
|
67
|
-
"get_market_stats": {"markets": ["A"], "reason": "A-share only"},
|
|
72
|
+
"get_market_stats": {"markets": ["A", "HK", "US"], "reason": "full-market snapshot breadth; limits A-share only"},
|
|
68
73
|
"get_fundamental_context": {"markets": ["A"], "reason": "A-share only"},
|
|
69
|
-
"resolve_stock_name": {"markets": ["A"], "reason": "A
|
|
74
|
+
"resolve_stock_name": {"markets": ["A", "HK", "US"], "reason": "A/HK/US name indexes only"},
|
|
70
75
|
"get_limit_up_pool": {"markets": ["A"], "reason": "A-share only"},
|
|
71
76
|
"get_dragon_tiger": {"markets": ["A"], "reason": "A-share only"},
|
|
72
77
|
"get_hot_stocks": {"markets": ["A"], "reason": "A-share only"},
|
|
@@ -90,6 +95,25 @@ TOOL_MATRIX = {
|
|
|
90
95
|
"record_signal": {"markets": ALL_MARKETS, "reason": None},
|
|
91
96
|
"evaluate_signals": {"markets": ALL_MARKETS, "reason": None},
|
|
92
97
|
"get_signal_summary": {"markets": ALL_MARKETS, "reason": None},
|
|
98
|
+
"evaluate_signal": {"markets": ALL_MARKETS, "reason": None},
|
|
99
|
+
"run_backtest": {"markets": ALL_MARKETS, "reason": None},
|
|
100
|
+
"screen_stocks": {"markets": ["A", "HK", "US"], "reason": "full-market snapshots exist for A/HK/US only"},
|
|
101
|
+
"get_fund_quote": {"markets": ["A"], "reason": "mainland China open-end fund NAV only"},
|
|
102
|
+
# symbol-driven workflows; per-tool market limits above still apply inside them
|
|
103
|
+
"run_watchlist_analysis": {"markets": ALL_MARKETS, "reason": None},
|
|
104
|
+
"build_watchlist_context": {"markets": ALL_MARKETS, "reason": None},
|
|
105
|
+
"analyze_position_context": {"markets": ALL_MARKETS, "reason": None},
|
|
106
|
+
"check_alert_rules": {"markets": ALL_MARKETS, "reason": None},
|
|
107
|
+
"parse_stock_list": {"markets": ALL_MARKETS, "reason": None},
|
|
108
|
+
"get_watchlist": {"markets": ALL_MARKETS, "reason": None},
|
|
109
|
+
"check_trading_day": {"markets": ALL_MARKETS, "reason": None},
|
|
110
|
+
"get_trading_days": {"markets": ALL_MARKETS, "reason": None},
|
|
111
|
+
"get_review_history": {"markets": ["A", "HK", "US"], "reason": "market review archives cover CN/HK/US only"},
|
|
112
|
+
# market-agnostic utilities
|
|
113
|
+
"render_stock_report": {"markets": ALL_MARKETS, "reason": None},
|
|
114
|
+
"render_market_report": {"markets": ["A", "HK", "US"], "reason": "market review reports cover CN/HK/US only"},
|
|
115
|
+
"diagnose_data_sources": {"markets": ALL_MARKETS, "reason": None},
|
|
116
|
+
"get_market_capabilities": {"markets": ALL_MARKETS, "reason": None},
|
|
93
117
|
}
|
|
94
118
|
|
|
95
119
|
|
package/tools/cli_budgets.json
CHANGED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Market-level CDS index levels: CFETS-SHCH onshore indices and DTCC-disseminated CDX/iTraxx trades."""
|
|
3
|
+
|
|
4
|
+
import argparse
|
|
5
|
+
import csv
|
|
6
|
+
import io
|
|
7
|
+
import json
|
|
8
|
+
import math
|
|
9
|
+
import sys
|
|
10
|
+
import zipfile
|
|
11
|
+
from collections import Counter, defaultdict
|
|
12
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
13
|
+
from datetime import date, timedelta
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
from statistics import median
|
|
16
|
+
|
|
17
|
+
import requests
|
|
18
|
+
|
|
19
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
20
|
+
from _cache import WEEK, cached
|
|
21
|
+
from _subproc import error_message, scrub_secrets, utf8_stdio
|
|
22
|
+
from trading_calendar import market_now
|
|
23
|
+
|
|
24
|
+
CN_LATEST = "https://www.chinamoney.com.cn/r/cms/www/chinamoney/data/bond/CDSIndex.json"
|
|
25
|
+
CN_HISTORY = "https://www.chinamoney.com.cn/ags/ms/cm-u-bk-bond/CDSIndexHis"
|
|
26
|
+
CN_HEADERS = {"User-Agent": "Mozilla/5.0", "Referer": "https://www.chinamoney.com.cn/chinese/cdsindx/"}
|
|
27
|
+
# The provider rejects pageSize >= 100 with HTTP 403 and windows longer than one calendar month.
|
|
28
|
+
PAGE_SIZE = 50
|
|
29
|
+
CN_TTL = 4 * 3600
|
|
30
|
+
DTCC_LISTING = "https://kgc0418-tdw-data-0.s3.amazonaws.com/dashboard/Cumulative.json"
|
|
31
|
+
# CDX HY trades on price; DTCC reports it like spreads (0.010575 = 105.75), so both scale by 10000.
|
|
32
|
+
GLOBAL_INDICES = {
|
|
33
|
+
"CDX.NA.IG": "spread_bp",
|
|
34
|
+
"CDX.NA.HY": "price",
|
|
35
|
+
"ITRAXX EUROPE": "spread_bp",
|
|
36
|
+
"ITRAXX EUROPE CROSSOVER": "spread_bp",
|
|
37
|
+
}
|
|
38
|
+
NOTES = [
|
|
39
|
+
"Market-level credit risk appetite, not single-name CDS or a stock signal.",
|
|
40
|
+
"Rising spreads, or a falling CDX.NA.HY price, mean weaker credit.",
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def today():
|
|
45
|
+
return market_now("A").date()
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def summarize(series):
|
|
49
|
+
values = [value for _, value in series]
|
|
50
|
+
latest = values[-1]
|
|
51
|
+
return {
|
|
52
|
+
"start_date": series[0][0],
|
|
53
|
+
"start": values[0],
|
|
54
|
+
"latest_date": series[-1][0],
|
|
55
|
+
"latest": latest,
|
|
56
|
+
"change": round(latest - values[0], 2),
|
|
57
|
+
"min": min(values),
|
|
58
|
+
"max": max(values),
|
|
59
|
+
"percentile": round(sum(value <= latest for value in values) / len(values) * 100, 1),
|
|
60
|
+
"observations": len(values),
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def month_windows(start, end):
|
|
65
|
+
windows = []
|
|
66
|
+
while start <= end:
|
|
67
|
+
# 28 days always fits a calendar month, February included.
|
|
68
|
+
stop = min(start + timedelta(days=27), end)
|
|
69
|
+
windows.append((start, stop))
|
|
70
|
+
start = stop + timedelta(days=1)
|
|
71
|
+
return windows
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def number(value):
|
|
75
|
+
try:
|
|
76
|
+
value = float(value)
|
|
77
|
+
except (TypeError, ValueError):
|
|
78
|
+
return None
|
|
79
|
+
return value if math.isfinite(value) else None
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def pick(candidates, query):
|
|
83
|
+
"""Keys whose names equal the query, else contain it (case-insensitive); empty query keeps all."""
|
|
84
|
+
keys = [key for key, _ in candidates]
|
|
85
|
+
query = query.strip()
|
|
86
|
+
if not query:
|
|
87
|
+
return keys
|
|
88
|
+
folded = query.casefold()
|
|
89
|
+
for hit in (lambda name: name.casefold() == folded, lambda name: folded in name.casefold()):
|
|
90
|
+
matched = [key for key, names in candidates if any(name and hit(name) for name in names)]
|
|
91
|
+
if matched:
|
|
92
|
+
return matched
|
|
93
|
+
raise ValueError(f"unknown CDS index {query!r}; available: {', '.join(keys)}")
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def cn_rows(name, start, end):
|
|
97
|
+
rows, page = [], 1
|
|
98
|
+
while True:
|
|
99
|
+
params = {
|
|
100
|
+
"indxName": name,
|
|
101
|
+
"startDate": start.isoformat(),
|
|
102
|
+
"endDate": end.isoformat(),
|
|
103
|
+
"pageNum": page,
|
|
104
|
+
"pageSize": PAGE_SIZE,
|
|
105
|
+
}
|
|
106
|
+
response = requests.post(CN_HISTORY, params=params, headers=CN_HEADERS, timeout=20)
|
|
107
|
+
response.raise_for_status()
|
|
108
|
+
payload = response.json()
|
|
109
|
+
if (payload.get("data") or {}).get("errorMessage"):
|
|
110
|
+
raise ValueError(f"chinamoney: {payload['data']['errorMessage']}")
|
|
111
|
+
records = payload.get("records") or []
|
|
112
|
+
rows += records
|
|
113
|
+
if not records or len(rows) >= int((payload.get("data") or {}).get("total") or 0):
|
|
114
|
+
return rows
|
|
115
|
+
page += 1
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def cn_index(start, end, query, tenor):
|
|
119
|
+
response = requests.post(CN_LATEST, headers=CN_HEADERS, timeout=20)
|
|
120
|
+
response.raise_for_status()
|
|
121
|
+
records = response.json().get("records") or []
|
|
122
|
+
wanted = set(pick([(record["cnName"], [record["cnName"], record.get("enName")]) for record in records], query))
|
|
123
|
+
indices = []
|
|
124
|
+
available = set()
|
|
125
|
+
for record in records:
|
|
126
|
+
name = record["cnName"]
|
|
127
|
+
if name not in wanted:
|
|
128
|
+
continue
|
|
129
|
+
curve = {row["prd"]: number(row["cdIndxVl"]) for row in record.get("dataList") or []}
|
|
130
|
+
curve = {key: value for key, value in curve.items() if value is not None}
|
|
131
|
+
available |= set(curve)
|
|
132
|
+
if tenor and tenor not in curve:
|
|
133
|
+
continue
|
|
134
|
+
series = defaultdict(list)
|
|
135
|
+
for window in month_windows(start, end):
|
|
136
|
+
key = f"cds_cn:{name}:{window[0]}:{window[1]}"
|
|
137
|
+
for row in cached(key, CN_TTL, lambda name=name, window=window: cn_rows(name, *window)):
|
|
138
|
+
if (value := number(row.get("cdIndxVl"))) is not None and (not tenor or row["prd"] == tenor):
|
|
139
|
+
series[row["prd"]].append((row["date"], value))
|
|
140
|
+
indices.append(
|
|
141
|
+
{
|
|
142
|
+
"name": name,
|
|
143
|
+
"name_en": record.get("enName"),
|
|
144
|
+
"latest_date": record.get("maxDate"),
|
|
145
|
+
"curve": {key: value for key, value in curve.items() if not tenor or key == tenor},
|
|
146
|
+
"history": {key: summarize(sorted(rows)) for key, rows in series.items()},
|
|
147
|
+
}
|
|
148
|
+
)
|
|
149
|
+
if tenor and not indices:
|
|
150
|
+
raise ValueError(f"tenor {tenor} unavailable; available: {', '.join(sorted(available))}")
|
|
151
|
+
if not indices:
|
|
152
|
+
raise ValueError("chinamoney returned no CDS indices")
|
|
153
|
+
return {
|
|
154
|
+
"source": "cn",
|
|
155
|
+
"provider": "chinamoney.com.cn (CFETS-SHCH CDS indices)",
|
|
156
|
+
"unit": "bp",
|
|
157
|
+
"start_date": start.isoformat(),
|
|
158
|
+
"end_date": end.isoformat(),
|
|
159
|
+
"indices": indices,
|
|
160
|
+
"notes": [*NOTES, "Provider history covers about the latest year."],
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def global_day(rows, day):
|
|
165
|
+
result = {}
|
|
166
|
+
for name in GLOBAL_INDICES:
|
|
167
|
+
trades = [
|
|
168
|
+
row
|
|
169
|
+
for row in rows
|
|
170
|
+
if row.get("UPI Underlier Name") == name
|
|
171
|
+
and row.get("UPI FISN") == "NA/CDS Corp Idx"
|
|
172
|
+
and row.get("Action type") == "NEWT"
|
|
173
|
+
and row.get("Event type") == "TRAD"
|
|
174
|
+
and row.get("Package indicator") == "FALSE"
|
|
175
|
+
and row.get("Spread notation-Leg 1") == "3"
|
|
176
|
+
and str(row.get("Execution Timestamp", "")).startswith(day)
|
|
177
|
+
and number(row.get("Spread-Leg 1")) is not None
|
|
178
|
+
]
|
|
179
|
+
if not trades:
|
|
180
|
+
continue
|
|
181
|
+
# The most-traded maturity is the on-the-run 5Y series.
|
|
182
|
+
maturity = Counter(row["Expiration Date"] for row in trades).most_common(1)[0][0]
|
|
183
|
+
values = [number(row["Spread-Leg 1"]) * 10000 for row in trades if row["Expiration Date"] == maturity]
|
|
184
|
+
result[name] = {"value": round(median(values), 2), "trades": len(values), "maturity": maturity}
|
|
185
|
+
return result
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def dtcc_day(path, day):
|
|
189
|
+
response = requests.get(path, timeout=30)
|
|
190
|
+
response.raise_for_status()
|
|
191
|
+
with zipfile.ZipFile(io.BytesIO(response.content)) as archive:
|
|
192
|
+
text = archive.read(archive.namelist()[0]).decode("utf-8-sig")
|
|
193
|
+
return global_day(list(csv.DictReader(io.StringIO(text))), day)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def global_index(days, query):
|
|
197
|
+
names = pick([(name, [name]) for name in GLOBAL_INDICES], query)
|
|
198
|
+
response = requests.get(DTCC_LISTING, timeout=20)
|
|
199
|
+
response.raise_for_status()
|
|
200
|
+
listed = []
|
|
201
|
+
for item in response.json().get("CFTC_CR") or []:
|
|
202
|
+
stamp = item["fileName"].removesuffix(".zip").rsplit("_", 3)[-3:]
|
|
203
|
+
day = date(*map(int, stamp))
|
|
204
|
+
# Weekend files carry no index trades. shortcut: holidays are not skipped, add a calendar if they matter.
|
|
205
|
+
if day.weekday() < 5:
|
|
206
|
+
listed.append((day, item["fullFilePath"]))
|
|
207
|
+
if not listed:
|
|
208
|
+
raise ValueError("DTCC listing has no daily files")
|
|
209
|
+
# A UTC day's file is published after that day closes, so the window ends at the newest weekday file, not today.
|
|
210
|
+
end = max(day for day, _ in listed)
|
|
211
|
+
start = end - timedelta(days=days - 1)
|
|
212
|
+
files = [(day.isoformat(), path) for day, path in listed if start <= day <= end]
|
|
213
|
+
|
|
214
|
+
def load(file):
|
|
215
|
+
day, path = file
|
|
216
|
+
try:
|
|
217
|
+
return day, cached(f"cds_dtcc:{day}", WEEK, lambda: dtcc_day(path, day)), None
|
|
218
|
+
except (requests.RequestException, zipfile.BadZipFile, ValueError, KeyError) as exc:
|
|
219
|
+
return day, {}, error_message(exc)
|
|
220
|
+
|
|
221
|
+
with ThreadPoolExecutor(max_workers=6) as pool:
|
|
222
|
+
loaded = sorted(pool.map(load, files))
|
|
223
|
+
failed = [{"date": day, "error": error} for day, _, error in loaded if error]
|
|
224
|
+
indices = []
|
|
225
|
+
for name in names:
|
|
226
|
+
quote_type = GLOBAL_INDICES[name]
|
|
227
|
+
observed = [(day, values[name]) for day, values, _ in loaded if name in values]
|
|
228
|
+
if not observed:
|
|
229
|
+
continue
|
|
230
|
+
latest = observed[-1][1]
|
|
231
|
+
# Series rolls change maturity and constituents; compare only the latest series.
|
|
232
|
+
series = [(day, item["value"]) for day, item in observed if item["maturity"] == latest["maturity"]]
|
|
233
|
+
indices.append(
|
|
234
|
+
{
|
|
235
|
+
"name": name,
|
|
236
|
+
"quote_type": quote_type,
|
|
237
|
+
"maturity": latest["maturity"],
|
|
238
|
+
"trades": latest["trades"],
|
|
239
|
+
"summary": summarize(series),
|
|
240
|
+
}
|
|
241
|
+
)
|
|
242
|
+
if not indices:
|
|
243
|
+
raise ValueError("DTCC returned no CDX/iTraxx index trades in the window")
|
|
244
|
+
return {
|
|
245
|
+
"source": "global",
|
|
246
|
+
"provider": "DTCC CFTC public dissemination (median of new on-the-run trades per day)",
|
|
247
|
+
"start_date": start.isoformat(),
|
|
248
|
+
"end_date": end.isoformat(),
|
|
249
|
+
"files": len(files),
|
|
250
|
+
"failed_files": failed,
|
|
251
|
+
"indices": indices,
|
|
252
|
+
"notes": [*NOTES, "Daily medians of public trade prints, not official closing levels."],
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def cds_index(args):
|
|
257
|
+
if not 1 <= args.days <= 90:
|
|
258
|
+
raise ValueError("days must be 1..90")
|
|
259
|
+
tenor = args.tenor.strip().upper()
|
|
260
|
+
if args.source == "global":
|
|
261
|
+
if tenor not in ("", "5Y"):
|
|
262
|
+
raise ValueError("global CDX/iTraxx levels are on-the-run 5Y only")
|
|
263
|
+
return global_index(args.days, args.index)
|
|
264
|
+
end = today()
|
|
265
|
+
return cn_index(end - timedelta(days=args.days - 1), end, args.index, tenor)
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def main():
|
|
269
|
+
parser = argparse.ArgumentParser(description="Market-level CDS index levels")
|
|
270
|
+
sub = parser.add_subparsers(dest="command")
|
|
271
|
+
p = sub.add_parser("cds_index")
|
|
272
|
+
p.add_argument("--source", choices=["cn", "global"], default="cn")
|
|
273
|
+
p.add_argument("--days", type=int, default=30)
|
|
274
|
+
p.add_argument("--index", default="", help="Exact or partial index name, e.g. 民企, 科创, CDX.NA.HY")
|
|
275
|
+
p.add_argument("--tenor", default="", help="CN tenor such as 3M or 1Y; global is 5Y only")
|
|
276
|
+
try:
|
|
277
|
+
args = parser.parse_args()
|
|
278
|
+
if args.command != "cds_index":
|
|
279
|
+
raise ValueError("a subcommand is required")
|
|
280
|
+
result = cds_index(args)
|
|
281
|
+
print(json.dumps(result, ensure_ascii=False, allow_nan=False))
|
|
282
|
+
except SystemExit as exc:
|
|
283
|
+
if exc.code:
|
|
284
|
+
print(json.dumps({"error": "invalid CLI arguments"}))
|
|
285
|
+
raise
|
|
286
|
+
except Exception as exc:
|
|
287
|
+
print(json.dumps({"error": scrub_secrets(str(exc))}, ensure_ascii=False))
|
|
288
|
+
sys.exit(1)
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
if __name__ == "__main__":
|
|
292
|
+
utf8_stdio()
|
|
293
|
+
main()
|
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]]}
|