stock-analysis-plugin 0.2.9 → 0.2.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.
@@ -54,7 +54,8 @@ def _get_index_kline(market: str) -> list:
54
54
  def _compute_indicators(klines: list) -> dict:
55
55
  import numpy as np
56
56
 
57
- df = pd.DataFrame(klines).reindex(columns=["close", "high", "low"]).apply(pd.to_numeric, errors="coerce")
57
+ raw = pd.DataFrame(klines)
58
+ df = raw.reindex(columns=["close", "high", "low"]).apply(pd.to_numeric, errors="coerce")
58
59
  df = df[np.isfinite(df).all(axis=1) & (df > 0).all(axis=1)]
59
60
  closes = df["close"].to_numpy()
60
61
  if len(closes) < 20:
@@ -64,7 +65,7 @@ def _compute_indicators(klines: list) -> dict:
64
65
  ma5 = float(ma_series(close_s, 5).iloc[-1])
65
66
  ma10 = float(ma_series(close_s, 10).iloc[-1])
66
67
  ma20 = float(ma_series(close_s, 20).iloc[-1])
67
- ma60 = float(ma_series(close_s, 60).iloc[-1]) if len(closes) >= 60 else float(np.mean(closes))
68
+ ma60 = float(ma_series(close_s, 60).iloc[-1]) if len(closes) >= 60 else None
68
69
  close = float(closes[-1])
69
70
 
70
71
  highs = df["high"].to_numpy()
@@ -79,14 +80,7 @@ def _compute_indicators(klines: list) -> dict:
79
80
 
80
81
  upper, mid, lower = bollinger_series(close_s)
81
82
  boll_width = float((upper.iloc[-1] - lower.iloc[-1]) / mid.iloc[-1]) if mid.iloc[-1] > 0 else 0
82
-
83
- # rsi_series leaves NaN when the window has zero losses: 14 straight up bars
84
- # is RSI 100, but 14 flat bars (no gains either) is undefined — call it neutral.
85
- rsi_val = rsi_series(close_s, 14).iloc[-1]
86
- if pd.isna(rsi_val):
87
- rsi14 = 100.0 if float(close_s.diff().clip(lower=0).iloc[-14:].sum()) > 0 else 50.0
88
- else:
89
- rsi14 = float(rsi_val)
83
+ rsi14 = float(rsi_series(close_s, 14).iloc[-1])
90
84
 
91
85
  # Real EMA12-EMA26 (the old code mislabeled an np.mean simple average as EMA).
92
86
  dif, _, _ = macd_series(close_s)
@@ -96,12 +90,15 @@ def _compute_indicators(klines: list) -> dict:
96
90
 
97
91
  prev_close = float(closes[-2]) if len(closes) >= 2 else close
98
92
  change_pct = (close - prev_close) / prev_close * 100 if prev_close > 0 else 0
93
+ as_of = pd.to_datetime(raw.iloc[df.index[-1]].get("date"), errors="coerce")
99
94
 
100
95
  return {
96
+ "as_of": as_of.strftime("%Y-%m-%d") if not pd.isna(as_of) else None,
97
+ "dropped_rows": len(klines) - len(df),
101
98
  "ma5": round(ma5, 2),
102
99
  "ma10": round(ma10, 2),
103
100
  "ma20": round(ma20, 2),
104
- "ma60": round(ma60, 2),
101
+ "ma60": round(ma60, 2) if ma60 is not None else None,
105
102
  "close": round(close, 2),
106
103
  "change_pct": round(change_pct, 2),
107
104
  "atr_ratio": round(atr_ratio, 4),
@@ -124,14 +121,14 @@ def classify_regime(ind: dict) -> tuple:
124
121
 
125
122
  bullish_ma = ma5 > ma10 > ma20
126
123
  bearish_ma = ma5 < ma10 < ma20
127
- above_ma60 = close > ma60
124
+ above_ma60 = close > ma60 if ma60 is not None else None
128
125
  high_volatility = atr_ratio > 0.025 or boll_width > 0.15
129
126
  strong_trend = ma_spread > 0.03
130
127
 
131
128
  if bullish_ma and above_ma60 and strong_trend:
132
129
  regime = "trending_up"
133
130
  confidence = 0.85 + (0.1 if ma_spread > 0.05 else 0)
134
- elif bearish_ma and not above_ma60 and strong_trend:
131
+ elif bearish_ma and above_ma60 is not None and not above_ma60 and strong_trend:
135
132
  regime = "trending_down"
136
133
  confidence = 0.85 + (0.1 if ma_spread > 0.05 else 0)
137
134
  elif high_volatility and not strong_trend:
@@ -176,6 +173,13 @@ def detect_regime(market: str = "A") -> dict:
176
173
  if not klines:
177
174
  return {"error": f"Failed to fetch index data for {name}"}
178
175
 
176
+ import numpy as np
177
+
178
+ if pd.isna(pd.to_datetime(klines[-1].get("date"), errors="coerce")):
179
+ return {"error": "Latest index bar has invalid date"}
180
+ latest = pd.DataFrame([klines[-1]]).reindex(columns=["close", "high", "low"]).apply(pd.to_numeric, errors="coerce")
181
+ if not np.isfinite(latest).all().all() or not (latest > 0).all().all():
182
+ return {"error": "Latest index bar has invalid OHLC data"}
179
183
  ind = _compute_indicators(klines)
180
184
  if not ind:
181
185
  return {"error": "Insufficient data to compute indicators"}
@@ -194,13 +198,19 @@ def detect_regime(market: str = "A") -> dict:
194
198
 
195
199
  return {
196
200
  "market": market,
197
- "index": {"code": code, "name": name, "close": ind["close"], "change_pct": ind["change_pct"]},
201
+ "index": {
202
+ "code": code,
203
+ "name": name,
204
+ "close": ind["close"],
205
+ "change_pct": ind["change_pct"],
206
+ "as_of": ind["as_of"],
207
+ },
198
208
  "regime": regime,
199
209
  "regime_cn": REGIME_CN.get(regime, regime),
200
210
  "confidence": round(confidence, 2),
201
211
  "indicators": {
202
212
  "ma_arrangement": ma_arrangement,
203
- "above_ma60": ind["close"] > ind["ma60"],
213
+ "above_ma60": ind["close"] > ind["ma60"] if ind["ma60"] is not None else None,
204
214
  "ma_spread_pct": ind["ma_spread_pct"],
205
215
  "atr_ratio": ind["atr_ratio"],
206
216
  "boll_width": ind["boll_width"],
@@ -210,6 +220,7 @@ def detect_regime(market: str = "A") -> dict:
210
220
  "sector_heat": sector_heat,
211
221
  "recommended_skills": recommended,
212
222
  "avoid_skills": avoid,
223
+ "warnings": [f"Dropped {ind['dropped_rows']} invalid OHLC rows"] if ind["dropped_rows"] else [],
213
224
  }
214
225
 
215
226
 
@@ -79,7 +79,7 @@ def _signal_to_stance(signal: str) -> str | None:
79
79
  return {"green": "offensive", "yellow": "balanced", "red": "defensive"}.get(signal)
80
80
 
81
81
 
82
- def review_market(market: str = "A") -> dict:
82
+ def review_market(market: str = "A", *, run=None) -> dict:
83
83
  """Gather market data and compute review for a single market."""
84
84
  from datetime import datetime
85
85
 
@@ -106,7 +106,7 @@ def review_market(market: str = "A") -> dict:
106
106
  results = {}
107
107
  with ThreadPoolExecutor(max_workers=5) as executor:
108
108
  futures = {
109
- key: executor.submit(_run_tool, script, args, command_timeout(script, args))
109
+ key: executor.submit(run or _run_tool, script, args, command_timeout(script, args))
110
110
  for key, (script, args) in tasks.items()
111
111
  }
112
112
  for key, future in futures.items():
@@ -8,6 +8,7 @@ import argparse
8
8
  import json
9
9
  import math
10
10
  import sys
11
+ from concurrent.futures import ThreadPoolExecutor
11
12
  from datetime import datetime, timezone
12
13
  from pathlib import Path
13
14
 
@@ -57,8 +58,10 @@ def analyze_position(
57
58
  ):
58
59
  if value is not None and (not isinstance(value, (int, float)) or not math.isfinite(value) or value <= 0):
59
60
  return {"symbol": symbol, "error": f"{field} must be finite and positive"}
60
- quote = _run_json("stock_data.py", ["quote", symbol]) or {}
61
- tech = _run_json("technical.py", ["analyze", symbol, "--period", "daily", "--count", "60"]) or {}
61
+ with ThreadPoolExecutor(max_workers=2) as pool:
62
+ quote_job = pool.submit(_run_json, "stock_data.py", ["quote", symbol])
63
+ tech_job = pool.submit(_run_json, "technical.py", ["analyze", symbol, "--period", "daily", "--count", "60"])
64
+ quote, tech = quote_job.result() or {}, tech_job.result() or {}
62
65
 
63
66
  price = quote.get("price") if isinstance(quote, dict) and "error" not in quote else None
64
67
  if price is None:
@@ -5,7 +5,7 @@ pandas>=2.0.0
5
5
  numpy>=1.24.0
6
6
  pyyaml>=6.0
7
7
  pypinyin>=0.50.0
8
- requests>=2.31.0
8
+ requests>=2.33.0
9
9
  exchange-calendars>=4.5.0
10
10
  newspaper4k>=0.9.0
11
11
  efinance>=0.5.0
@@ -13,4 +13,4 @@ baostock>=0.8.8
13
13
  pytdx>=1.72
14
14
  longport>=1.0.0
15
15
  finnhub-python>=2.4.0
16
- jinja2>=3.1.0
16
+ jinja2>=3.1.6
@@ -6,7 +6,7 @@ import json
6
6
  import math
7
7
  import os
8
8
  import sys
9
- from concurrent.futures import ThreadPoolExecutor
9
+ from concurrent.futures import Future, ThreadPoolExecutor
10
10
  from datetime import datetime, timedelta
11
11
 
12
12
  sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
@@ -28,8 +28,12 @@ def _safe_float(val) -> float | None:
28
28
  # --------------- Check 1: Valuation ---------------
29
29
 
30
30
 
31
- def check_valuation(symbol: str) -> dict:
32
- quote = _run_tool("stock_data.py", ["quote", symbol]) or {}
31
+ _UNSET = object()
32
+
33
+
34
+ def check_valuation(symbol: str, *, quote=_UNSET) -> dict:
35
+ if quote is _UNSET:
36
+ quote = _run_tool("stock_data.py", ["quote", symbol]) or {}
33
37
  if not isinstance(quote, dict) or "error" in quote:
34
38
  quote = {}
35
39
  pe = _safe_float(quote.get("pe"))
@@ -93,8 +97,9 @@ def check_valuation(symbol: str) -> dict:
93
97
  # --------------- Check 2: Technical Warning ---------------
94
98
 
95
99
 
96
- def check_technical(symbol: str) -> dict:
97
- ta = _run_tool("technical.py", ["analyze", symbol])
100
+ def check_technical(symbol: str, *, ta=_UNSET) -> dict:
101
+ if ta is _UNSET:
102
+ ta = _run_tool("technical.py", ["analyze", symbol])
98
103
  result = {"flags": [], "available": False}
99
104
 
100
105
  if not isinstance(ta, dict) or not ta or "error" in ta:
@@ -149,11 +154,17 @@ def check_technical(symbol: str) -> dict:
149
154
  # --------------- Check 3: Lock-up Expiry ---------------
150
155
 
151
156
 
152
- def check_lockup(symbol: str) -> dict:
153
- result = _run_tool("risk_screening.py", ["lockup", symbol], timeout=25)
157
+ def check_lockup(symbol: str, *, run=None) -> dict:
158
+ result = (run or _run_tool)("risk_screening.py", ["lockup", symbol], timeout=25)
154
159
  return (
155
160
  result
156
- if isinstance(result, dict) and "error" not in result
161
+ if (
162
+ isinstance(result, dict)
163
+ and "error" not in result
164
+ and isinstance(result.get("upcoming_30d"), list)
165
+ and isinstance(result.get("flags"), list)
166
+ and isinstance(result.get("available"), bool)
167
+ )
157
168
  else {"upcoming_30d": [], "flags": [], "available": False}
158
169
  )
159
170
 
@@ -204,7 +215,7 @@ def _fetch_lockup(symbol: str) -> dict:
204
215
  # --------------- Check 4-7: News-based risks ---------------
205
216
 
206
217
 
207
- def check_news_risks(symbol: str, name: str = "") -> dict:
218
+ def check_news_risks(symbol: str, name: str = "", *, run=None) -> dict:
208
219
  search_name = name if name else symbol
209
220
  categories = {
210
221
  "insider": f"{search_name} 减持 股东减持",
@@ -218,7 +229,7 @@ def check_news_risks(symbol: str, name: str = "") -> dict:
218
229
  # declaration order keeps result keys and flags stably ordered
219
230
  with ThreadPoolExecutor(max_workers=len(categories)) as pool:
220
231
  futures = {
221
- cat: pool.submit(_run_tool, "search_intel.py", ["search", query, "--count", "3"])
232
+ cat: pool.submit(run or _run_tool, "search_intel.py", ["search", query, "--count", "3"])
222
233
  for cat, query in categories.items()
223
234
  }
224
235
  for cat, future in futures.items():
@@ -279,15 +290,31 @@ def compute_risk(flags: list) -> dict:
279
290
  # --------------- Main entry ---------------
280
291
 
281
292
 
282
- def screen_risk(symbol: str, name: str = "") -> dict:
293
+ def screen_risk(symbol: str, name: str = "", *, inputs=None, run=None) -> dict:
294
+ inputs = inputs or {}
295
+
296
+ def supplied(key):
297
+ value = inputs[key]
298
+ return value.result() if isinstance(value, Future) else value
299
+
300
+ with ThreadPoolExecutor(max_workers=4) as pool:
301
+ valuation = pool.submit(
302
+ lambda: check_valuation(symbol, quote=supplied("quote")) if "quote" in inputs else check_valuation(symbol)
303
+ )
304
+ technical = pool.submit(
305
+ lambda: (
306
+ check_technical(symbol, ta=supplied("technical")) if "technical" in inputs else check_technical(symbol)
307
+ )
308
+ )
309
+ lockups = pool.submit(check_lockup, symbol, **({"run": run} if run else {}))
310
+ searches = pool.submit(check_news_risks, symbol, name, **({"run": run} if run else {}))
311
+ val, tech, lockup, news = (future.result() for future in (valuation, technical, lockups, searches))
283
312
  all_flags = []
284
313
  checks = {}
285
314
 
286
- val = check_valuation(symbol)
287
315
  checks["valuation"] = {k: val[k] for k in ("pe", "pb", "status", "available")}
288
316
  all_flags.extend(val["flags"])
289
317
 
290
- tech = check_technical(symbol)
291
318
  checks["technical"] = {
292
319
  "buy_signal": tech.get("buy_signal"),
293
320
  "signal_score": tech.get("signal_score"),
@@ -296,11 +323,9 @@ def screen_risk(symbol: str, name: str = "") -> dict:
296
323
  }
297
324
  all_flags.extend(tech["flags"])
298
325
 
299
- lockup = check_lockup(symbol)
300
326
  checks["lockup"] = {"upcoming_30d": lockup["upcoming_30d"], "available": lockup["available"]}
301
327
  all_flags.extend(lockup["flags"])
302
328
 
303
- news = check_news_risks(symbol, name)
304
329
  checks["news_risks"] = {k: v for k, v in news.items() if k != "flags"}
305
330
  all_flags.extend(news["flags"])
306
331
 
package/tools/screener.py CHANGED
@@ -10,6 +10,7 @@ import argparse
10
10
  import json
11
11
  import sys
12
12
  from concurrent.futures import ThreadPoolExecutor
13
+ from contextlib import nullcontext
13
14
  from pathlib import Path
14
15
 
15
16
  import numpy as np
@@ -127,12 +128,13 @@ def _finite(v):
127
128
 
128
129
 
129
130
  def compute_scores(df: pd.DataFrame) -> pd.DataFrame:
130
- # Value: lower PE/PB → higher score (inverted)
131
+ # Value: lower positive PE/PB → higher score; finite non-positive values get the worst score.
131
132
  value_parts = []
132
- if "pe" in df.columns:
133
- value_parts.append(100 - _minmax(df["pe"]))
134
- if "pb" in df.columns:
135
- value_parts.append(100 - _minmax(df["pb"]))
133
+ for name in ("pe", "pb"):
134
+ if name in df.columns:
135
+ series = df[name]
136
+ score = 100 - _minmax(series.where(series > 0))
137
+ value_parts.append(score.mask((series <= 0) & np.isfinite(series), 0))
136
138
  if "dividend_yield" in df.columns:
137
139
  df["dividend_score"] = _minmax(df["dividend_yield"])
138
140
  value_parts.append(df["dividend_score"])
@@ -194,11 +196,27 @@ def compute_sentiment(market: str, snapshot: list | None = None) -> dict:
194
196
  return fallback
195
197
 
196
198
 
197
- def _enrich_one(symbol: str, market: str) -> dict:
199
+ def _enrich_one(symbol: str, market: str, *, pool=None) -> dict:
198
200
  """Fetch raw L2 indicators for one candidate; missing factor sources just yield missing keys."""
201
+ jobs = {
202
+ "kline": ["kline", symbol, "--period", "daily", "--count", "250"],
203
+ "fundamental": ["fundamental_context" if market == "A" else "financials", symbol],
204
+ }
205
+ if market == "A":
206
+ jobs["flow"] = ["capital_flow", symbol, "--mode", "summary"]
207
+ collected = {}
208
+ with ThreadPoolExecutor(max_workers=3) if pool is None else nullcontext(pool) as pool:
209
+ futures = {key: pool.submit(_fetch_json, args) for key, args in jobs.items()}
210
+ for key, future in futures.items():
211
+ try:
212
+ collected[key] = future.result()
213
+ except Exception:
214
+ collected[key] = None
199
215
  result: dict = {}
200
216
  try:
201
- kline = _fetch_json(["kline", symbol, "--period", "daily", "--count", "250"])
217
+ kline = collected["kline"]
218
+ if isinstance(kline, list):
219
+ result["vol20_samples"] = 0
202
220
  if isinstance(kline, list) and len(kline) >= 2:
203
221
  closes = pd.Series([float(k["close"]) for k in kline if k.get("close") is not None])
204
222
  highs = [float(k["high"]) for k in kline if k.get("high") is not None]
@@ -210,17 +228,19 @@ def _enrich_one(symbol: str, market: str) -> dict:
210
228
  peak = max(highs)
211
229
  if peak > 0:
212
230
  result["high_250_proximity"] = round(float(closes.iloc[-1]) / peak, 4)
213
- rets = closes.pct_change().dropna().tail(20)
214
- if len(rets) >= 2:
231
+ valid_closes = closes[np.isfinite(closes) & (closes > 0)]
232
+ rets = valid_closes.pct_change().replace([np.inf, -np.inf], np.nan).dropna().tail(20)
233
+ result["vol20_samples"] = len(rets)
234
+ if len(rets) == 20:
215
235
  result["vol20"] = round(float(rets.std()) * float(np.sqrt(252)), 4)
216
236
  except Exception:
217
237
  pass
218
238
 
219
239
  if market == "A":
220
- fund = _fetch_json(["fundamental_context", symbol])
240
+ fund = collected["fundamental"]
221
241
  if isinstance(fund, dict) and "error" not in fund:
222
242
  prof = fund.get("profitability") or {}
223
- for key in ("roe", "gross_margin", "net_margin"):
243
+ for key in ("roe", "gross_margin", "net_margin", "report_date"):
224
244
  if prof.get(key) is not None:
225
245
  result[key] = prof[key]
226
246
  growth = fund.get("growth") or {}
@@ -228,14 +248,14 @@ def _enrich_one(symbol: str, market: str) -> dict:
228
248
  if growth.get(key) is not None:
229
249
  result[key] = growth[key]
230
250
  else:
231
- fin = _fetch_json(["financials", symbol])
251
+ fin = collected["fundamental"]
232
252
  if isinstance(fin, dict) and "error" not in fin:
233
- for key in ("roe", "profit_margin"):
253
+ for key in ("roe", "profit_margin", "report_date"):
234
254
  if fin.get(key) is not None:
235
255
  result[key] = fin[key]
236
256
 
237
257
  if market == "A":
238
- flow = _fetch_json(["capital_flow", symbol, "--mode", "summary"])
258
+ flow = collected["flow"]
239
259
  if isinstance(flow, dict) and "error" not in flow and flow.get("main_net_5d") is not None:
240
260
  result["main_net_5d"] = flow["main_net_5d"]
241
261
  return result
@@ -264,7 +284,11 @@ def _cross_sectional_scores(enrichments: dict) -> dict:
264
284
  margin = profit_margin
265
285
  elif profit_margin is not None:
266
286
  margin = margin.combine_first(profit_margin)
267
- add_group("quality", [_minmax(s) for s in (col("roe"), margin, col("net_margin")) if s is not None])
287
+ report_dates = fdf.get("report_date", pd.Series("unknown", index=fdf.index)).fillna("unknown")
288
+ add_group(
289
+ "quality",
290
+ [s.groupby(report_dates).transform(_minmax) for s in (col("roe"), margin, col("net_margin")) if s is not None],
291
+ )
268
292
  add_group(
269
293
  "growth", [_minmax(s.clip(-50, 100)) for s in (col("revenue_yoy"), col("net_income_yoy")) if s is not None]
270
294
  )
@@ -288,8 +312,8 @@ def _apply_l2(candidates: list, market: str, multiplier: float) -> None:
288
312
  """Enrich candidates with L2 factors and rewrite each scores['final'] as composite_l2 × multiplier."""
289
313
  symbols = [c.get("symbol") for c in candidates]
290
314
  enrichments: dict = {}
291
- with ThreadPoolExecutor(max_workers=6) as executor:
292
- futures = {executor.submit(_enrich_one, sym, market): sym for sym in symbols if sym}
315
+ with ThreadPoolExecutor(max_workers=6) as collection, ThreadPoolExecutor(max_workers=6) as executor:
316
+ futures = {executor.submit(_enrich_one, sym, market, pool=collection): sym for sym in symbols if sym}
293
317
  for future, sym in futures.items():
294
318
  try:
295
319
  enrichments[sym] = future.result() or {}
@@ -301,7 +325,7 @@ def _apply_l2(candidates: list, market: str, multiplier: float) -> None:
301
325
  for c in candidates:
302
326
  sym = c.get("symbol")
303
327
  groups = factor_scores.get(sym, {})
304
- c["enriched"] = bool(enrichments.get(sym))
328
+ c["enriched"] = bool(groups)
305
329
  scores = c["scores"]
306
330
  # scores are sanitized to None when non-finite; L2 math needs numbers
307
331
  components = {
@@ -325,6 +349,7 @@ def _apply_l2(candidates: list, market: str, multiplier: float) -> None:
325
349
  "ratio": round(total_weight, 4),
326
350
  "effective_weights": {g: L2_WEIGHTS[g] / total_weight for g in components},
327
351
  "effective_scores": components,
352
+ "vol20_samples": enrichments.get(sym, {}).get("vol20_samples"),
328
353
  }
329
354
 
330
355
 
@@ -442,6 +467,7 @@ def screen(market: str, top: int, config: dict | None, l2: bool = False) -> dict
442
467
  "volume",
443
468
  "turnover_rate",
444
469
  "pe",
470
+ "pe_basis",
445
471
  "pb",
446
472
  "market_cap",
447
473
  "volume_ratio",
@@ -2,15 +2,62 @@
2
2
  """Search intelligence & social sentiment — multi-engine web search + Reddit/X/A-share sentiment aggregation."""
3
3
 
4
4
  import argparse
5
+ import contextlib
5
6
  import json
6
7
  import os
7
8
  import re
8
9
  import sys
9
10
  import time as _time
11
+ from contextvars import ContextVar
10
12
 
11
13
  sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
12
14
  from _subproc import scrub_secrets, utf8_stdio
13
15
 
16
+ _SEARCH_STATE = ContextVar("search_budget", default=None)
17
+
18
+
19
+ @contextlib.contextmanager
20
+ def _search_budget(seconds):
21
+ parent = _SEARCH_STATE.get()
22
+ state = {
23
+ "deadline": min(_time.monotonic() + seconds, parent["deadline"] if parent else float("inf")),
24
+ "failed": parent["failed"] if parent else {},
25
+ }
26
+ token = _SEARCH_STATE.set(state)
27
+ try:
28
+ yield state
29
+ finally:
30
+ _SEARCH_STATE.reset(token)
31
+
32
+
33
+ def _search_timeout():
34
+ state = _SEARCH_STATE.get()
35
+ remaining = state["deadline"] - _time.monotonic() if state else 15
36
+ if remaining <= 0:
37
+ raise TimeoutError("Search time budget exhausted")
38
+ return min(15, remaining)
39
+
40
+
41
+ class _SearchUnavailable(RuntimeError):
42
+ pass
43
+
44
+
45
+ def _unavailable(error):
46
+ if isinstance(error, (_SearchUnavailable, ConnectionError, TimeoutError)):
47
+ return True
48
+ try:
49
+ from requests.exceptions import ConnectionError as RequestsConnectionError
50
+ from requests.exceptions import Timeout as RequestsTimeout
51
+ except ImportError:
52
+ return False
53
+ return isinstance(error, (RequestsConnectionError, RequestsTimeout))
54
+
55
+
56
+ def _search_error(message, code):
57
+ unavailable = str(code) in {"401", "403", "429"} or re.fullmatch(r"5\d\d", str(code))
58
+ return (_SearchUnavailable if unavailable else RuntimeError)(message)
59
+
60
+
14
61
  # --------------- Web Search ---------------
15
62
 
16
63
 
@@ -28,10 +75,10 @@ def _tavily_search(query: str, max_results: int = 5) -> list[dict]:
28
75
  "max_results": max_results,
29
76
  "search_depth": "basic",
30
77
  },
31
- timeout=15,
78
+ timeout=_search_timeout(),
32
79
  )
33
80
  if not resp.ok:
34
- raise RuntimeError(f"tavily HTTP {resp.status_code}")
81
+ raise _search_error(f"tavily HTTP {resp.status_code}", resp.status_code)
35
82
  data = resp.json()
36
83
  return [
37
84
  {
@@ -54,10 +101,10 @@ def _brave_search(query: str, count: int = 5) -> list[dict]:
54
101
  "https://api.search.brave.com/res/v1/web/search",
55
102
  headers={"X-Subscription-Token": api_key, "Accept": "application/json"},
56
103
  params={"q": query, "count": count},
57
- timeout=15,
104
+ timeout=_search_timeout(),
58
105
  )
59
106
  if not resp.ok:
60
- raise RuntimeError(f"brave HTTP {resp.status_code}")
107
+ raise _search_error(f"brave HTTP {resp.status_code}", resp.status_code)
61
108
  data = resp.json()
62
109
  return [
63
110
  {
@@ -84,10 +131,10 @@ def _serpapi_search(query: str, num: int = 5) -> list[dict]:
84
131
  "num": num,
85
132
  "engine": "google",
86
133
  },
87
- timeout=15,
134
+ timeout=_search_timeout(),
88
135
  )
89
136
  if not resp.ok:
90
- raise RuntimeError(f"serpapi HTTP {resp.status_code}")
137
+ raise _search_error(f"serpapi HTTP {resp.status_code}", resp.status_code)
91
138
  data = resp.json()
92
139
  return [
93
140
  {
@@ -110,15 +157,15 @@ def _bocha_search(query: str, max_results: int = 5) -> list[dict]:
110
157
  "https://api.bochaai.com/v1/web-search",
111
158
  headers={"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"},
112
159
  json={"query": query, "count": max_results},
113
- timeout=15,
160
+ timeout=_search_timeout(),
114
161
  )
115
162
  if not resp.ok:
116
- raise RuntimeError(f"bocha HTTP {resp.status_code}")
163
+ raise _search_error(f"bocha HTTP {resp.status_code}", resp.status_code)
117
164
  data = resp.json()
118
165
  code = data.get("code")
119
166
  if code is not None and str(code) != "200":
120
167
  # Bocha can signal quota/auth failure via HTTP 200 + non-200 envelope code
121
- raise RuntimeError(f"bocha code {code}: {data.get('msg') or data.get('message') or 'unknown'}")
168
+ raise _search_error(f"bocha code {code}: {data.get('msg') or data.get('message') or 'unknown'}", code)
122
169
  # Bocha web-search returns Bing-style data.webPages.value[] (issue #13)
123
170
  items = ((data.get("data") or {}).get("webPages") or {}).get("value") or []
124
171
  return [
@@ -144,6 +191,7 @@ def _searxng_search(query: str, max_results: int = 5) -> list[dict]:
144
191
  import requests
145
192
 
146
193
  last_error = None
194
+ last_exception = None
147
195
  responded = False
148
196
  for base in base_urls:
149
197
  try:
@@ -151,10 +199,11 @@ def _searxng_search(query: str, max_results: int = 5) -> list[dict]:
151
199
  f"{base}/search",
152
200
  params={"q": query, "format": "json"},
153
201
  headers={"Accept": "application/json"},
154
- timeout=15,
202
+ timeout=_search_timeout(),
155
203
  )
156
204
  if not resp.ok:
157
205
  last_error = f"searxng {base} HTTP {resp.status_code}"
206
+ last_exception = _search_error(last_error, resp.status_code)
158
207
  continue
159
208
  responded = True
160
209
  data = resp.json()
@@ -171,7 +220,10 @@ def _searxng_search(query: str, max_results: int = 5) -> list[dict]:
171
220
  return results
172
221
  except Exception as e:
173
222
  last_error = f"searxng {base}: {e}"
223
+ last_exception = e
174
224
  if last_error and not responded:
225
+ if _unavailable(last_exception):
226
+ raise _SearchUnavailable(last_error)
175
227
  raise RuntimeError(last_error)
176
228
  return []
177
229
 
@@ -182,24 +234,33 @@ def search_news(query: str, max_results: int = 10) -> dict:
182
234
  engines_available = []
183
235
  engines_errors = {}
184
236
 
185
- for name, fn in [
186
- ("tavily", _tavily_search),
187
- ("brave", _brave_search),
188
- ("bocha", _bocha_search),
189
- ("serpapi", _serpapi_search),
190
- ("searxng", _searxng_search),
191
- ]:
192
- engines_tried.append(name)
193
- try:
194
- items = fn(query, max_results)
195
- except Exception as e:
196
- engines_errors[name] = scrub_secrets(str(e))
197
- continue
198
- if items:
199
- engines_available.append(name)
200
- results.extend(items)
201
- if len(results) >= max_results:
202
- break
237
+ with _search_budget(45) as state:
238
+ for name, fn in [
239
+ ("tavily", _tavily_search),
240
+ ("brave", _brave_search),
241
+ ("bocha", _bocha_search),
242
+ ("serpapi", _serpapi_search),
243
+ ("searxng", _searxng_search),
244
+ ]:
245
+ engines_tried.append(name)
246
+ if name in state["failed"]:
247
+ engines_errors[name] = state["failed"][name]
248
+ continue
249
+ if _time.monotonic() >= state["deadline"]:
250
+ engines_errors[name] = "Search time budget exhausted"
251
+ continue
252
+ try:
253
+ items = fn(query, max_results)
254
+ except Exception as e:
255
+ engines_errors[name] = scrub_secrets(str(e))
256
+ if _unavailable(e):
257
+ state["failed"][name] = engines_errors[name]
258
+ continue
259
+ if items:
260
+ engines_available.append(name)
261
+ results.extend(items)
262
+ if len(results) >= max_results:
263
+ break
203
264
 
204
265
  if not results:
205
266
  if engines_errors:
@@ -246,10 +307,11 @@ def search_comprehensive(symbol: str, name: str = None) -> dict:
246
307
 
247
308
  results = {}
248
309
  engines_errors = {}
249
- for dim, query in dimensions.items():
250
- data = search_news(query, max_results=3)
251
- results[dim] = data.get("results", [])
252
- engines_errors.update(data.get("engines_errors") or {})
310
+ with _search_budget(90):
311
+ for dim, query in dimensions.items():
312
+ data = search_news(query, max_results=3)
313
+ results[dim] = data.get("results", [])
314
+ engines_errors.update(data.get("engines_errors") or {})
253
315
 
254
316
  return {"symbol": symbol, "name": label, "dimensions": results, "engines_errors": engines_errors}
255
317