@weaxs/openclaw-stock-analysis 0.1.3 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/index.js +5275 -0
- package/index.ts +217 -12
- package/openclaw.plugin.json +10 -2
- package/package.json +7 -3
- package/schemas/market_review_schema.json +176 -0
- package/scripts/sync-version.sh +55 -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/alert_rules.py +238 -0
- package/tools/backtest.py +115 -0
- package/tools/capabilities.py +103 -0
- package/tools/diagnostics.py +141 -0
- package/tools/import_parser.py +274 -0
- package/tools/position_context.py +156 -0
- package/tools/report_renderer.py +111 -0
- package/tools/requirements.txt +2 -0
- package/tools/search_intel.py +45 -1
- package/tools/stock_data.py +105 -39
- package/tools/trading_calendar.py +27 -4
- package/tools/watchlist_context.py +180 -0
|
@@ -0,0 +1,103 @@
|
|
|
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
|
+
main()
|
|
@@ -0,0 +1,141 @@
|
|
|
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
|
+
main()
|
|
@@ -0,0 +1,274 @@
|
|
|
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
|
+
main()
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Position context analyzer — pnl, distance to stop-loss/take-profit, position advice.
|
|
3
|
+
|
|
4
|
+
Not a portfolio system. Stateless: user passes cost/quantity/stops each call.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import argparse
|
|
8
|
+
import json
|
|
9
|
+
import subprocess
|
|
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 stock_data import detect_market # noqa: E402
|
|
16
|
+
|
|
17
|
+
TOOLS_DIR = Path(__file__).resolve().parent
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _find_python() -> str:
|
|
21
|
+
venv = TOOLS_DIR.parent / ".venv" / "bin" / "python3"
|
|
22
|
+
return str(venv) if venv.exists() else sys.executable
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _run_json(script: str, args: list[str], timeout: int = 30):
|
|
26
|
+
cmd = [_find_python(), str(TOOLS_DIR / script)] + args
|
|
27
|
+
try:
|
|
28
|
+
r = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout)
|
|
29
|
+
if r.returncode == 0 and r.stdout.strip():
|
|
30
|
+
return json.loads(r.stdout)
|
|
31
|
+
except Exception:
|
|
32
|
+
pass
|
|
33
|
+
return None
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _risk_level(distance_to_stop_pct: float, pnl_pct: float, trend: str) -> str:
|
|
37
|
+
# ponytail: simple 3-bucket heuristic, upgrade to volatility-adjusted if needed
|
|
38
|
+
if distance_to_stop_pct is not None and distance_to_stop_pct < 3:
|
|
39
|
+
return "high"
|
|
40
|
+
if pnl_pct < -5 or trend == "bearish":
|
|
41
|
+
return "high"
|
|
42
|
+
if pnl_pct < 0 or (distance_to_stop_pct is not None and distance_to_stop_pct < 6):
|
|
43
|
+
return "medium"
|
|
44
|
+
return "low"
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _advice(pnl_pct: float, distance_to_stop_pct, distance_to_take_pct, trend: str, stop_loss, cost) -> str:
|
|
48
|
+
if distance_to_stop_pct is not None and distance_to_stop_pct < 3:
|
|
49
|
+
return "接近止损,建议减仓或严守止损位"
|
|
50
|
+
if pnl_pct > 15 and distance_to_take_pct is not None and distance_to_take_pct < 5:
|
|
51
|
+
return "接近目标价,考虑分批止盈"
|
|
52
|
+
if pnl_pct > 8 and stop_loss is not None and stop_loss < cost:
|
|
53
|
+
return "已有盈利,建议将止损上移到成本价或以上"
|
|
54
|
+
if pnl_pct < -5 and trend == "bearish":
|
|
55
|
+
return "浮亏且趋势偏空,控制仓位避免加仓"
|
|
56
|
+
if trend == "bullish" and pnl_pct >= 0:
|
|
57
|
+
return "趋势配合,可持有观察后续量能"
|
|
58
|
+
return "维持现有策略,关注关键位变化"
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def analyze_position(
|
|
62
|
+
symbol: str,
|
|
63
|
+
cost: float,
|
|
64
|
+
quantity: float,
|
|
65
|
+
stop_loss: float | None = None,
|
|
66
|
+
take_profit: float | None = None,
|
|
67
|
+
) -> dict:
|
|
68
|
+
quote = _run_json("stock_data.py", ["quote", symbol]) or {}
|
|
69
|
+
tech = _run_json("technical.py", ["analyze", symbol, "--period", "daily", "--count", "60"]) or {}
|
|
70
|
+
|
|
71
|
+
price = quote.get("price")
|
|
72
|
+
if price is None:
|
|
73
|
+
return {"symbol": symbol, "error": "cannot fetch current price"}
|
|
74
|
+
price = float(price)
|
|
75
|
+
|
|
76
|
+
pnl_per_share = price - cost
|
|
77
|
+
pnl_pct = (pnl_per_share / cost * 100) if cost else 0.0
|
|
78
|
+
market_value = price * quantity
|
|
79
|
+
total_pnl = pnl_per_share * quantity
|
|
80
|
+
|
|
81
|
+
distance_to_stop_pct = None
|
|
82
|
+
if stop_loss is not None:
|
|
83
|
+
distance_to_stop_pct = (price - stop_loss) / price * 100 if price else None
|
|
84
|
+
|
|
85
|
+
distance_to_take_pct = None
|
|
86
|
+
if take_profit is not None:
|
|
87
|
+
distance_to_take_pct = (take_profit - price) / price * 100 if price else None
|
|
88
|
+
|
|
89
|
+
trend = (tech.get("trend") or {}).get("overall", "unknown")
|
|
90
|
+
support = (tech.get("support_resistance") or {}).get("support")
|
|
91
|
+
resistance = (tech.get("support_resistance") or {}).get("resistance")
|
|
92
|
+
|
|
93
|
+
risk_level = _risk_level(distance_to_stop_pct, pnl_pct, trend)
|
|
94
|
+
advice = _advice(pnl_pct, distance_to_stop_pct, distance_to_take_pct, trend, stop_loss, cost)
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
"meta": {
|
|
98
|
+
"provider": "position_context",
|
|
99
|
+
"fetched_at": datetime.now(timezone.utc).isoformat(),
|
|
100
|
+
"freshness": "realtime_or_latest_trade_day",
|
|
101
|
+
"fallback_used": False,
|
|
102
|
+
"warnings": [],
|
|
103
|
+
},
|
|
104
|
+
"symbol": symbol,
|
|
105
|
+
"market": detect_market(symbol),
|
|
106
|
+
"name": quote.get("name"),
|
|
107
|
+
"position": {
|
|
108
|
+
"cost": cost,
|
|
109
|
+
"quantity": quantity,
|
|
110
|
+
"current_price": price,
|
|
111
|
+
"market_value": round(market_value, 2),
|
|
112
|
+
"total_pnl": round(total_pnl, 2),
|
|
113
|
+
"pnl_pct": round(pnl_pct, 2),
|
|
114
|
+
},
|
|
115
|
+
"levels": {
|
|
116
|
+
"stop_loss": stop_loss,
|
|
117
|
+
"take_profit": take_profit,
|
|
118
|
+
"support": support,
|
|
119
|
+
"resistance": resistance,
|
|
120
|
+
"distance_to_stop_loss_pct": round(distance_to_stop_pct, 2) if distance_to_stop_pct is not None else None,
|
|
121
|
+
"distance_to_take_profit_pct": round(distance_to_take_pct, 2) if distance_to_take_pct is not None else None,
|
|
122
|
+
},
|
|
123
|
+
"trend": trend,
|
|
124
|
+
"risk_level": risk_level,
|
|
125
|
+
"position_advice": advice,
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def main():
|
|
130
|
+
parser = argparse.ArgumentParser(description="Position context analyzer")
|
|
131
|
+
sub = parser.add_subparsers(dest="command")
|
|
132
|
+
p = sub.add_parser("analyze")
|
|
133
|
+
p.add_argument("symbol")
|
|
134
|
+
p.add_argument("--cost", type=float, required=True, help="Cost basis per share")
|
|
135
|
+
p.add_argument("--quantity", type=float, required=True, help="Number of shares held")
|
|
136
|
+
p.add_argument("--stop-loss", type=float, help="Stop loss price")
|
|
137
|
+
p.add_argument("--take-profit", type=float, help="Take profit price")
|
|
138
|
+
|
|
139
|
+
args = parser.parse_args()
|
|
140
|
+
if not args.command:
|
|
141
|
+
parser.print_help()
|
|
142
|
+
sys.exit(1)
|
|
143
|
+
|
|
144
|
+
result = analyze_position(
|
|
145
|
+
args.symbol,
|
|
146
|
+
cost=args.cost,
|
|
147
|
+
quantity=args.quantity,
|
|
148
|
+
stop_loss=args.stop_loss,
|
|
149
|
+
take_profit=args.take_profit,
|
|
150
|
+
)
|
|
151
|
+
json.dump(result, sys.stdout, ensure_ascii=False, indent=2, default=str)
|
|
152
|
+
print()
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
if __name__ == "__main__":
|
|
156
|
+
main()
|