@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.
package/tools/_cache.py CHANGED
@@ -1,14 +1,16 @@
1
1
  """TTL cache for slow public datasets; live quotes and errors are never stored."""
2
2
 
3
3
  import contextlib
4
+ import errno
4
5
  import hashlib
5
6
  import json
6
7
  import os
7
- import tempfile
8
+ import sys
8
9
  import time
9
10
  from pathlib import Path
11
+ from uuid import uuid4
10
12
 
11
- CACHE_DIR = Path.home() / ".stock-analysis" / "cache"
13
+ CACHE_DIR = Path(os.environ.get("STOCK_CACHE_DIR") or Path.home() / ".stock-analysis" / "cache")
12
14
  DAY = 86400
13
15
  WEEK = 7 * DAY
14
16
 
@@ -34,28 +36,98 @@ def cached_values(namespace, ttl):
34
36
  yield entry.get("data")
35
37
 
36
38
 
39
+ @contextlib.contextmanager
40
+ def file_lock(path, timeout=5):
41
+ """Coordinate processes using a stable lock inode, with bounded waiting."""
42
+ path = Path(path)
43
+ path.parent.mkdir(parents=True, exist_ok=True)
44
+ with path.open("a+b") as lock:
45
+ if sys.platform == "win32":
46
+ import msvcrt
47
+
48
+ if lock.seek(0, os.SEEK_END) == 0:
49
+ lock.write(b"\0")
50
+ lock.flush()
51
+
52
+ def acquire():
53
+ lock.seek(0)
54
+ msvcrt.locking(lock.fileno(), msvcrt.LK_NBLCK, 1)
55
+
56
+ def release():
57
+ lock.seek(0)
58
+ msvcrt.locking(lock.fileno(), msvcrt.LK_UNLCK, 1)
59
+ else:
60
+ import fcntl
61
+
62
+ def acquire():
63
+ fcntl.flock(lock.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
64
+
65
+ def release():
66
+ fcntl.flock(lock.fileno(), fcntl.LOCK_UN)
67
+
68
+ deadline = time.monotonic() + timeout
69
+ while True:
70
+ try:
71
+ acquire()
72
+ break
73
+ except OSError as exc:
74
+ if exc.errno not in (errno.EACCES, errno.EAGAIN):
75
+ raise
76
+ remaining = deadline - time.monotonic()
77
+ if remaining <= 0:
78
+ raise TimeoutError("File lock timed out") from None
79
+ time.sleep(min(0.05, remaining))
80
+ try:
81
+ yield
82
+ finally:
83
+ release()
84
+
85
+
86
+ @contextlib.contextmanager
87
+ def atomic_write(path, *, binary=False):
88
+ """Publish a private same-directory file only after its writer succeeds."""
89
+ path = Path(path)
90
+ path.parent.mkdir(parents=True, exist_ok=True)
91
+ candidate = path.with_suffix(f".tmp-{uuid4().hex}")
92
+ tmp = None
93
+ try:
94
+ # tempfile may retry ACL denial billions of times on Windows.
95
+ with open(
96
+ candidate,
97
+ "x+b" if binary else "x",
98
+ encoding=None if binary else "utf-8",
99
+ opener=lambda name, flags: os.open(name, flags, 0o600),
100
+ ) as stream:
101
+ tmp = candidate
102
+ yield stream
103
+ os.replace(candidate, path)
104
+ finally:
105
+ if tmp is not None:
106
+ with contextlib.suppress(OSError):
107
+ tmp.unlink(missing_ok=True)
108
+
109
+
37
110
  def cached(key: str, ttl: int, fetch):
38
111
  path = CACHE_DIR / f"{_namespace_prefix(key.partition(':')[0])}-{hashlib.sha256(key.encode()).hexdigest()}.json"
39
112
  entry = _read(path, ttl)
40
113
  if entry and "data" in entry:
41
114
  return entry["data"]
42
- result = fetch()
43
- rows = result if isinstance(result, list) else [result]
44
- if not result or any(isinstance(row, dict) and (row.get("error") or row.get("warnings")) for row in rows):
115
+ with contextlib.ExitStack() as stack:
116
+ # shortcut: slow fetches may outlast lock wait; prefer duplicate work to blocking callers indefinitely.
117
+ with contextlib.suppress(OSError):
118
+ stack.enter_context(file_lock(path.with_suffix(".lock")))
119
+ entry = _read(path, ttl)
120
+ if entry and "data" in entry:
121
+ return entry["data"]
122
+ result = fetch()
123
+ rows = result if isinstance(result, list) else [result]
124
+ if not result or any(isinstance(row, dict) and (row.get("error") or row.get("warnings")) for row in rows):
125
+ return result
126
+ try:
127
+ with atomic_write(path) as stream:
128
+ json.dump(
129
+ {"key": key, "fetched_at": time.time(), "data": result}, stream, ensure_ascii=False, allow_nan=False
130
+ )
131
+ except (OSError, ValueError, TypeError):
132
+ pass
45
133
  return result
46
- tmp = None
47
- try:
48
- CACHE_DIR.mkdir(parents=True, exist_ok=True)
49
- with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", dir=CACHE_DIR, delete=False) as stream:
50
- tmp = stream.name
51
- json.dump(
52
- {"key": key, "fetched_at": time.time(), "data": result}, stream, ensure_ascii=False, allow_nan=False
53
- )
54
- os.replace(tmp, path)
55
- except (OSError, ValueError, TypeError):
56
- pass
57
- finally:
58
- if tmp:
59
- with contextlib.suppress(OSError):
60
- Path(tmp).unlink(missing_ok=True)
61
- return result
package/tools/_subproc.py CHANGED
@@ -14,6 +14,7 @@ import re
14
14
  import socket
15
15
  import subprocess
16
16
  import sys
17
+ import threading
17
18
  from pathlib import Path
18
19
 
19
20
  TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
@@ -113,19 +114,53 @@ def utf8_stdio() -> None:
113
114
  s.reconfigure(encoding="utf-8")
114
115
 
115
116
 
117
+ _timeout_lock = threading.RLock()
118
+ _active_timeouts = {}
119
+ _original_socket_timeout = None
120
+ _original_send = None
121
+
122
+
116
123
  @contextlib.contextmanager
117
124
  def socket_timeout(seconds: float):
118
- """Bound every blocking socket op for the wrapped chain's duration: akshare/efinance
119
- leave requests timeout-less, so on a blackholed network a leg hangs until the kernel
120
- TCP timeout (~2min) and the chain would overrun the caller's subprocess budget before
121
- ever reaching the plain-HTTPS legs. try/finally always restores the previous default
122
- (no global-state leak); explicit per-request timeouts (tencent/sina legs) still win."""
123
- old_timeout = socket.getdefaulttimeout()
124
- socket.setdefaulttimeout(seconds)
125
+ """Default socket/requests timeouts while guards are active (not a total deadline).
126
+
127
+ Like the socket default, this is process-wide: overlapping guards use the shortest
128
+ budget. Explicit requests timeouts still win. Restore only after the last guard exits.
129
+ """
130
+ try:
131
+ import requests
132
+ except ImportError:
133
+ requests = None
134
+
135
+ global _original_socket_timeout, _original_send
136
+ token = object()
137
+ with _timeout_lock:
138
+ if not _active_timeouts:
139
+ _original_socket_timeout = socket.getdefaulttimeout()
140
+ _original_send = requests.sessions.Session.send if requests is not None else None
141
+ if requests is not None:
142
+ original_send = _original_send
143
+
144
+ def bounded_send(session, request, **kwargs):
145
+ with _timeout_lock:
146
+ if kwargs.get("timeout") is None and _active_timeouts:
147
+ kwargs["timeout"] = min(_active_timeouts.values())
148
+ return original_send(session, request, **kwargs)
149
+
150
+ requests.sessions.Session.send = bounded_send
151
+ _active_timeouts[token] = seconds
152
+ socket.setdefaulttimeout(min(_active_timeouts.values()))
125
153
  try:
126
154
  yield
127
155
  finally:
128
- socket.setdefaulttimeout(old_timeout)
156
+ with _timeout_lock:
157
+ del _active_timeouts[token]
158
+ if _active_timeouts:
159
+ socket.setdefaulttimeout(min(_active_timeouts.values()))
160
+ else:
161
+ if requests is not None and _original_send is not None:
162
+ requests.sessions.Session.send = _original_send
163
+ socket.setdefaulttimeout(_original_socket_timeout)
129
164
 
130
165
 
131
166
  _SECRET_PARAM_RE = re.compile(r"(?i)((?:api[_-]?key|token|secret|access[_-]?token)=)[^&\s]+")
@@ -140,28 +140,77 @@ def check_rules(symbol: str, rules: list[dict]) -> dict:
140
140
  needs_anom = any(r.get("type") == "anomaly" for r in rules)
141
141
  needs_risk = any(r.get("type") in ("risk_veto", "risk_level_at_least") for r in rules)
142
142
 
143
- # the fetches are independent subprocesses — fan out concurrently (same
144
- # ThreadPoolExecutor pattern as gather._run_all), then read back in fixed order
145
- jobs = {"quote": ("stock_data.py", ["quote", symbol])}
146
- if needs_tech:
147
- jobs["tech"] = ("technical.py", ["analyze", symbol, "--period", "daily", "--count", "60"])
148
- if needs_anom:
149
- jobs["anom"] = ("anomaly_detect.py", ["detect", symbol])
150
- if needs_risk:
151
- jobs["risk"] = ("risk_screening.py", ["screen", symbol])
152
- with ThreadPoolExecutor(max_workers=len(jobs)) as pool:
153
- futures = {key: pool.submit(_run_json, script, args) for key, (script, args) in jobs.items()}
154
- fetched = {key: future.result() for key, future in futures.items()}
143
+ needs_quote = (
144
+ needs_anom
145
+ or needs_risk
146
+ or any(r.get("type") in ("price_below", "price_above", "change_pct_above", "change_pct_below") for r in rules)
147
+ )
148
+
149
+ # Share one market snapshot while retaining 60-bar alerts and 120-bar risk analysis.
150
+ with ThreadPoolExecutor(max_workers=4) as pool:
151
+ jobs = {}
152
+ if needs_quote:
153
+ quote_job = pool.submit(_run_json, "stock_data.py", ["quote", symbol])
154
+ jobs["quote"] = quote_job
155
+ if needs_tech or needs_anom or needs_risk:
156
+ from technical import analyze_records
157
+
158
+ kline_job = pool.submit(
159
+ _run_json,
160
+ "stock_data.py",
161
+ ["kline", symbol, "--period", "daily", "--count", "120" if needs_risk else "60"],
162
+ )
163
+
164
+ def analyze(count):
165
+ records = kline_job.result()
166
+ try:
167
+ return analyze_records(symbol, records[-count:] if isinstance(records, list) else records)
168
+ except Exception as exc:
169
+ return {"error": f"Technical analysis failed ({type(exc).__name__})"}
170
+
171
+ if needs_risk:
172
+ from risk_screening import screen_risk
173
+
174
+ technical_job = pool.submit(analyze, 120)
175
+
176
+ def collect_risk():
177
+ try:
178
+ return screen_risk(
179
+ symbol, inputs={"quote": quote_job, "technical": technical_job}, run=_run_json
180
+ )
181
+ except Exception as exc:
182
+ return {"error": f"Risk analysis failed ({type(exc).__name__})"}
183
+
184
+ jobs["risk"] = pool.submit(collect_risk)
185
+ if needs_tech:
186
+ jobs["tech"] = pool.submit(analyze, 60)
187
+ if needs_anom:
188
+ from anomaly_detect import detect_anomalies
189
+
190
+ def anomalies():
191
+ records = kline_job.result()
192
+ try:
193
+ return detect_anomalies(
194
+ symbol,
195
+ records=records[-60:] if isinstance(records, list) else records,
196
+ quote=quote_job.result(),
197
+ run=_run_json,
198
+ )
199
+ except Exception as exc:
200
+ return {"error": f"Anomaly analysis failed ({type(exc).__name__})"}
201
+
202
+ jobs["anom"] = pool.submit(anomalies)
203
+ fetched = {key: future.result() for key, future in jobs.items()}
155
204
  fetched = {
156
205
  key: value if isinstance(value, dict) and "error" not in value else None for key, value in fetched.items()
157
206
  }
158
- quote = fetched["quote"] or {}
207
+ quote = fetched.get("quote") or {}
159
208
  tech = fetched.get("tech")
160
209
  anom = fetched.get("anom")
161
210
  risk = fetched.get("risk")
162
211
 
163
212
  warnings = []
164
- if not quote or quote.get("price") is None:
213
+ if needs_quote and (not quote or quote.get("price") is None):
165
214
  warnings.append("quote fetch failed or missing price — price/change rules cannot fire")
166
215
  if needs_tech and not tech:
167
216
  warnings.append("technical analysis fetch failed — volume_ratio rules cannot fire")
@@ -323,12 +323,12 @@ def detect_limit_hit(symbol: str, quote: dict) -> list:
323
323
  return anomalies
324
324
 
325
325
 
326
- def detect_capital_flow_anomaly(symbol: str) -> list:
326
+ def detect_capital_flow_anomaly(symbol: str, *, run=None) -> list:
327
327
  market = detect_market(symbol)
328
328
  if market != "A":
329
329
  return []
330
330
 
331
- flow_data = _run_tool("stock_data.py", ["capital_flow", symbol, "--mode", "detail"])
331
+ flow_data = (run or _run_tool)("stock_data.py", ["capital_flow", symbol, "--mode", "detail"])
332
332
  if not flow_data or not isinstance(flow_data, list) or len(flow_data) < 3:
333
333
  return []
334
334
 
@@ -468,8 +468,12 @@ def detect_gap(df: pd.DataFrame) -> list:
468
468
  # --------------- Orchestration ---------------
469
469
 
470
470
 
471
- def detect_anomalies(symbol: str) -> dict:
472
- records = fetch_kline(symbol, "daily", 60)
471
+ _UNSET = object()
472
+
473
+
474
+ def detect_anomalies(symbol: str, *, records=_UNSET, quote=_UNSET, run=None) -> dict:
475
+ if records is _UNSET:
476
+ records = fetch_kline(symbol, "daily", 60)
473
477
  if isinstance(records, dict) and "error" in records:
474
478
  return {"symbol": symbol, "error": records.get("error", "Failed to fetch kline")}
475
479
  if not records:
@@ -484,7 +488,8 @@ def detect_anomalies(symbol: str) -> dict:
484
488
  low = df["low"]
485
489
  volume = df["volume"]
486
490
 
487
- quote = _run_tool("stock_data.py", ["quote", symbol]) or {}
491
+ if quote is _UNSET:
492
+ quote = _run_tool("stock_data.py", ["quote", symbol]) or {}
488
493
 
489
494
  anomalies = []
490
495
  anomalies.extend(detect_macd_cross(close))
@@ -501,7 +506,7 @@ def detect_anomalies(symbol: str) -> dict:
501
506
  anomalies.extend(detect_limit_hit(symbol, quote))
502
507
  anomalies.extend(detect_large_move(quote))
503
508
 
504
- anomalies.extend(detect_capital_flow_anomaly(symbol))
509
+ anomalies.extend(detect_capital_flow_anomaly(symbol, **({"run": run} if run else {})))
505
510
 
506
511
  severity_order = {"high": 0, "medium": 1, "low": 2}
507
512
  anomalies.sort(key=lambda a: severity_order.get(a["severity"], 3))