@weaxs/openclaw-stock-analysis 0.2.1 → 0.2.2
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/package.json +1 -1
- package/tools/stock_data.py +103 -19
package/package.json
CHANGED
package/tools/stock_data.py
CHANGED
|
@@ -2029,6 +2029,20 @@ def _board_entry(name, source: str, board_type: str) -> dict:
|
|
|
2029
2029
|
return {"name": str(name), "source": source, "board_type": board_type}
|
|
2030
2030
|
|
|
2031
2031
|
|
|
2032
|
+
class _DataMiss(ValueError):
|
|
2033
|
+
"""Per-symbol data miss: the provider answered but doesn't cover this code
|
|
2034
|
+
(empty DataFrame / no industry field for an ETF/BSE/uncovered symbol). Never an
|
|
2035
|
+
outage — resolve_stock_sectors' negative cache must not mark the leg down."""
|
|
2036
|
+
|
|
2037
|
+
|
|
2038
|
+
def _require_industry(value, source: str) -> str:
|
|
2039
|
+
"""Unwrap a provider's industry field into a real name. pandas NaN is truthy —
|
|
2040
|
+
only a non-empty string counts; a missing one is a _DataMiss, not an outage."""
|
|
2041
|
+
if not (isinstance(value, str) and value):
|
|
2042
|
+
raise _DataMiss(f"{source} has no industry")
|
|
2043
|
+
return value
|
|
2044
|
+
|
|
2045
|
+
|
|
2032
2046
|
def _stock_boards_em(symbol: str, info_map=None) -> list:
|
|
2033
2047
|
"""A-share industry board from eastmoney individual info. Pass an already-fetched
|
|
2034
2048
|
info_map (item → value) to reuse it instead of hitting the network again."""
|
|
@@ -2037,12 +2051,9 @@ def _stock_boards_em(symbol: str, info_map=None) -> list:
|
|
|
2037
2051
|
|
|
2038
2052
|
df = _akshare_retry(ak.stock_individual_info_em, symbol=symbol)
|
|
2039
2053
|
if df is None or df.empty:
|
|
2040
|
-
raise
|
|
2054
|
+
raise _DataMiss("eastmoney individual info unavailable")
|
|
2041
2055
|
info_map = {row.iloc[0]: row.iloc[1] for _, row in df.iterrows()}
|
|
2042
|
-
industry = info_map.get("行业")
|
|
2043
|
-
# pandas NaN is truthy — only a real string counts as an industry name
|
|
2044
|
-
if not (isinstance(industry, str) and industry):
|
|
2045
|
-
raise ValueError("eastmoney individual info has no industry")
|
|
2056
|
+
industry = _require_industry(info_map.get("行业"), "eastmoney individual info")
|
|
2046
2057
|
return [_board_entry(industry, "eastmoney", "industry")]
|
|
2047
2058
|
|
|
2048
2059
|
|
|
@@ -2052,7 +2063,7 @@ def _stock_boards_efinance(symbol: str) -> list:
|
|
|
2052
2063
|
|
|
2053
2064
|
df = ef.stock.get_belong_board(symbol)
|
|
2054
2065
|
if df is None or df.empty or "板块名称" not in df.columns:
|
|
2055
|
-
raise
|
|
2066
|
+
raise _DataMiss("efinance belong-board unavailable")
|
|
2056
2067
|
# pandas NaN is truthy and str(nan) == "nan" — drop non-string names or they
|
|
2057
2068
|
# would pollute the dedup in resolve_stock_sectors as a bogus "nan" board
|
|
2058
2069
|
return [
|
|
@@ -2080,17 +2091,30 @@ def _stock_boards_xueqiu(symbol: str) -> list:
|
|
|
2080
2091
|
|
|
2081
2092
|
df = _akshare_retry(ak.stock_individual_basic_info_xq, symbol=_xq_symbol(symbol), token=token)
|
|
2082
2093
|
if df is None or df.empty:
|
|
2083
|
-
raise
|
|
2094
|
+
raise _DataMiss("xueqiu basic info unavailable")
|
|
2084
2095
|
info_map = {row.iloc[0]: row.iloc[1] for _, row in df.iterrows()}
|
|
2085
2096
|
# affiliate_industry is a nested dict: {'ind_code': 'BK0025', 'ind_name': '汽车整车'}
|
|
2086
2097
|
industry = info_map.get("affiliate_industry")
|
|
2087
|
-
name = industry.get("ind_name") if isinstance(industry, dict) else None
|
|
2088
|
-
# pandas NaN is truthy — only a real string counts as an industry name
|
|
2089
|
-
if not (isinstance(name, str) and name):
|
|
2090
|
-
raise ValueError("xueqiu basic info has no industry")
|
|
2098
|
+
name = _require_industry(industry.get("ind_name") if isinstance(industry, dict) else None, "xueqiu basic info")
|
|
2091
2099
|
return [_board_entry(name, "xueqiu", "industry")]
|
|
2092
2100
|
|
|
2093
2101
|
|
|
2102
|
+
def _stock_boards_cninfo(symbol: str) -> list:
|
|
2103
|
+
"""A-share industry from cninfo (官方披露站) via akshare stock_profile_cninfo —
|
|
2104
|
+
the token-free, non-eastmoney active leg (issue #35). Runs only as the
|
|
2105
|
+
last-resort fallback: its industry naming follows the CSRC classification
|
|
2106
|
+
(酒、饮料和精制茶制造业), not eastmoney board names (酿酒行业), so its entries
|
|
2107
|
+
don't resolve against eastmoney/sina board lists downstream."""
|
|
2108
|
+
import akshare as ak
|
|
2109
|
+
|
|
2110
|
+
bare = symbol[2:] if _A_PREFIXED_RE.fullmatch(symbol) else symbol
|
|
2111
|
+
df = _akshare_retry(ak.stock_profile_cninfo, symbol=bare)
|
|
2112
|
+
if df is None or df.empty or "所属行业" not in df.columns:
|
|
2113
|
+
raise _DataMiss("cninfo profile unavailable")
|
|
2114
|
+
industry = _require_industry(df.iloc[0].get("所属行业"), "cninfo profile")
|
|
2115
|
+
return [_board_entry(industry, "cninfo", "industry")]
|
|
2116
|
+
|
|
2117
|
+
|
|
2094
2118
|
def _stock_boards_from_cache(symbol: str) -> list:
|
|
2095
2119
|
"""Reverse lookup against the get_sector_constituents disk cache — the honest
|
|
2096
2120
|
fallback when eastmoney blocks the caller's IP and xueqiu has no token (the two
|
|
@@ -2137,10 +2161,34 @@ def _stock_sectors_hk(symbol: str) -> list:
|
|
|
2137
2161
|
return sectors
|
|
2138
2162
|
|
|
2139
2163
|
|
|
2164
|
+
# A failed A-share resolve leg is skipped for this long on subsequent calls
|
|
2165
|
+
# (issue #35): the motivating failure mode — regional blocking of eastmoney — is
|
|
2166
|
+
# chronic, while a 1h TTL keeps a transient blip from degrading the merged result
|
|
2167
|
+
# for the whole day. An expired marker costs one retried attempt (self-healing).
|
|
2168
|
+
_RESOLVE_DOWN_TTL = 3600
|
|
2169
|
+
|
|
2170
|
+
|
|
2171
|
+
def _resolve_leg_down(name: str) -> bool:
|
|
2172
|
+
"""True while leg `name`'s failure marker is fresh. The marker stores a plain
|
|
2173
|
+
timestamp — the effective TTL above is shorter than the 24h file lifetime that
|
|
2174
|
+
_disk_cache_get enforces on the shared cache dir."""
|
|
2175
|
+
ts = _disk_cache_get(f"resolve-down-{name}")
|
|
2176
|
+
return isinstance(ts, (int, float)) and time.time() - ts < _RESOLVE_DOWN_TTL
|
|
2177
|
+
|
|
2178
|
+
|
|
2140
2179
|
def resolve_stock_sectors(symbol: str, info_map=None) -> dict:
|
|
2141
|
-
"""Reverse map: stock → boards it belongs to (A/HK).
|
|
2142
|
-
is not fatal — the
|
|
2143
|
-
|
|
2180
|
+
"""Reverse map: stock → boards it belongs to (A/HK). Sources are merged (a single
|
|
2181
|
+
source failing is not fatal — the others' boards are still returned) rather than
|
|
2182
|
+
first-hit failover: each source covers a different flavor (industry vs concept).
|
|
2183
|
+
An already-fetched eastmoney info_map is reused for the em source instead of
|
|
2184
|
+
re-fetching; that leg then needs no network and bypasses the failure cache.
|
|
2185
|
+
|
|
2186
|
+
A-share legs are socket-timeout-bounded and negative-cached per leg (issue #35):
|
|
2187
|
+
on an eastmoney-blackholed host the first call pays each dead leg's bounded
|
|
2188
|
+
timeout once, later calls skip them until the marker expires. The cninfo leg
|
|
2189
|
+
runs only when every other active leg came up empty — its CSRC industry names
|
|
2190
|
+
don't resolve against eastmoney/sina board lists, so it is a fallback, not an
|
|
2191
|
+
always-on flavor."""
|
|
2144
2192
|
market = detect_market(symbol)
|
|
2145
2193
|
result = {"symbol": symbol, "market": market}
|
|
2146
2194
|
if market == "A":
|
|
@@ -2156,11 +2204,47 @@ def resolve_stock_sectors(symbol: str, info_map=None) -> dict:
|
|
|
2156
2204
|
result["error"] = f"resolve_stock_sectors only supports A-share and HK stocks, got market {market}"
|
|
2157
2205
|
return result
|
|
2158
2206
|
sectors, errors = [], []
|
|
2159
|
-
|
|
2207
|
+
|
|
2208
|
+
def run_legs(legs, negative_cache: bool):
|
|
2209
|
+
for name, fn in legs:
|
|
2210
|
+
# em with an info_map in hand is a pure dict lookup — never skip it, and
|
|
2211
|
+
# a lookup miss must not mark a network leg down
|
|
2212
|
+
offline_em = name == "eastmoney" and info_map is not None
|
|
2213
|
+
if negative_cache and not offline_em and _resolve_leg_down(name):
|
|
2214
|
+
errors.append(f"{name}: skipped (failed within {_RESOLVE_DOWN_TTL}s)")
|
|
2215
|
+
continue
|
|
2216
|
+
try:
|
|
2217
|
+
sectors.extend(fn() or [])
|
|
2218
|
+
if negative_cache and not offline_em:
|
|
2219
|
+
_disk_cache_set(f"resolve-down-{name}", 0) # success clears the marker
|
|
2220
|
+
except _DataMiss as e:
|
|
2221
|
+
# per-symbol miss (provider answered but doesn't cover this code) —
|
|
2222
|
+
# not an outage: never poison the leg for every other symbol
|
|
2223
|
+
errors.append(f"{name}: {type(e).__name__}: {e}")
|
|
2224
|
+
except Exception as e:
|
|
2225
|
+
errors.append(f"{name}: {type(e).__name__}: {e}")
|
|
2226
|
+
if negative_cache and not offline_em:
|
|
2227
|
+
_disk_cache_set(f"resolve-down-{name}", time.time())
|
|
2228
|
+
|
|
2229
|
+
if market == "A":
|
|
2230
|
+
# akshare/efinance leave requests timeout-less — on a blackholed network a leg
|
|
2231
|
+
# hangs until the kernel TCP timeout (snapshot_a has the same guard). Bound
|
|
2232
|
+
# each blocking socket op so every leg gets its turn within the caller's
|
|
2233
|
+
# subprocess budget.
|
|
2234
|
+
import socket
|
|
2235
|
+
|
|
2236
|
+
old_timeout = socket.getdefaulttimeout()
|
|
2237
|
+
socket.setdefaulttimeout(25)
|
|
2160
2238
|
try:
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2239
|
+
run_legs(sources, negative_cache=True)
|
|
2240
|
+
# cninfo last: the token-free non-eastmoney active fallback (issue #35),
|
|
2241
|
+
# tried only when everything above yielded nothing
|
|
2242
|
+
if not sectors:
|
|
2243
|
+
run_legs([("cninfo", lambda: _stock_boards_cninfo(symbol))], negative_cache=True)
|
|
2244
|
+
finally:
|
|
2245
|
+
socket.setdefaulttimeout(old_timeout)
|
|
2246
|
+
else:
|
|
2247
|
+
run_legs(sources, negative_cache=False)
|
|
2164
2248
|
seen = set()
|
|
2165
2249
|
deduped = []
|
|
2166
2250
|
for s in sectors:
|
|
@@ -2172,7 +2256,7 @@ def resolve_stock_sectors(symbol: str, info_map=None) -> dict:
|
|
|
2172
2256
|
if market == "A":
|
|
2173
2257
|
# em/efinance are both eastmoney-flavored and xueqiu is usually untokened,
|
|
2174
2258
|
# so an all-empty A-share result almost always means eastmoney blocked us.
|
|
2175
|
-
error += ";
|
|
2259
|
+
error += "; 东财/cninfo 均不可用且缓存为空,可先跑 get_sector_constituents 预热或设置 XUEQIU_TOKEN"
|
|
2176
2260
|
result["error"] = error
|
|
2177
2261
|
return result
|
|
2178
2262
|
result["sectors"] = deduped
|