@weaxs/dsh-stock-analysis 0.2.4 → 0.2.6

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.
@@ -10,9 +10,12 @@ host decides when to record and when to evaluate.
10
10
  """
11
11
 
12
12
  import argparse
13
+ import contextlib
13
14
  import json
15
+ import math
14
16
  import os
15
17
  import sys
18
+ import tempfile
16
19
  from datetime import datetime
17
20
  from pathlib import Path
18
21
  from uuid import uuid4
@@ -31,26 +34,108 @@ def _store_path(args) -> Path:
31
34
  return Path(args.store) if args.store else _default_store()
32
35
 
33
36
 
34
- def _load_records(store: Path) -> list:
37
+ def _validate_signal(record):
38
+ if not isinstance(record, dict) or any(
39
+ not isinstance(record.get(field), str) or not record[field] for field in ("id", "symbol", "record_date")
40
+ ):
41
+ raise ValueError("Invalid signal identity/date")
42
+ if record.get("status") not in ("open", "closed") or record.get("direction") not in ("buy", "sell"):
43
+ raise ValueError("Invalid signal status/direction")
44
+ if record.get("source") is not None and not isinstance(record["source"], str):
45
+ raise ValueError("Signal source must be a string")
46
+ if record.get("entry_price") is None or any(
47
+ isinstance(record.get(field), bool)
48
+ or not isinstance(record[field], (int, float))
49
+ or not math.isfinite(record[field])
50
+ or record[field] <= 0
51
+ for field in ("entry_price", "target_price", "stop_price")
52
+ if record.get(field) is not None
53
+ ):
54
+ raise ValueError("Signal prices must be finite and positive")
55
+ horizon = record.get("horizon_days")
56
+ if isinstance(horizon, bool) or not isinstance(horizon, int) or horizon <= 0:
57
+ raise ValueError("Signal horizon_days must be a positive integer")
58
+ try:
59
+ date = datetime.strptime(record["record_date"], "%Y-%m-%d")
60
+ except ValueError as e:
61
+ raise ValueError("Signal date must be YYYY-MM-DD") from e
62
+ if date.strftime("%Y-%m-%d") != record["record_date"]:
63
+ raise ValueError("Signal date must be YYYY-MM-DD")
64
+
65
+
66
+ @contextlib.contextmanager
67
+ def _store_lock(store: Path):
68
+ # Lock a separate file: replacing the JSONL must not replace the lock inode.
69
+ store.parent.mkdir(parents=True, exist_ok=True)
70
+ with store.with_name(store.name + ".lock").open("a+b") as lock:
71
+ if sys.platform == "win32":
72
+ import msvcrt
73
+
74
+ if lock.seek(0, os.SEEK_END) == 0:
75
+ lock.write(b"\0")
76
+ lock.flush()
77
+ lock.seek(0)
78
+ msvcrt.locking(lock.fileno(), msvcrt.LK_LOCK, 1)
79
+ else:
80
+ import fcntl
81
+
82
+ fcntl.flock(lock.fileno(), fcntl.LOCK_EX)
83
+ try:
84
+ yield
85
+ finally:
86
+ if sys.platform == "win32":
87
+ lock.seek(0)
88
+ msvcrt.locking(lock.fileno(), msvcrt.LK_UNLCK, 1)
89
+ else:
90
+ fcntl.flock(lock.fileno(), fcntl.LOCK_UN)
91
+
92
+
93
+ def _load_records(store: Path, strict: bool = False) -> list:
35
94
  if not store.exists():
36
95
  return []
37
96
  records = []
38
- for line in store.read_text(encoding="utf-8").splitlines():
97
+ for number, line in enumerate(store.read_text(encoding="utf-8").splitlines(), 1):
39
98
  line = line.strip()
40
99
  if line:
41
100
  try:
42
- records.append(json.loads(line))
43
- except json.JSONDecodeError:
101
+ record = json.loads(line)
102
+ except json.JSONDecodeError as e:
103
+ if strict:
104
+ raise ValueError(f"Invalid signal JSON at line {number}; store left unchanged") from e
44
105
  continue
106
+ if strict:
107
+ try:
108
+ _validate_signal(record)
109
+ except ValueError as e:
110
+ raise ValueError(f"Invalid signal record at line {number}; store left unchanged: {e}") from e
111
+ records.append(record)
45
112
  return records
46
113
 
47
114
 
48
115
  def _save_records(store: Path, records: list):
49
- """Atomic rewrite: write <store>.tmp, then os.replace onto the store."""
116
+ """Atomic rewrite; caller holds the store lock."""
50
117
  store.parent.mkdir(parents=True, exist_ok=True)
51
- tmp = store.parent / (store.name + ".tmp")
52
- tmp.write_text("".join(json.dumps(r, ensure_ascii=False) + "\n" for r in records), encoding="utf-8")
53
- os.replace(tmp, store)
118
+ tmp = None
119
+ try:
120
+ with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", dir=store.parent, delete=False) as stream:
121
+ tmp = Path(stream.name)
122
+ stream.write("".join(json.dumps(r, ensure_ascii=False) + "\n" for r in records))
123
+ os.replace(tmp, store)
124
+ finally:
125
+ if tmp:
126
+ with contextlib.suppress(OSError):
127
+ tmp.unlink(missing_ok=True)
128
+
129
+
130
+ def _append_record(store: Path, record: dict):
131
+ """Append one complete JSON line; caller holds the store lock."""
132
+ payload = (json.dumps(record, ensure_ascii=False, default=str, allow_nan=False) + "\n").encode("utf-8")
133
+ with store.open("a+b") as stream:
134
+ if stream.seek(0, os.SEEK_END):
135
+ stream.seek(-1, os.SEEK_END)
136
+ if stream.read(1) != b"\n":
137
+ stream.write(b"\n")
138
+ stream.write(payload)
54
139
 
55
140
 
56
141
  def cmd_record(args):
@@ -78,10 +163,11 @@ def cmd_record(args):
78
163
  }
79
164
  store = _store_path(args)
80
165
  try:
81
- store.parent.mkdir(parents=True, exist_ok=True)
82
- with store.open("a", encoding="utf-8") as f:
83
- f.write(json.dumps(record, ensure_ascii=False) + "\n")
84
- except OSError as e:
166
+ _validate_signal(record)
167
+ with _store_lock(store):
168
+ _load_records(store, strict=True)
169
+ _append_record(store, record)
170
+ except (OSError, ValueError) as e:
85
171
  return {"error": str(e)}
86
172
  return record
87
173
 
@@ -135,9 +221,26 @@ def _kline_count(rec: dict) -> int:
135
221
  return min(max(horizon + 40, elapsed + 15), 800)
136
222
 
137
223
 
224
+ def _valid_bar(bar) -> bool:
225
+ if not isinstance(bar, dict):
226
+ return False
227
+ try:
228
+ date = datetime.strptime(bar["date"], "%Y-%m-%d")
229
+ except (KeyError, TypeError, ValueError):
230
+ return False
231
+ return date.strftime("%Y-%m-%d") == bar["date"] and all(
232
+ isinstance(bar.get(key), (int, float)) and math.isfinite(bar[key]) and bar[key] > 0
233
+ for key in ("high", "low", "close")
234
+ )
235
+
236
+
138
237
  def cmd_evaluate(args):
139
238
  store = _store_path(args)
140
- records = _load_records(store)
239
+ try:
240
+ with _store_lock(store):
241
+ records = _load_records(store, strict=True)
242
+ except (OSError, ValueError) as e:
243
+ return {"error": str(e)}
141
244
  results = []
142
245
  evaluated = closed = 0
143
246
  changed = False
@@ -152,7 +255,12 @@ def cmd_evaluate(args):
152
255
  "stock_data.py",
153
256
  ["kline", rec["symbol"], "--period", "daily", "--count", str(_kline_count(rec))],
154
257
  )
155
- if not isinstance(bars, list):
258
+ if (
259
+ not isinstance(bars, list)
260
+ or not bars
261
+ or any(not _valid_bar(bar) for bar in bars)
262
+ or min(str(b.get("date", ""))[:10] for b in bars) > rec["record_date"]
263
+ ):
156
264
  entry.update({"outcome": "skipped_no_data", "return_pct": None, "exit_date": None})
157
265
  else:
158
266
  ev = _evaluate_bars(rec, bars)
@@ -172,20 +280,35 @@ def cmd_evaluate(args):
172
280
  "return_pct": ret,
173
281
  }
174
282
  )
175
- closed += 1
176
283
  changed = True
177
284
  entry.update({"outcome": outcome, "return_pct": ret, "exit_date": exit_date})
178
285
  results.append(entry)
179
- if changed:
286
+ if results:
180
287
  try:
181
- _save_records(store, records)
182
- except OSError as e:
288
+ updates = {r["id"]: r for r in records if r.get("status") == "closed"}
289
+ with _store_lock(store):
290
+ current = _load_records(store, strict=True)
291
+ merged = [updates.get(r["id"], r) if r.get("status") == "open" else r for r in current]
292
+ if changed:
293
+ _save_records(store, merged)
294
+ persisted = {r["id"]: r for r in merged}
295
+ for entry in results:
296
+ rec = persisted[entry["id"]]
297
+ if rec.get("status") == "closed":
298
+ entry.update({key: rec.get(key) for key in ("outcome", "return_pct", "exit_date")})
299
+ closed += 1
300
+ except (OSError, ValueError) as e:
183
301
  return {"error": str(e)}
184
302
  return {"evaluated": evaluated, "closed": closed, "still_open": evaluated - closed, "results": results}
185
303
 
186
304
 
187
305
  def cmd_summary(args):
188
- records = _load_records(_store_path(args))
306
+ store = _store_path(args)
307
+ try:
308
+ with _store_lock(store):
309
+ records = _load_records(store, strict=True)
310
+ except (OSError, ValueError) as e:
311
+ return {"error": str(e)}
189
312
  filtered = records
190
313
  if args.source:
191
314
  filtered = [r for r in filtered if r.get("source") == args.source]