@weaxs/openclaw-stock-analysis 0.2.8 → 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.
Files changed (46) hide show
  1. package/README.md +3 -1
  2. package/dist/index.js +154 -20
  3. package/index.ts +126 -6
  4. package/openclaw.plugin.json +4 -1
  5. package/package.json +4 -3
  6. package/scripts/profile-offline.py +127 -0
  7. package/scripts/profile-workflows.py +87 -0
  8. package/scripts/setup-python.mjs +11 -4
  9. package/scripts/venv-python.mjs +7 -0
  10. package/skills/event-driven/SKILL.md +2 -0
  11. package/skills/growth-quality/SKILL.md +2 -0
  12. package/skills/market-review/SKILL.md +1 -1
  13. package/skills/sector-rotation/SKILL.md +57 -0
  14. package/skills/sector-rotation/references/computation.md +83 -0
  15. package/skills/sector-rotation/references/strategy_rules.md +62 -0
  16. package/skills/sector-rotation/scripts/rotation_metrics.py +11 -0
  17. package/skills/stock-analysis/SKILL.md +15 -4
  18. package/skills/stock-analysis/references/host-workflows.md +45 -0
  19. package/skills/stock-analysis/references/stateless-analysis.md +16 -2
  20. package/skills/stock-screener/SKILL.md +6 -0
  21. package/skills/strategy-backtest/SKILL.md +10 -3
  22. package/tools/_cache.py +93 -21
  23. package/tools/_subproc.py +43 -8
  24. package/tools/anomaly_detect.py +11 -6
  25. package/tools/backtest.py +225 -70
  26. package/tools/capabilities.py +3 -0
  27. package/tools/cli_budgets.json +2 -1
  28. package/tools/gather.py +63 -8
  29. package/tools/market_regime.py +26 -15
  30. package/tools/market_review.py +2 -2
  31. package/tools/position_context.py +5 -2
  32. package/tools/report_renderer.py +4 -0
  33. package/tools/requirements.txt +2 -2
  34. package/tools/risk_screening.py +40 -15
  35. package/tools/screener.py +44 -18
  36. package/tools/search_intel.py +94 -32
  37. package/tools/sec_holders.py +27 -25
  38. package/tools/sector_rotation.py +466 -0
  39. package/tools/signal_tracker.py +100 -49
  40. package/tools/stateless_analysis.py +600 -33
  41. package/tools/stock_data.py +111 -26
  42. package/tools/technical.py +42 -40
  43. package/tools/trading_calendar.py +26 -21
  44. package/tools/value_data.py +6 -2
  45. package/tools/watchlist.py +24 -15
  46. package/tools/watchlist_context.py +63 -49
@@ -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
 
@@ -4,17 +4,15 @@ import csv
4
4
  import hashlib
5
5
  import io
6
6
  import math
7
- import os
8
7
  import re
9
- import tempfile
10
8
  import time
11
9
  import zipfile
12
10
  from collections import Counter
13
11
  from datetime import date
14
- from pathlib import Path
15
12
  from xml.etree import ElementTree
16
13
 
17
14
  import _cache
15
+ from _cache import atomic_write, file_lock
18
16
  from sec_edgar import archive_base, sec_get, xml_root
19
17
 
20
18
  CATALOG = "https://www.sec.gov/data-research/sec-markets-data/form-13f-data-sets"
@@ -102,34 +100,38 @@ def latest_bulk():
102
100
  return max(windows, key=lambda w: w["end"])
103
101
 
104
102
 
103
+ def _fresh_archive(path):
104
+ try:
105
+ return 0 <= time.time() - path.stat().st_mtime < _cache.WEEK and zipfile.is_zipfile(path)
106
+ except OSError:
107
+ return False
108
+
109
+
105
110
  def bulk_file(window):
106
111
  path = _cache.CACHE_DIR / f"sec-13f-{hashlib.sha256(window['url'].encode()).hexdigest()[:16]}.zip"
107
- if path.exists() and 0 <= time.time() - path.stat().st_mtime < _cache.WEEK and zipfile.is_zipfile(path):
112
+ if _fresh_archive(path):
108
113
  return path
109
- path.parent.mkdir(parents=True, exist_ok=True)
110
- tmp = None
111
114
  try:
112
- response = sec_get(window["url"], stream=True)
113
- try:
114
- with tempfile.NamedTemporaryFile(dir=path.parent, delete=False) as out:
115
- tmp = Path(out.name)
116
- size = 0
117
- for chunk in response.iter_content(chunk_size=1024 * 1024):
118
- size += len(chunk)
119
- if size > 256 * 1024 * 1024:
120
- raise ValueError("SEC bulk archive exceeds the 256 MiB download limit")
121
- out.write(chunk)
122
- finally:
123
- response.close()
124
- if not zipfile.is_zipfile(tmp):
125
- raise ValueError("SEC bulk download is not a valid ZIP archive")
126
- os.replace(tmp, path)
127
- return path
115
+ with file_lock(path.with_suffix(".lock")):
116
+ if _fresh_archive(path):
117
+ return path
118
+ response = sec_get(window["url"], stream=True)
119
+ try:
120
+ with atomic_write(path, binary=True) as out:
121
+ size = 0
122
+ for chunk in response.iter_content(chunk_size=1024 * 1024):
123
+ size += len(chunk)
124
+ if size > 256 * 1024 * 1024:
125
+ raise ValueError("SEC bulk archive exceeds the 256 MiB download limit")
126
+ out.write(chunk)
127
+ out.flush()
128
+ if not zipfile.is_zipfile(out):
129
+ raise ValueError("SEC bulk download is not a valid ZIP archive")
130
+ finally:
131
+ response.close()
132
+ return path
128
133
  except OSError as exc:
129
134
  raise ValueError(f"SEC bulk archive unavailable ({type(exc).__name__})") from None
130
- finally:
131
- if tmp:
132
- tmp.unlink(missing_ok=True)
133
135
 
134
136
 
135
137
  def table_rows(archive, table):