@weaxs/openclaw-stock-analysis 0.2.9 → 0.2.11

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.
@@ -231,12 +231,13 @@ def macd_series(close: pd.Series) -> tuple:
231
231
 
232
232
 
233
233
  def rsi_series(close: pd.Series, period: int) -> pd.Series:
234
- """Full RSI series (SMA of gains/losses over `period`). NaN where there were no losses."""
234
+ """Full RSI series (SMA of gains/losses over `period`)."""
235
235
  delta = close.diff()
236
236
  gain = delta.where(delta > 0, 0.0).rolling(period).mean()
237
237
  loss = (-delta.where(delta < 0, 0.0)).rolling(period).mean()
238
238
  rs = gain / loss.replace(0, np.nan)
239
- return 100 - 100 / (1 + rs)
239
+ rsi = (100 - 100 / (1 + rs)).mask((loss == 0) & (gain > 0), 100)
240
+ return rsi.mask((loss == 0) & (gain == 0), 50)
240
241
 
241
242
 
242
243
  def bollinger_series(close: pd.Series, period: int = 20) -> tuple:
@@ -257,22 +258,23 @@ def kdj_series(high: pd.Series, low: pd.Series, close: pd.Series) -> tuple:
257
258
  return k, d, 3 * k - 2 * d
258
259
 
259
260
 
261
+ def _ma_arrangement(values):
262
+ valid = [value for value in values if value is not None]
263
+ if len(valid) < 3:
264
+ return "insufficient_data"
265
+ if all(a > b for a, b in zip(valid, valid[1:])):
266
+ return "bullish"
267
+ if all(a < b for a, b in zip(valid, valid[1:])):
268
+ return "bearish"
269
+ return "mixed"
270
+
271
+
260
272
  def calc_ma(close: pd.Series) -> dict:
261
273
  mas = {}
262
274
  for n in [5, 10, 20, 60]:
263
275
  ma = ma_series(close, n)
264
276
  mas[f"ma{n}"] = round(float(ma.iloc[-1]), 2) if len(ma.dropna()) > 0 else None
265
- vals = [mas.get(f"ma{n}") for n in [5, 10, 20, 60]]
266
- valid = [v for v in vals if v is not None]
267
- if len(valid) >= 3:
268
- if valid == sorted(valid, reverse=True):
269
- mas["ma_arrangement"] = "bullish"
270
- elif valid == sorted(valid):
271
- mas["ma_arrangement"] = "bearish"
272
- else:
273
- mas["ma_arrangement"] = "mixed"
274
- else:
275
- mas["ma_arrangement"] = "insufficient_data"
277
+ mas["ma_arrangement"] = _ma_arrangement([mas.get(f"ma{n}") for n in [5, 10, 20, 60]])
276
278
  return mas
277
279
 
278
280
 
@@ -353,11 +355,13 @@ def calc_kdj(high: pd.Series, low: pd.Series, close: pd.Series) -> dict:
353
355
 
354
356
 
355
357
  def calc_volume(volume: pd.Series) -> dict:
356
- vol_ma5 = float(volume.rolling(5).mean().iloc[-1])
357
- vol_ma10 = float(volume.rolling(10).mean().iloc[-1])
358
+ vol_ma5 = float(volume.rolling(5).mean().shift(1).iloc[-1])
359
+ vol_ma10 = float(volume.rolling(10).mean().shift(1).iloc[-1])
358
360
  cur = float(volume.iloc[-1])
359
- ratio = cur / vol_ma5 if vol_ma5 > 0 else 0
360
- if ratio > 3:
361
+ ratio = cur / vol_ma5 if np.isfinite(vol_ma5) and vol_ma5 > 0 else None
362
+ if ratio is None:
363
+ signal = "insufficient_data"
364
+ elif ratio > 3:
361
365
  signal = "extremely_heavy"
362
366
  elif ratio > 2:
363
367
  signal = "heavy"
@@ -367,9 +371,9 @@ def calc_volume(volume: pd.Series) -> dict:
367
371
  signal = "normal"
368
372
  return {
369
373
  "current": int(cur),
370
- "vol_ma5": round(vol_ma5, 0),
371
- "vol_ma10": round(vol_ma10, 0),
372
- "volume_ratio": round(ratio, 2),
374
+ "vol_ma5": round(vol_ma5, 0) if np.isfinite(vol_ma5) else None,
375
+ "vol_ma10": round(vol_ma10, 0) if np.isfinite(vol_ma10) else None,
376
+ "volume_ratio": round(ratio, 2) if ratio is not None else None,
373
377
  "signal": signal,
374
378
  }
375
379
 
@@ -411,9 +415,11 @@ def calc_bias(close: pd.Series, ma_data: dict) -> dict:
411
415
 
412
416
  def calc_volume_direction(close: pd.Series, volume: pd.Series) -> str:
413
417
  if len(close) < 2:
414
- return "normal"
418
+ return "insufficient_data"
415
419
  price_up = float(close.iloc[-1]) > float(close.iloc[-2])
416
- vol_ma5 = float(volume.rolling(5).mean().iloc[-1])
420
+ vol_ma5 = float(volume.rolling(5).mean().shift(1).iloc[-1])
421
+ if not np.isfinite(vol_ma5) or vol_ma5 <= 0:
422
+ return "insufficient_data"
417
423
  cur_vol = float(volume.iloc[-1])
418
424
  heavy = cur_vol > vol_ma5 * 1.5
419
425
  shrink = cur_vol < vol_ma5 * 0.7
@@ -461,6 +467,8 @@ def generate_signal_score(
461
467
  # --- Trend score (30 points) ---
462
468
  arrangement = ma_data.get("ma_arrangement", "mixed")
463
469
  overall = trend_data.get("overall", "neutral")
470
+ ma_values = [value for value in (ma_data.get(f"ma{n}") for n in (5, 10, 20, 60)) if value is not None]
471
+ flat_ma = len(ma_values) >= 3 and len(set(ma_values)) == 1
464
472
  short_t = trend_data.get("short_term", "neutral")
465
473
  medium_t = trend_data.get("medium_term", "neutral")
466
474
  long_t = trend_data.get("long_term", "neutral")
@@ -479,15 +487,15 @@ def generate_signal_score(
479
487
  reasons.append("短中期趋势偏多")
480
488
  elif arrangement == "mixed":
481
489
  trend_score = 12
482
- elif bear_count >= 2:
483
- trend_score = 8
484
- risks.append("中长期趋势偏空")
485
490
  elif arrangement == "bearish" and bear_count == 3:
486
491
  trend_score = 0
487
492
  risks.append("均线空头排列,趋势极弱")
488
493
  elif arrangement == "bearish":
489
494
  trend_score = 4
490
495
  risks.append("均线空头排列,不宜做多")
496
+ elif bear_count >= 2:
497
+ trend_score = 8
498
+ risks.append("中长期趋势偏空")
491
499
  else:
492
500
  trend_score = 12
493
501
  score += trend_score
@@ -604,9 +612,9 @@ def generate_signal_score(
604
612
  score += rsi_score
605
613
 
606
614
  # --- Buy signal classification ---
607
- if score >= 75 and overall == "bullish":
615
+ if score >= 75 and overall == "bullish" and not flat_ma:
608
616
  buy_signal = "STRONG_BUY"
609
- elif score >= 60 and overall in ("bullish", "neutral"):
617
+ elif score >= 60 and overall in ("bullish", "neutral") and not flat_ma:
610
618
  buy_signal = "BUY"
611
619
  elif score >= 45:
612
620
  buy_signal = "HOLD"
@@ -640,7 +648,9 @@ def generate_signals(
640
648
  signals.append("MACD空头排列(DIF < DEA)")
641
649
 
642
650
  rsi_sig = rsi_data.get("signal", "")
643
- if "overbought" in rsi_sig:
651
+ if rsi_sig == "insufficient_data":
652
+ signals.append("RSI数据不足,无法判断超买超卖")
653
+ elif "overbought" in rsi_sig:
644
654
  signals.append(f"RSI超买区间(RSI6={rsi_data.get('rsi6')}),注意回调风险")
645
655
  elif "oversold" in rsi_sig:
646
656
  signals.append(f"RSI超卖区间(RSI6={rsi_data.get('rsi6')}),关注反弹机会")
@@ -663,7 +673,9 @@ def generate_signals(
663
673
  signals.append(f"KDJ超卖(J={kdj_data['j']}),关注低位机会")
664
674
 
665
675
  vs = vol_data.get("signal", "")
666
- if "heavy" in vs:
676
+ if vs == "insufficient_data":
677
+ signals.append("成交量数据不足,无法判断放量或缩量")
678
+ elif "heavy" in vs:
667
679
  signals.append(f"成交量放大(量比={vol_data['volume_ratio']}),市场关注度高")
668
680
  elif vs == "light":
669
681
  signals.append(f"缩量交易(量比={vol_data['volume_ratio']}),市场观望情绪浓")
@@ -712,17 +724,7 @@ def calculate_ma_standalone(
712
724
  else:
713
725
  bias[f"bias_ma{n}"] = None
714
726
 
715
- valid_mas = [(n, mas[f"ma{n}"]) for n in periods if mas.get(f"ma{n}") is not None]
716
- if len(valid_mas) >= 3:
717
- vals = [v for _, v in valid_mas]
718
- if vals == sorted(vals, reverse=True):
719
- alignment = "bullish"
720
- elif vals == sorted(vals):
721
- alignment = "bearish"
722
- else:
723
- alignment = "mixed"
724
- else:
725
- alignment = "insufficient_data"
727
+ alignment = _ma_arrangement([mas[f"ma{n}"] for n in periods if mas.get(f"ma{n}") is not None])
726
728
 
727
729
  cross_signals = []
728
730
  for fast, slow in [(5, 10), (5, 20), (10, 20), (20, 60)]:
@@ -0,0 +1,263 @@
1
+ """Public, login-free TIAA disclosures from the official YouZhiYouXing site."""
2
+
3
+ import re
4
+ import sys
5
+ import threading
6
+ import time
7
+ import xml.etree.ElementTree as ET
8
+ from concurrent.futures import TimeoutError as FutureTimeoutError
9
+ from datetime import datetime, timezone
10
+ from html import unescape
11
+ from html.parser import HTMLParser
12
+ from pathlib import Path
13
+ from urllib.parse import urlparse
14
+
15
+ import requests
16
+
17
+ sys.path.insert(0, str(Path(__file__).resolve().parent))
18
+ from _cache import cached # noqa: E402
19
+
20
+ _SITEMAP = "https://youzhiyouxing.cn/sitemap.xml"
21
+ _ADVISOR_URL = "https://www.csisc.cn/zbscbzw/c100264/list_jjbm.shtml"
22
+ _ARTICLE_URL = re.compile(r"https://youzhiyouxing\.cn/(?:n/)?lite-posts/(\d+)(?:[/?#].*)?")
23
+ _DISCLOSURE_TERMS = ("示例账户", "成分基金", "配置", "业绩", "收益", "调仓", "持仓")
24
+ _MAX_ARTICLES = 40
25
+ _DISCLOSURE_DEADLINE = 30
26
+
27
+
28
+ class _DisclosureTimeout(Exception):
29
+ """A bounded disclosure attempt failed and must not be cached as an empty result."""
30
+
31
+
32
+ def _until(deadline, fn, *args):
33
+ """Run a blocking provider call in a daemon thread and stop waiting at the deadline.
34
+
35
+ A provider that ignores its own timeout can then neither stretch the request nor keep
36
+ the CLI process alive after it has answered.
37
+ """
38
+ box = {}
39
+
40
+ def run():
41
+ try:
42
+ box["value"] = fn(*args)
43
+ except BaseException as exc: # re-raised in the caller's thread
44
+ box["error"] = exc
45
+
46
+ worker = threading.Thread(target=run, daemon=True)
47
+ worker.start()
48
+ worker.join(max(0.0, deadline - time.monotonic()))
49
+ if worker.is_alive():
50
+ raise _DisclosureTimeout
51
+ if "error" in box:
52
+ raise box["error"]
53
+ return box.get("value")
54
+
55
+
56
+ class _ArticleParser(HTMLParser):
57
+ def __init__(self):
58
+ super().__init__(convert_charrefs=True)
59
+ self.meta = {}
60
+ self.parts = []
61
+ self.ignored = 0
62
+
63
+ def handle_starttag(self, tag, attrs):
64
+ if tag in ("script", "style"):
65
+ self.ignored += 1
66
+ if tag == "meta":
67
+ values = dict(attrs)
68
+ if values.get("property") in ("og:title", "og:description"):
69
+ self.meta[values["property"]] = values.get("content", "")
70
+ if tag in ("p", "div", "h1", "h2", "h3", "li", "br"):
71
+ self.parts.append("\n")
72
+
73
+ def handle_endtag(self, tag):
74
+ if tag in ("script", "style") and self.ignored:
75
+ self.ignored -= 1
76
+ if tag in ("p", "div", "h1", "h2", "h3", "li"):
77
+ self.parts.append("\n")
78
+
79
+ def handle_data(self, data):
80
+ if not self.ignored:
81
+ self.parts.append(data)
82
+
83
+ def text(self):
84
+ lines = []
85
+ for line in "".join(self.parts).splitlines():
86
+ clean = re.sub(r"\s+", " ", line).strip()
87
+ if clean:
88
+ lines.append(clean)
89
+ title = self.meta.get("og:title")
90
+ title_lines = [index for index, line in enumerate(lines) if line == title]
91
+ if title_lines:
92
+ lines = lines[title_lines[-1] + 1 :]
93
+ comments = next((index for index, line in enumerate(lines) if line.endswith("条回应")), len(lines))
94
+ return "\n".join(lines[:comments])
95
+
96
+
97
+ def _sitemap_urls():
98
+ response = requests.get(_SITEMAP, headers={"User-Agent": "Mozilla/5.0"}, timeout=(2, 8))
99
+ response.raise_for_status()
100
+ root = ET.fromstring(response.content)
101
+ namespace = {"s": "http://www.sitemaps.org/schemas/sitemap/0.9"}
102
+ entries = []
103
+ for node in root.findall("s:url", namespace):
104
+ location = node.findtext("s:loc", default="", namespaces=namespace)
105
+ match = _ARTICLE_URL.fullmatch(location)
106
+ if match:
107
+ entries.append((node.findtext("s:lastmod", default="", namespaces=namespace), match.group(1)))
108
+ return [
109
+ f"https://youzhiyouxing.cn/n/lite-posts/{article_id}" for _, article_id in sorted(entries, reverse=True)[:40]
110
+ ]
111
+
112
+
113
+ def _search_urls(portfolio_name, budget_seconds=45):
114
+ try:
115
+ from search_intel import search_news
116
+
117
+ query = f"site:youzhiyouxing.cn/n/lite-posts/ {portfolio_name} 示例账户 成分基金"
118
+ result = search_news(query, max_results=10, budget_seconds=budget_seconds)
119
+ if any("time budget exhausted" in str(error).lower() for error in result.get("engines_errors", {}).values()):
120
+ raise _DisclosureTimeout
121
+ urls = []
122
+ for row in result.get("results", []):
123
+ match = _ARTICLE_URL.fullmatch(row.get("url", ""))
124
+ if match:
125
+ urls.append(f"https://youzhiyouxing.cn/n/lite-posts/{match.group(1)}")
126
+ return urls
127
+ except Exception:
128
+ return []
129
+
130
+
131
+ def _article(url, portfolio_name):
132
+ try:
133
+ response = requests.get(url, headers={"User-Agent": "Mozilla/5.0"}, timeout=(1, 3))
134
+ response.raise_for_status()
135
+ parser = _ArticleParser()
136
+ parser.feed(response.text)
137
+ text = parser.text()
138
+ if portfolio_name not in text or not any(term in text for term in _DISCLOSURE_TERMS):
139
+ return None
140
+ date_match = re.search(r"(20\d{2})\s*年\s*(\d{1,2})\s*月\s*(\d{1,2})\s*日", response.text)
141
+ published_at = None
142
+ if date_match:
143
+ published_at = "-".join([date_match.group(1), *(part.zfill(2) for part in date_match.groups()[1:])])
144
+ return {
145
+ "title": parser.meta.get("og:title", ""),
146
+ "url": url,
147
+ "published_at": published_at,
148
+ "summary": parser.meta.get("og:description", ""),
149
+ "text": text[:3000],
150
+ "source": "youzhiyouxing",
151
+ "partial": True,
152
+ }
153
+ except Exception:
154
+ return None
155
+
156
+
157
+ def advisor_metadata(symbol):
158
+ advisor_code = f"A{symbol[4:7]}"
159
+
160
+ def fetch():
161
+ response = requests.get(_ADVISOR_URL, headers={"User-Agent": "Mozilla/5.0"}, timeout=15)
162
+ response.raise_for_status()
163
+ text = response.content.decode("utf-8-sig")
164
+ rows = re.findall(
165
+ r"<tr\b[^>]*>\s*<td\b[^>]*>\s*\d+\s*</td>\s*"
166
+ r"<td\b[^>]*>\s*(.*?)\s*</td>\s*<td\b[^>]*>\s*(A\d{3})\s*</td>",
167
+ text,
168
+ re.S,
169
+ )
170
+ return {
171
+ "organizations": {code: unescape(re.sub(r"<[^>]+>", "", name)).strip() for name, code in rows},
172
+ "retrieved_at": datetime.now(timezone.utc).isoformat(),
173
+ }
174
+
175
+ data = cached("csisc-fund-advisor-organizations", 24 * 60 * 60, fetch)
176
+ return {
177
+ "advisor_code": advisor_code,
178
+ "advisor_org": data["organizations"].get(advisor_code),
179
+ "advisor_mapping_basis": "tiaa_prefix_best_effort",
180
+ "advisor_source_url": _ADVISOR_URL,
181
+ "advisor_retrieved_at": data["retrieved_at"],
182
+ }
183
+
184
+
185
+ def public_disclosures(symbol, portfolio_name):
186
+ def fetch():
187
+ deadline = time.monotonic() + _DISCLOSURE_DEADLINE
188
+ urls = _sitemap_urls()
189
+ search_results = []
190
+
191
+ timeout_state = {"search": False, "articles": False}
192
+ if time.monotonic() < deadline:
193
+ try:
194
+ search_results = _until(
195
+ deadline, _search_urls, portfolio_name, max(1, int(deadline - time.monotonic()))
196
+ )
197
+ # concurrent.futures.TimeoutError only aliases the builtin from Python 3.11.
198
+ except (TimeoutError, FutureTimeoutError, _DisclosureTimeout):
199
+ timeout_state["search"] = True
200
+ if time.monotonic() >= deadline:
201
+ timeout_state["search"] = True
202
+ else:
203
+ timeout_state["search"] = True
204
+ urls = list(dict.fromkeys([*search_results, *urls]))[:_MAX_ARTICLES]
205
+ items = []
206
+ for url in urls:
207
+ if time.monotonic() >= deadline:
208
+ timeout_state["articles"] = True
209
+ break
210
+ try:
211
+ item = _until(deadline, _article, url, portfolio_name)
212
+ if item:
213
+ items.append(item)
214
+ except _DisclosureTimeout:
215
+ timeout_state["articles"] = True
216
+ break
217
+ except (TimeoutError, FutureTimeoutError):
218
+ timeout_state["articles"] = True
219
+ if time.monotonic() >= deadline and urls:
220
+ timeout_state["articles"] = True
221
+ items.sort(key=lambda item: item["published_at"] or "", reverse=True)
222
+ items = items[:3]
223
+ result = {
224
+ "items": items,
225
+ "as_of": max((item["published_at"] for item in items if item["published_at"]), default=None),
226
+ }
227
+ if any(timeout_state.values()):
228
+ if not items:
229
+ raise _DisclosureTimeout
230
+ result["error"] = "disclosure_timeout"
231
+ return result
232
+
233
+ return cached(f"tiaa-public-disclosures:{symbol}", 6 * 60 * 60, fetch)
234
+
235
+
236
+ def search_public(symbol, portfolio_name):
237
+ def fetch():
238
+ try:
239
+ from search_intel import search_news
240
+
241
+ label = portfolio_name or symbol
242
+ result = search_news(f'"{symbol}" "{label}" 示例账户 成分基金 业绩', max_results=3)
243
+ rows = []
244
+ for row in result.get("results", []):
245
+ parsed = urlparse(row.get("url", ""))
246
+ if parsed.scheme not in ("http", "https") or not parsed.netloc:
247
+ continue
248
+ rows.append(
249
+ {
250
+ "title": row.get("title", ""),
251
+ "url": row["url"],
252
+ "snippet": row.get("snippet", ""),
253
+ "source": row.get("source", ""),
254
+ "publisher": parsed.netloc,
255
+ "verification": "unverified_search_result",
256
+ "partial": True,
257
+ }
258
+ )
259
+ return rows
260
+ except Exception:
261
+ return []
262
+
263
+ return cached(f"tiaa-public-search:{symbol}", 6 * 60 * 60, fetch)
@@ -4,10 +4,12 @@
4
4
  import argparse
5
5
  import json
6
6
  import sys
7
- from datetime import datetime, timedelta
7
+ from datetime import date, datetime, timedelta
8
8
  from pathlib import Path
9
+ from zoneinfo import ZoneInfo
9
10
 
10
11
  sys.path.insert(0, str(Path(__file__).resolve().parent))
12
+ from _cache import DAY, cached
11
13
  from _subproc import socket_timeout, utf8_stdio
12
14
 
13
15
  # Supported markets. argparse uses type=str.upper so lowercase input is accepted too.
@@ -18,10 +20,8 @@ def _is_weekend(d: datetime) -> bool:
18
20
  return d.weekday() >= 5
19
21
 
20
22
 
21
- # Process-local memo: one CLI call = one process, and the CN path
22
- # (is_trading_day / the prev|next_trading_days walk-back) would otherwise
23
- # re-fetch the sina calendar per step — up to ~5 fetches for one stale check.
24
- _CN_TRADE_DATES: dict[int, set[str]] = {}
23
+ # Year views reuse a daily cached full calendar across CLI processes.
24
+ _CN_TRADE_DATES: dict[tuple[str, int], set[str]] = {}
25
25
 
26
26
 
27
27
  def cn_trade_dates(year: int) -> set[str]:
@@ -30,25 +30,30 @@ def cn_trade_dates(year: int) -> set[str]:
30
30
  Empty set when the calendar can't be fetched (failures are not cached — the
31
31
  next call retries); callers treat empty as "weekends are the only non-trading
32
32
  days"."""
33
- cached = _CN_TRADE_DATES.get(year)
34
- if cached is not None:
35
- return cached
33
+ stamp = datetime.now(ZoneInfo("Asia/Shanghai")).date().isoformat()
34
+ memo = _CN_TRADE_DATES.get((stamp, year))
35
+ if memo is not None:
36
+ return memo
36
37
  try:
37
- import akshare as ak
38
-
39
- with socket_timeout(25):
40
- df = ak.tool_trade_date_hist_sina()
41
- dates = set()
42
- for _, row in df.iterrows():
43
- val = row.iloc[0]
44
- d = str(val)[:10]
45
- if d.startswith(str(year)):
46
- dates.add(d)
38
+
39
+ def fetch():
40
+ import akshare as ak
41
+
42
+ with socket_timeout(25):
43
+ df = ak.tool_trade_date_hist_sina()
44
+ if df is None or df.empty:
45
+ return []
46
+ return sorted({date.fromisoformat(str(value)[:10]).isoformat() for value in df.iloc[:, 0]})
47
+
48
+ dates = cached(f"cn-trade-calendar:v1:{stamp}", DAY, fetch)
49
+ grouped = {}
50
+ for day in dates:
51
+ parsed = date.fromisoformat(day).isoformat()
52
+ grouped.setdefault((stamp, int(parsed[:4])), set()).add(parsed)
53
+ _CN_TRADE_DATES.update(grouped)
47
54
  except Exception:
48
55
  return set()
49
- if dates:
50
- _CN_TRADE_DATES[year] = dates
51
- return dates
56
+ return _CN_TRADE_DATES.get((stamp, year), set())
52
57
 
53
58
 
54
59
  def _is_exchange_trading_day(exchange_code: str, date_str: str) -> bool:
@@ -1146,7 +1146,11 @@ def etf_basket(symbol):
1146
1146
 
1147
1147
  def fund_disclosed_equities(symbol):
1148
1148
  import requests
1149
- from akshare.utils import demjson
1149
+
1150
+ try:
1151
+ from akshare.utils.jsonish import loads as decode
1152
+ except ImportError:
1153
+ from akshare.utils.demjson import decode
1150
1154
  from bs4 import BeautifulSoup
1151
1155
 
1152
1156
  limit = 10000
@@ -1160,7 +1164,7 @@ def fund_disclosed_equities(symbol):
1160
1164
  )
1161
1165
  response.raise_for_status()
1162
1166
  text = response.text
1163
- payload = demjson.decode(text[text.index("{") : text.rindex("}") + 1])
1167
+ payload = decode(text[text.index("{") : text.rindex("}") + 1])
1164
1168
  soup = BeautifulSoup(payload["content"], "lxml")
1165
1169
  for block in soup.select("div.boxitem"):
1166
1170
  heading = block.find("h4")
@@ -6,11 +6,12 @@ import json
6
6
  import math
7
7
  import sys
8
8
  from concurrent.futures import ThreadPoolExecutor, as_completed
9
+ from contextlib import nullcontext
9
10
  from pathlib import Path
10
11
 
11
12
  sys.path.insert(0, str(Path(__file__).resolve().parent))
12
13
  from _subproc import utf8_stdio
13
- from gather import gather_analysis
14
+ from gather import Collection, gather_analysis
14
15
 
15
16
 
16
17
  def has_analysis_data(data) -> bool:
@@ -28,27 +29,35 @@ def has_analysis_data(data) -> bool:
28
29
  return False
29
30
 
30
31
 
31
- def analyze_watchlist(symbols: list[str], workers: int = 3) -> dict:
32
+ def analyze_watchlist(symbols: list[str], workers: int = 3, *, session=None, on_result=None) -> dict:
32
33
  if isinstance(workers, bool) or not isinstance(workers, int) or workers <= 0:
33
34
  return {"error": "workers must be a positive integer"}
34
35
  results = {}
35
36
  success = 0
36
37
  failed = 0
37
38
 
38
- with ThreadPoolExecutor(max_workers=workers) as pool:
39
- futures = {pool.submit(gather_analysis, sym): sym for sym in symbols}
40
- for future in as_completed(futures):
41
- sym = futures[future]
42
- try:
43
- data = future.result()
44
- results[sym] = data
45
- if has_analysis_data(data):
46
- success += 1
47
- else:
39
+ with Collection() if session is None else nullcontext(session) as session:
40
+
41
+ def analyze(sym):
42
+ data = gather_analysis(sym, session=session)
43
+ if on_result is not None:
44
+ on_result(sym, data)
45
+ return data
46
+
47
+ with ThreadPoolExecutor(max_workers=workers) as pool:
48
+ futures = {pool.submit(analyze, sym): sym for sym in symbols}
49
+ for future in as_completed(futures):
50
+ sym = futures[future]
51
+ try:
52
+ data = future.result()
53
+ results[sym] = data
54
+ if has_analysis_data(data):
55
+ success += 1
56
+ else:
57
+ failed += 1
58
+ except Exception:
59
+ results[sym] = None
48
60
  failed += 1
49
- except Exception:
50
- results[sym] = None
51
- failed += 1
52
61
 
53
62
  return {
54
63
  "results": results,