stock-analysis-plugin 0.1.10
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 +498 -0
- package/package.json +88 -0
- package/pi/index.ts +1142 -0
- package/schemas/market_review_schema.json +176 -0
- package/schemas/report_schema.json +131 -0
- package/scripts/setup-python.mjs +57 -0
- package/scripts/sync-version.sh +56 -0
- package/scripts/venv-python.mjs +20 -0
- package/skills/bottom-volume/SKILL.md +63 -0
- package/skills/bottom-volume/scripts/gather.py +22 -0
- package/skills/box-oscillation/SKILL.md +72 -0
- package/skills/box-oscillation/scripts/gather.py +22 -0
- package/skills/bull-trend/SKILL.md +60 -0
- package/skills/bull-trend/scripts/gather.py +22 -0
- package/skills/chan-theory/SKILL.md +71 -0
- package/skills/chan-theory/scripts/gather.py +22 -0
- package/skills/dragon-head/SKILL.md +69 -0
- package/skills/dragon-head/scripts/gather.py +22 -0
- package/skills/emotion-cycle/SKILL.md +65 -0
- package/skills/emotion-cycle/scripts/gather.py +22 -0
- package/skills/event-driven/SKILL.md +73 -0
- package/skills/event-driven/scripts/gather.py +22 -0
- package/skills/expectation-repricing/SKILL.md +72 -0
- package/skills/expectation-repricing/scripts/gather.py +22 -0
- package/skills/growth-quality/SKILL.md +74 -0
- package/skills/growth-quality/scripts/gather.py +22 -0
- package/skills/hot-theme/SKILL.md +74 -0
- package/skills/hot-theme/scripts/gather.py +22 -0
- package/skills/ma-crossover/SKILL.md +63 -0
- package/skills/ma-crossover/scripts/gather.py +22 -0
- package/skills/market-review/SKILL.md +70 -0
- package/skills/market-review/scripts/gather.py +23 -0
- package/skills/one-yang-three-yin/SKILL.md +74 -0
- package/skills/one-yang-three-yin/scripts/gather.py +22 -0
- package/skills/shrink-pullback/SKILL.md +59 -0
- package/skills/shrink-pullback/scripts/gather.py +22 -0
- package/skills/stock-analysis/SKILL.md +134 -0
- package/skills/stock-analysis/scripts/gather.py +22 -0
- package/skills/stock-screener/SKILL.md +68 -0
- package/skills/stock-screener/scripts/gather.py +22 -0
- package/skills/strategy-backtest/SKILL.md +129 -0
- package/skills/strategy-backtest/scripts/gather.py +23 -0
- package/skills/volume-breakout/SKILL.md +58 -0
- package/skills/volume-breakout/scripts/gather.py +22 -0
- package/skills/wave-theory/SKILL.md +67 -0
- package/skills/wave-theory/scripts/gather.py +22 -0
- package/skills/wisburg-research/SKILL.md +119 -0
- package/strategies/examples/bottom_volume.yaml +47 -0
- package/strategies/examples/box_oscillation.yaml +41 -0
- package/strategies/examples/bull_trend.yaml +42 -0
- package/strategies/examples/dragon_head.yaml +45 -0
- package/strategies/examples/emotion_cycle.yaml +43 -0
- package/strategies/examples/ma_crossover.yaml +34 -0
- package/strategies/examples/rsi_oversold.yaml +35 -0
- package/strategies/examples/shrink_pullback.yaml +45 -0
- package/strategies/examples/volume_breakout.yaml +42 -0
- package/templates/_macros.j2 +90 -0
- package/templates/market_review/full.md.j2 +103 -0
- package/templates/stock_analysis/brief.md.j2 +28 -0
- package/templates/stock_analysis/full.md.j2 +108 -0
- package/tools/__init__.py +0 -0
- package/tools/alert_rules.py +243 -0
- package/tools/anomaly_detect.py +575 -0
- package/tools/backtest.py +764 -0
- package/tools/capabilities.py +108 -0
- package/tools/diagnostics.py +146 -0
- package/tools/gather.py +157 -0
- package/tools/import_parser.py +279 -0
- package/tools/market_regime.py +252 -0
- package/tools/market_review.py +155 -0
- package/tools/name_resolver.py +136 -0
- package/tools/pattern.py +515 -0
- package/tools/position_context.py +161 -0
- package/tools/report_renderer.py +116 -0
- package/tools/requirements.txt +15 -0
- package/tools/risk_screening.py +323 -0
- package/tools/screener.py +219 -0
- package/tools/search_intel.py +504 -0
- package/tools/stock_data.py +1721 -0
- package/tools/technical.py +642 -0
- package/tools/trading_calendar.py +226 -0
- package/tools/volume_analysis.py +178 -0
- package/tools/watchlist.py +66 -0
- package/tools/watchlist_context.py +185 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Market capability boundaries — tell agent what a given market supports.
|
|
3
|
+
|
|
4
|
+
Prevents agents from calling A-share-only tools on HK/US/JP/KR/TW and vice versa.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import argparse
|
|
8
|
+
import json
|
|
9
|
+
import sys
|
|
10
|
+
from datetime import datetime, timezone
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
14
|
+
from stock_data import detect_market
|
|
15
|
+
|
|
16
|
+
ALL_MARKETS = ["A", "HK", "US", "JP", "KR", "TW"]
|
|
17
|
+
|
|
18
|
+
# tool_name -> {markets: [supported markets], reason: str for unsupported}
|
|
19
|
+
TOOL_MATRIX = {
|
|
20
|
+
"get_kline": {"markets": ALL_MARKETS, "reason": None},
|
|
21
|
+
"get_quote": {"markets": ALL_MARKETS, "reason": None},
|
|
22
|
+
"get_news": {"markets": ALL_MARKETS, "reason": None},
|
|
23
|
+
"get_financials": {"markets": ALL_MARKETS, "reason": None},
|
|
24
|
+
"get_technical_analysis": {"markets": ALL_MARKETS, "reason": None},
|
|
25
|
+
"analyze_pattern": {"markets": ALL_MARKETS, "reason": None},
|
|
26
|
+
"get_market_indices": {"markets": ALL_MARKETS, "reason": None},
|
|
27
|
+
"calculate_ma": {"markets": ALL_MARKETS, "reason": None},
|
|
28
|
+
"get_volume_analysis": {"markets": ALL_MARKETS, "reason": None},
|
|
29
|
+
"detect_anomaly": {"markets": ALL_MARKETS, "reason": None},
|
|
30
|
+
"get_stock_info": {"markets": ALL_MARKETS, "reason": None},
|
|
31
|
+
"detect_market_regime": {"markets": ["A", "HK", "US"], "reason": "index coverage is CN/HK/US only"},
|
|
32
|
+
"get_market_review": {"markets": ["A", "HK", "US"], "reason": "review data coverage is CN/HK/US only"},
|
|
33
|
+
# A-share only
|
|
34
|
+
"get_capital_flow": {"markets": ["A"], "reason": "A-share only (akshare data)"},
|
|
35
|
+
"get_chip_distribution": {"markets": ["A"], "reason": "A-share only"},
|
|
36
|
+
"get_sector_rankings": {"markets": ["A"], "reason": "A-share only"},
|
|
37
|
+
"get_market_stats": {"markets": ["A"], "reason": "A-share only"},
|
|
38
|
+
"get_fundamental_context": {"markets": ["A"], "reason": "A-share only"},
|
|
39
|
+
"resolve_stock_name": {"markets": ["A"], "reason": "A-share name/pinyin index only"},
|
|
40
|
+
# US-leaning
|
|
41
|
+
"get_social_sentiment": {
|
|
42
|
+
"markets": ["US"],
|
|
43
|
+
"reason": "sentiment providers are US-centric; requires SENTIMENT_API_KEY",
|
|
44
|
+
},
|
|
45
|
+
"get_trending_sentiment": {
|
|
46
|
+
"markets": ALL_MARKETS,
|
|
47
|
+
"reason": None,
|
|
48
|
+
},
|
|
49
|
+
# search-based, market-agnostic
|
|
50
|
+
"search_stock_news": {"markets": ALL_MARKETS, "reason": None},
|
|
51
|
+
"search_comprehensive_intel": {"markets": ALL_MARKETS, "reason": None},
|
|
52
|
+
"extract_article": {"markets": ALL_MARKETS, "reason": None},
|
|
53
|
+
"screen_risk": {"markets": ALL_MARKETS, "reason": None},
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def get_capabilities(market: str) -> dict:
|
|
58
|
+
market = market.upper()
|
|
59
|
+
supported = []
|
|
60
|
+
unsupported = []
|
|
61
|
+
for tool, spec in TOOL_MATRIX.items():
|
|
62
|
+
if market in spec["markets"]:
|
|
63
|
+
supported.append(tool)
|
|
64
|
+
else:
|
|
65
|
+
unsupported.append({"tool": tool, "reason": spec["reason"] or "not supported"})
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
"meta": {
|
|
69
|
+
"provider": "capabilities",
|
|
70
|
+
"fetched_at": datetime.now(timezone.utc).isoformat(),
|
|
71
|
+
"freshness": "static",
|
|
72
|
+
"fallback_used": False,
|
|
73
|
+
"warnings": [],
|
|
74
|
+
},
|
|
75
|
+
"market": market,
|
|
76
|
+
"supported": supported,
|
|
77
|
+
"unsupported": unsupported,
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def main():
|
|
82
|
+
parser = argparse.ArgumentParser(description="Market capability boundaries")
|
|
83
|
+
sub = parser.add_subparsers(dest="command")
|
|
84
|
+
p = sub.add_parser("get")
|
|
85
|
+
grp = p.add_mutually_exclusive_group(required=True)
|
|
86
|
+
grp.add_argument("--market", type=str.upper, choices=ALL_MARKETS, help="Market code")
|
|
87
|
+
grp.add_argument("--symbol", help="Stock symbol (auto-detects market)")
|
|
88
|
+
args = parser.parse_args()
|
|
89
|
+
|
|
90
|
+
if not args.command:
|
|
91
|
+
parser.print_help()
|
|
92
|
+
sys.exit(1)
|
|
93
|
+
|
|
94
|
+
market = args.market or detect_market(args.symbol)
|
|
95
|
+
result = get_capabilities(market)
|
|
96
|
+
if args.symbol:
|
|
97
|
+
result["symbol"] = args.symbol
|
|
98
|
+
json.dump(result, sys.stdout, ensure_ascii=False, indent=2, default=str)
|
|
99
|
+
print()
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
if __name__ == "__main__":
|
|
103
|
+
# Windows defaults stdio to a legacy code page (cp1252) that cannot encode the
|
|
104
|
+
# Chinese text these tools emit — force UTF-8 so stdout never crashes there.
|
|
105
|
+
for _s in (sys.stdout, sys.stderr):
|
|
106
|
+
if hasattr(_s, "reconfigure"):
|
|
107
|
+
_s.reconfigure(encoding="utf-8")
|
|
108
|
+
main()
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Data source diagnostics — report which providers are usable per market.
|
|
3
|
+
|
|
4
|
+
Checks: (1) python package importable, (2) required env vars present.
|
|
5
|
+
Does NOT hit the network — this is a cheap capability probe, not a health check.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import argparse
|
|
9
|
+
import importlib
|
|
10
|
+
import importlib.util
|
|
11
|
+
import json
|
|
12
|
+
import os
|
|
13
|
+
import sys
|
|
14
|
+
from datetime import datetime, timezone
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
|
|
17
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
18
|
+
from capabilities import ALL_MARKETS # noqa: E402
|
|
19
|
+
|
|
20
|
+
# provider -> (import_name, required_env_vars, markets, note)
|
|
21
|
+
PROVIDERS = {
|
|
22
|
+
"akshare": ("akshare", [], ["A"], None),
|
|
23
|
+
"tushare": ("tushare", ["TUSHARE_TOKEN"], ["A"], None),
|
|
24
|
+
"efinance": ("efinance", [], ["A"], None),
|
|
25
|
+
"pytdx": ("pytdx", [], ["A"], "connects to public Tencent servers"),
|
|
26
|
+
"yfinance": ("yfinance", [], ["HK", "US", "JP", "KR", "TW"], "quote may be delayed 15-20min"),
|
|
27
|
+
"finnhub": ("finnhub", ["FINNHUB_API_KEY"], ["HK", "US"], None),
|
|
28
|
+
"longbridge": (
|
|
29
|
+
"longport.openapi",
|
|
30
|
+
["LONGBRIDGE_APP_KEY", "LONGBRIDGE_APP_SECRET", "LONGBRIDGE_ACCESS_TOKEN"],
|
|
31
|
+
["HK", "US"],
|
|
32
|
+
None,
|
|
33
|
+
),
|
|
34
|
+
"alphavantage": ("requests", ["ALPHAVANTAGE_API_KEY"], ["US"], None),
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _check_provider(name: str) -> dict:
|
|
39
|
+
import_name, env_vars, markets, note = PROVIDERS[name]
|
|
40
|
+
|
|
41
|
+
# find_spec avoids executing the package's top-level code — cheaper and
|
|
42
|
+
# side-effect-free vs import_module. This is a capability probe, not a call.
|
|
43
|
+
try:
|
|
44
|
+
spec = importlib.util.find_spec(import_name)
|
|
45
|
+
pkg_ok = spec is not None
|
|
46
|
+
pkg_err = None if pkg_ok else f"package '{import_name}' not installed"
|
|
47
|
+
except Exception as e:
|
|
48
|
+
pkg_ok = False
|
|
49
|
+
pkg_err = f"package '{import_name}' not installed: {e}"
|
|
50
|
+
|
|
51
|
+
missing_env = [v for v in env_vars if not os.environ.get(v)]
|
|
52
|
+
|
|
53
|
+
available = pkg_ok and not missing_env
|
|
54
|
+
reasons = []
|
|
55
|
+
if not pkg_ok:
|
|
56
|
+
reasons.append(pkg_err)
|
|
57
|
+
if missing_env:
|
|
58
|
+
reasons.append(f"missing env: {', '.join(missing_env)}")
|
|
59
|
+
|
|
60
|
+
entry = {
|
|
61
|
+
"name": name,
|
|
62
|
+
"available": available,
|
|
63
|
+
"markets": markets,
|
|
64
|
+
"reason": "; ".join(reasons) if reasons else None,
|
|
65
|
+
}
|
|
66
|
+
if note:
|
|
67
|
+
entry["note"] = note
|
|
68
|
+
return entry
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def diagnose(market: str = "all") -> dict:
|
|
72
|
+
market = (market or "all").upper()
|
|
73
|
+
all_providers = [_check_provider(p) for p in PROVIDERS]
|
|
74
|
+
|
|
75
|
+
markets_to_report = ALL_MARKETS if market == "ALL" else [market]
|
|
76
|
+
|
|
77
|
+
result = {
|
|
78
|
+
"meta": {
|
|
79
|
+
"provider": "diagnostics",
|
|
80
|
+
"fetched_at": datetime.now(timezone.utc).isoformat(),
|
|
81
|
+
"freshness": "instant",
|
|
82
|
+
"fallback_used": False,
|
|
83
|
+
"warnings": [],
|
|
84
|
+
},
|
|
85
|
+
"markets": [],
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
for m in markets_to_report:
|
|
89
|
+
providers = [p for p in all_providers if m in p["markets"]]
|
|
90
|
+
available = [p for p in providers if p["available"]]
|
|
91
|
+
|
|
92
|
+
warnings = []
|
|
93
|
+
if not available:
|
|
94
|
+
warnings.append(f"no working data source for {m}")
|
|
95
|
+
# market-specific caveats
|
|
96
|
+
if m == "A" and not any(p["name"] == "tushare" and p["available"] for p in providers):
|
|
97
|
+
warnings.append("TUSHARE_TOKEN not set — akshare-only, some deep data unavailable")
|
|
98
|
+
if (
|
|
99
|
+
m in ("HK", "US")
|
|
100
|
+
and not any(p["name"] == "longbridge" and p["available"] for p in providers)
|
|
101
|
+
and not any(p["name"] == "finnhub" and p["available"] for p in providers)
|
|
102
|
+
):
|
|
103
|
+
warnings.append(f"{m} quotes will be delayed (yfinance only)")
|
|
104
|
+
if m in ("JP", "KR", "TW"):
|
|
105
|
+
warnings.append(f"{m} is served by yfinance only — quotes delayed 15-20min, no failover")
|
|
106
|
+
|
|
107
|
+
result["markets"].append(
|
|
108
|
+
{
|
|
109
|
+
"market": m,
|
|
110
|
+
"available": bool(available),
|
|
111
|
+
"providers": providers,
|
|
112
|
+
"warnings": warnings,
|
|
113
|
+
}
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
return result
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def main():
|
|
120
|
+
parser = argparse.ArgumentParser(description="Data source diagnostics")
|
|
121
|
+
sub = parser.add_subparsers(dest="command")
|
|
122
|
+
p = sub.add_parser("check")
|
|
123
|
+
p.add_argument(
|
|
124
|
+
"--market",
|
|
125
|
+
default="ALL",
|
|
126
|
+
type=str.upper,
|
|
127
|
+
choices=ALL_MARKETS + ["ALL"],
|
|
128
|
+
help="Market to diagnose (default: ALL)",
|
|
129
|
+
)
|
|
130
|
+
args = parser.parse_args()
|
|
131
|
+
if not args.command:
|
|
132
|
+
parser.print_help()
|
|
133
|
+
sys.exit(1)
|
|
134
|
+
|
|
135
|
+
result = diagnose(args.market)
|
|
136
|
+
json.dump(result, sys.stdout, ensure_ascii=False, indent=2, default=str)
|
|
137
|
+
print()
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
if __name__ == "__main__":
|
|
141
|
+
# Windows defaults stdio to a legacy code page (cp1252) that cannot encode the
|
|
142
|
+
# Chinese text these tools emit — force UTF-8 so stdout never crashes there.
|
|
143
|
+
for _s in (sys.stdout, sys.stderr):
|
|
144
|
+
if hasattr(_s, "reconfigure"):
|
|
145
|
+
_s.reconfigure(encoding="utf-8")
|
|
146
|
+
main()
|
package/tools/gather.py
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Unified data gathering — parallel subprocess calls to tools for skill scripts."""
|
|
3
|
+
|
|
4
|
+
import argparse
|
|
5
|
+
import json
|
|
6
|
+
import subprocess
|
|
7
|
+
import sys
|
|
8
|
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
TOOLS_DIR = Path(__file__).resolve().parent
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _find_python() -> str:
|
|
15
|
+
venv = TOOLS_DIR.parent / ".venv" / "bin" / "python3"
|
|
16
|
+
if venv.exists():
|
|
17
|
+
return str(venv)
|
|
18
|
+
return sys.executable
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _run(script: str, args: list[str], timeout: int = 60) -> str | None:
|
|
22
|
+
python = _find_python()
|
|
23
|
+
cmd = [python, str(TOOLS_DIR / script)] + args
|
|
24
|
+
try:
|
|
25
|
+
r = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8", timeout=timeout)
|
|
26
|
+
if r.returncode == 0 and r.stdout.strip():
|
|
27
|
+
return r.stdout.strip()
|
|
28
|
+
except subprocess.TimeoutExpired:
|
|
29
|
+
pass
|
|
30
|
+
except Exception:
|
|
31
|
+
pass
|
|
32
|
+
return None
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _parse_json(raw: str | None):
|
|
36
|
+
if raw is None:
|
|
37
|
+
return None
|
|
38
|
+
try:
|
|
39
|
+
return json.loads(raw)
|
|
40
|
+
except json.JSONDecodeError:
|
|
41
|
+
return raw
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def gather_analysis(symbol: str) -> dict:
|
|
45
|
+
tasks = {
|
|
46
|
+
"quote": ("stock_data.py", ["quote", symbol]),
|
|
47
|
+
"kline": ("stock_data.py", ["kline", symbol, "--period", "daily", "--count", "120"]),
|
|
48
|
+
"technical": ("technical.py", ["analyze", symbol, "--period", "daily", "--count", "120"]),
|
|
49
|
+
"financials": ("stock_data.py", ["financials", symbol]),
|
|
50
|
+
"capital_flow": ("stock_data.py", ["capital_flow", symbol]),
|
|
51
|
+
"news": ("search_intel.py", ["search", f"{symbol} stock news"]),
|
|
52
|
+
"risk": ("risk_screening.py", ["screen", symbol]),
|
|
53
|
+
"regime": ("market_regime.py", ["detect"]),
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
results = {}
|
|
57
|
+
with ThreadPoolExecutor(max_workers=8) as pool:
|
|
58
|
+
futures = {pool.submit(_run, script, args): key for key, (script, args) in tasks.items()}
|
|
59
|
+
for future in as_completed(futures):
|
|
60
|
+
key = futures[future]
|
|
61
|
+
results[key] = _parse_json(future.result())
|
|
62
|
+
|
|
63
|
+
return results
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def gather_technical(symbol: str, kline_count: int = 120, with_quote: bool = False) -> dict:
|
|
67
|
+
tasks = {
|
|
68
|
+
"kline": ("stock_data.py", ["kline", symbol, "--period", "daily", "--count", str(kline_count)]),
|
|
69
|
+
"technical": ("technical.py", ["analyze", symbol, "--period", "daily", "--count", str(kline_count)]),
|
|
70
|
+
}
|
|
71
|
+
if with_quote:
|
|
72
|
+
tasks["quote"] = ("stock_data.py", ["quote", symbol])
|
|
73
|
+
|
|
74
|
+
results = {}
|
|
75
|
+
with ThreadPoolExecutor(max_workers=4) as pool:
|
|
76
|
+
futures = {pool.submit(_run, script, args): key for key, (script, args) in tasks.items()}
|
|
77
|
+
for future in as_completed(futures):
|
|
78
|
+
key = futures[future]
|
|
79
|
+
results[key] = _parse_json(future.result())
|
|
80
|
+
|
|
81
|
+
return results
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def gather_screen(market: str = "A", top: int = 20, config: str | None = None) -> dict:
|
|
85
|
+
args = ["screen", "--market", market, "--top", str(top)]
|
|
86
|
+
if config:
|
|
87
|
+
args += ["--config", config]
|
|
88
|
+
raw = _run("screener.py", args, timeout=120)
|
|
89
|
+
return {"screen_results": _parse_json(raw)}
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def gather_fundamental(symbol: str) -> dict:
|
|
93
|
+
tasks = {
|
|
94
|
+
"quote": ("stock_data.py", ["quote", symbol]),
|
|
95
|
+
"kline": ("stock_data.py", ["kline", symbol, "--period", "daily", "--count", "60"]),
|
|
96
|
+
"technical": ("technical.py", ["analyze", symbol, "--period", "daily", "--count", "60"]),
|
|
97
|
+
"financials": ("stock_data.py", ["financials", symbol]),
|
|
98
|
+
"news": ("search_intel.py", ["search", f"{symbol} stock news"]),
|
|
99
|
+
"stock_info": ("stock_data.py", ["stock_info", symbol]),
|
|
100
|
+
"sector_rankings": ("stock_data.py", ["sector_rankings", "--top", "5", "--direction", "both"]),
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
results = {}
|
|
104
|
+
with ThreadPoolExecutor(max_workers=7) as pool:
|
|
105
|
+
futures = {pool.submit(_run, script, args): key for key, (script, args) in tasks.items()}
|
|
106
|
+
for future in as_completed(futures):
|
|
107
|
+
key = futures[future]
|
|
108
|
+
results[key] = _parse_json(future.result())
|
|
109
|
+
|
|
110
|
+
return results
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def main():
|
|
114
|
+
parser = argparse.ArgumentParser(description="Unified data gathering for skills")
|
|
115
|
+
sub = parser.add_subparsers(dest="command")
|
|
116
|
+
|
|
117
|
+
p_analysis = sub.add_parser("analysis")
|
|
118
|
+
p_analysis.add_argument("symbol")
|
|
119
|
+
|
|
120
|
+
p_technical = sub.add_parser("technical")
|
|
121
|
+
p_technical.add_argument("symbol")
|
|
122
|
+
p_technical.add_argument("--kline-count", type=int, default=120)
|
|
123
|
+
p_technical.add_argument("--with-quote", action="store_true")
|
|
124
|
+
|
|
125
|
+
p_screen = sub.add_parser("screen")
|
|
126
|
+
p_screen.add_argument("--market", default="A")
|
|
127
|
+
p_screen.add_argument("--top", type=int, default=20)
|
|
128
|
+
p_screen.add_argument("--config")
|
|
129
|
+
|
|
130
|
+
p_fundamental = sub.add_parser("fundamental")
|
|
131
|
+
p_fundamental.add_argument("symbol")
|
|
132
|
+
|
|
133
|
+
args = parser.parse_args()
|
|
134
|
+
|
|
135
|
+
if args.command == "analysis":
|
|
136
|
+
result = gather_analysis(args.symbol)
|
|
137
|
+
elif args.command == "technical":
|
|
138
|
+
result = gather_technical(args.symbol, args.kline_count, args.with_quote)
|
|
139
|
+
elif args.command == "screen":
|
|
140
|
+
result = gather_screen(args.market, args.top, args.config)
|
|
141
|
+
elif args.command == "fundamental":
|
|
142
|
+
result = gather_fundamental(args.symbol)
|
|
143
|
+
else:
|
|
144
|
+
parser.print_help()
|
|
145
|
+
sys.exit(1)
|
|
146
|
+
|
|
147
|
+
json.dump(result, sys.stdout, ensure_ascii=False, indent=2)
|
|
148
|
+
print()
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
if __name__ == "__main__":
|
|
152
|
+
# Windows defaults stdio to a legacy code page (cp1252) that cannot encode the
|
|
153
|
+
# Chinese text these tools emit — force UTF-8 so stdout never crashes there.
|
|
154
|
+
for _s in (sys.stdout, sys.stderr):
|
|
155
|
+
if hasattr(_s, "reconfigure"):
|
|
156
|
+
_s.reconfigure(encoding="utf-8")
|
|
157
|
+
main()
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Parse stock list from freeform text / CSV / markdown.
|
|
3
|
+
|
|
4
|
+
Extracts symbols using regex heuristics + name_resolver for A-share Chinese names.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import argparse
|
|
8
|
+
import json
|
|
9
|
+
import re
|
|
10
|
+
import sys
|
|
11
|
+
from datetime import datetime, timezone
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
15
|
+
from name_resolver import resolve # noqa: E402
|
|
16
|
+
from stock_data import detect_market # noqa: E402
|
|
17
|
+
|
|
18
|
+
# regex patterns
|
|
19
|
+
RE_A_CODE = re.compile(r"(?<!\d)(\d{6})(?!\d)")
|
|
20
|
+
RE_HK_CODE = re.compile(r"(?<![A-Za-z0-9])(\d{4,5}\.HK)(?![A-Za-z0-9])", re.IGNORECASE)
|
|
21
|
+
RE_ASIA_SUFFIX_CODE = re.compile(
|
|
22
|
+
r"(?<![A-Za-z0-9])(\d{4}\.T|\d{6}\.(?:KS|KQ)|\d{4}\.(?:TW|TWO))(?![A-Za-z0-9])", re.IGNORECASE
|
|
23
|
+
)
|
|
24
|
+
RE_US_TICKER = re.compile(r"(?<![A-Za-z0-9])([A-Z]{1,5})(?![A-Za-z0-9\.])")
|
|
25
|
+
RE_CN_NAME = re.compile(r"[一-鿿][一-鿿\w]{1,7}")
|
|
26
|
+
|
|
27
|
+
# US ticker false-positive filter (common ALL-CAPS words that aren't tickers)
|
|
28
|
+
US_STOPWORDS = {
|
|
29
|
+
"A",
|
|
30
|
+
"I",
|
|
31
|
+
"AM",
|
|
32
|
+
"PM",
|
|
33
|
+
"AN",
|
|
34
|
+
"AS",
|
|
35
|
+
"AT",
|
|
36
|
+
"BE",
|
|
37
|
+
"BY",
|
|
38
|
+
"DO",
|
|
39
|
+
"GO",
|
|
40
|
+
"IF",
|
|
41
|
+
"IN",
|
|
42
|
+
"IS",
|
|
43
|
+
"IT",
|
|
44
|
+
"ME",
|
|
45
|
+
"MY",
|
|
46
|
+
"NO",
|
|
47
|
+
"OF",
|
|
48
|
+
"ON",
|
|
49
|
+
"OR",
|
|
50
|
+
"SO",
|
|
51
|
+
"TO",
|
|
52
|
+
"UP",
|
|
53
|
+
"US",
|
|
54
|
+
"WE",
|
|
55
|
+
"THE",
|
|
56
|
+
"AND",
|
|
57
|
+
"FOR",
|
|
58
|
+
"BUT",
|
|
59
|
+
"NOT",
|
|
60
|
+
"YOU",
|
|
61
|
+
"ALL",
|
|
62
|
+
"CAN",
|
|
63
|
+
"HAD",
|
|
64
|
+
"HAS",
|
|
65
|
+
"HER",
|
|
66
|
+
"WAS",
|
|
67
|
+
"ONE",
|
|
68
|
+
"OUR",
|
|
69
|
+
"OUT",
|
|
70
|
+
"DAY",
|
|
71
|
+
"GET",
|
|
72
|
+
"HIM",
|
|
73
|
+
"HIS",
|
|
74
|
+
"HOW",
|
|
75
|
+
"MAN",
|
|
76
|
+
"NEW",
|
|
77
|
+
"NOW",
|
|
78
|
+
"OLD",
|
|
79
|
+
"SEE",
|
|
80
|
+
"TWO",
|
|
81
|
+
"WAY",
|
|
82
|
+
"WHO",
|
|
83
|
+
"BOY",
|
|
84
|
+
"DID",
|
|
85
|
+
"ITS",
|
|
86
|
+
"LET",
|
|
87
|
+
"PUT",
|
|
88
|
+
"SAY",
|
|
89
|
+
"SHE",
|
|
90
|
+
"TOO",
|
|
91
|
+
"USE",
|
|
92
|
+
"PE",
|
|
93
|
+
"PB",
|
|
94
|
+
"PS",
|
|
95
|
+
"ROE",
|
|
96
|
+
"MA",
|
|
97
|
+
"RSI",
|
|
98
|
+
"MACD",
|
|
99
|
+
"KDJ",
|
|
100
|
+
"BOLL",
|
|
101
|
+
"ETF",
|
|
102
|
+
"IPO",
|
|
103
|
+
"AI",
|
|
104
|
+
"ML",
|
|
105
|
+
"GDP",
|
|
106
|
+
"CPI",
|
|
107
|
+
"PPI",
|
|
108
|
+
"CEO",
|
|
109
|
+
"CFO",
|
|
110
|
+
"CTO",
|
|
111
|
+
"COO",
|
|
112
|
+
"USA",
|
|
113
|
+
"PDF",
|
|
114
|
+
"CSV",
|
|
115
|
+
"API",
|
|
116
|
+
"URL",
|
|
117
|
+
"HTTP",
|
|
118
|
+
"SQL",
|
|
119
|
+
"JSON",
|
|
120
|
+
"XML",
|
|
121
|
+
"HTML",
|
|
122
|
+
"CSS",
|
|
123
|
+
"OK",
|
|
124
|
+
"TV",
|
|
125
|
+
"PC",
|
|
126
|
+
"OS",
|
|
127
|
+
"USD",
|
|
128
|
+
"CNY",
|
|
129
|
+
"HKD",
|
|
130
|
+
"EUR",
|
|
131
|
+
"JPY",
|
|
132
|
+
"GBP",
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def _dedupe(items: list[dict]) -> list[dict]:
|
|
137
|
+
seen = set()
|
|
138
|
+
result = []
|
|
139
|
+
for it in items:
|
|
140
|
+
key = it.get("symbol")
|
|
141
|
+
if key and key not in seen:
|
|
142
|
+
seen.add(key)
|
|
143
|
+
result.append(it)
|
|
144
|
+
return result
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def parse(text: str) -> dict:
|
|
148
|
+
text = text.strip()
|
|
149
|
+
items: list[dict] = []
|
|
150
|
+
unresolved: list[str] = []
|
|
151
|
+
seen_symbols: set[str] = set()
|
|
152
|
+
|
|
153
|
+
# 1. HK codes (do first — they contain digits that A-share regex would also match)
|
|
154
|
+
hk_matches: list[str] = []
|
|
155
|
+
for m in RE_HK_CODE.finditer(text):
|
|
156
|
+
code = m.group(1).upper()
|
|
157
|
+
# normalize to 5-digit
|
|
158
|
+
num, suf = code.split(".")
|
|
159
|
+
code = f"{num.zfill(5)}.HK"
|
|
160
|
+
if code not in seen_symbols:
|
|
161
|
+
items.append({"symbol": code, "market": "HK"})
|
|
162
|
+
seen_symbols.add(code)
|
|
163
|
+
hk_matches.append(m.group(1))
|
|
164
|
+
|
|
165
|
+
# 1b. JP/KR/TW suffixed codes (7203.T, 005930.KS, 2330.TW — digits would hit A-share regex too)
|
|
166
|
+
asia_matches: list[str] = []
|
|
167
|
+
for m in RE_ASIA_SUFFIX_CODE.finditer(text):
|
|
168
|
+
code = m.group(1).upper()
|
|
169
|
+
if code not in seen_symbols:
|
|
170
|
+
items.append({"symbol": code, "market": detect_market(code)})
|
|
171
|
+
seen_symbols.add(code)
|
|
172
|
+
asia_matches.append(m.group(1))
|
|
173
|
+
|
|
174
|
+
# strip suffixed codes so their digit part doesn't get picked up as A-share
|
|
175
|
+
# and their suffix doesn't get picked up as a US ticker
|
|
176
|
+
text_without_suffixed = text
|
|
177
|
+
for m in hk_matches + asia_matches:
|
|
178
|
+
text_without_suffixed = text_without_suffixed.replace(m, " ")
|
|
179
|
+
|
|
180
|
+
# 2. A-share 6-digit codes
|
|
181
|
+
for m in RE_A_CODE.finditer(text_without_suffixed):
|
|
182
|
+
code = m.group(1)
|
|
183
|
+
if code not in seen_symbols:
|
|
184
|
+
items.append({"symbol": code, "market": "A"})
|
|
185
|
+
seen_symbols.add(code)
|
|
186
|
+
|
|
187
|
+
# 3. US tickers (1-5 uppercase letters), filtered against stopwords
|
|
188
|
+
# runs on stripped text so HK/JP/KR/TW suffixes (HK, TW, KS...) aren't misread as tickers
|
|
189
|
+
for m in RE_US_TICKER.finditer(text_without_suffixed):
|
|
190
|
+
tick = m.group(1)
|
|
191
|
+
if tick in US_STOPWORDS:
|
|
192
|
+
continue
|
|
193
|
+
if len(tick) == 1:
|
|
194
|
+
continue # too noisy
|
|
195
|
+
if tick not in seen_symbols:
|
|
196
|
+
items.append({"symbol": tick, "market": "US"})
|
|
197
|
+
seen_symbols.add(tick)
|
|
198
|
+
|
|
199
|
+
# 4. Chinese names → resolve via name_resolver
|
|
200
|
+
cn_names = set(RE_CN_NAME.findall(text))
|
|
201
|
+
# filter obviously non-stock words
|
|
202
|
+
noise = {"成本", "持仓", "买入", "卖出", "止损", "止盈", "股票", "代码", "行情", "分析", "策略"}
|
|
203
|
+
cn_names = {n for n in cn_names if n not in noise and len(n) >= 2}
|
|
204
|
+
|
|
205
|
+
for name in cn_names:
|
|
206
|
+
try:
|
|
207
|
+
hits = resolve(name, top=1)
|
|
208
|
+
except Exception:
|
|
209
|
+
hits = []
|
|
210
|
+
if hits and isinstance(hits, list) and hits[0].get("code"):
|
|
211
|
+
top_hit = hits[0]
|
|
212
|
+
# ponytail: name_resolver returns nearest fuzzy match even for names not in A-share
|
|
213
|
+
# (e.g. "腾讯" → "拓新药业" via pinyin). Reject low-confidence hits.
|
|
214
|
+
score = top_hit.get("score")
|
|
215
|
+
hit_name = top_hit.get("name", "")
|
|
216
|
+
is_exact = (name in hit_name) or (hit_name in name)
|
|
217
|
+
if not is_exact and score is not None and score < 0.85:
|
|
218
|
+
unresolved.append(name)
|
|
219
|
+
continue
|
|
220
|
+
code = top_hit["code"]
|
|
221
|
+
if code not in seen_symbols:
|
|
222
|
+
items.append(
|
|
223
|
+
{
|
|
224
|
+
"symbol": code,
|
|
225
|
+
"market": "A",
|
|
226
|
+
"name": hit_name,
|
|
227
|
+
"resolved_from": name,
|
|
228
|
+
}
|
|
229
|
+
)
|
|
230
|
+
seen_symbols.add(code)
|
|
231
|
+
else:
|
|
232
|
+
unresolved.append(name)
|
|
233
|
+
|
|
234
|
+
return {
|
|
235
|
+
"meta": {
|
|
236
|
+
"provider": "import_parser",
|
|
237
|
+
"fetched_at": datetime.now(timezone.utc).isoformat(),
|
|
238
|
+
"freshness": "instant",
|
|
239
|
+
"fallback_used": False,
|
|
240
|
+
"warnings": [],
|
|
241
|
+
},
|
|
242
|
+
"items": _dedupe(items),
|
|
243
|
+
"unresolved": unresolved,
|
|
244
|
+
"count": len(items),
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def main():
|
|
249
|
+
parser = argparse.ArgumentParser(description="Parse stock list from text")
|
|
250
|
+
sub = parser.add_subparsers(dest="command")
|
|
251
|
+
p = sub.add_parser("parse")
|
|
252
|
+
p.add_argument("--text", help='Text to parse (or "-" for stdin)')
|
|
253
|
+
p.add_argument("--text-b64", help="Base64-encoded text")
|
|
254
|
+
|
|
255
|
+
args = parser.parse_args()
|
|
256
|
+
if not args.command:
|
|
257
|
+
parser.print_help()
|
|
258
|
+
sys.exit(1)
|
|
259
|
+
|
|
260
|
+
if args.text_b64:
|
|
261
|
+
import base64
|
|
262
|
+
|
|
263
|
+
text = base64.b64decode(args.text_b64).decode("utf-8")
|
|
264
|
+
elif not args.text or args.text == "-":
|
|
265
|
+
text = sys.stdin.read()
|
|
266
|
+
else:
|
|
267
|
+
text = args.text
|
|
268
|
+
result = parse(text)
|
|
269
|
+
json.dump(result, sys.stdout, ensure_ascii=False, indent=2, default=str)
|
|
270
|
+
print()
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
if __name__ == "__main__":
|
|
274
|
+
# Windows defaults stdio to a legacy code page (cp1252) that cannot encode the
|
|
275
|
+
# Chinese text these tools emit — force UTF-8 so stdout never crashes there.
|
|
276
|
+
for _s in (sys.stdout, sys.stderr):
|
|
277
|
+
if hasattr(_s, "reconfigure"):
|
|
278
|
+
_s.reconfigure(encoding="utf-8")
|
|
279
|
+
main()
|