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.
@@ -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):
@@ -15,16 +15,41 @@ import json
15
15
  import math
16
16
  import os
17
17
  import sys
18
- import tempfile
19
18
  import time
20
19
  from datetime import datetime
20
+ from decimal import Decimal
21
21
  from pathlib import Path
22
22
  from uuid import uuid4
23
+ from zoneinfo import ZoneInfo
23
24
 
24
25
  sys.path.insert(0, str(Path(__file__).resolve().parent))
26
+ from _cache import atomic_write, file_lock
25
27
  from _subproc import command_timeout, run_tool, utf8_stdio # noqa: E402
26
28
  from stock_data import detect_market # noqa: E402
27
29
 
30
+ _MARKET_TIMEZONES = {
31
+ "A": "Asia/Shanghai",
32
+ "HK": "Asia/Hong_Kong",
33
+ "US": "America/New_York",
34
+ "JP": "Asia/Tokyo",
35
+ "KR": "Asia/Seoul",
36
+ "TW": "Asia/Taipei",
37
+ }
38
+
39
+
40
+ def _price_date(value, market):
41
+ if not isinstance(value, str) or not value:
42
+ return None
43
+ try:
44
+ if len(value) == 10:
45
+ return datetime.strptime(value, "%Y-%m-%d").date().isoformat()
46
+ timestamp = datetime.fromisoformat(value.replace("Z", "+00:00"))
47
+ if timestamp.tzinfo is None:
48
+ return None
49
+ return timestamp.astimezone(ZoneInfo(_MARKET_TIMEZONES[market])).date().isoformat()
50
+ except (ValueError, KeyError, OverflowError):
51
+ return None
52
+
28
53
 
29
54
  def _default_store() -> Path:
30
55
  env = os.environ.get("STOCK_SIGNAL_STORE")
@@ -53,6 +78,15 @@ def _validate_signal(record):
53
78
  if record.get(field) is not None
54
79
  ):
55
80
  raise ValueError("Signal prices must be finite and positive")
81
+ entry = record["entry_price"]
82
+ target = record.get("target_price")
83
+ stop = record.get("stop_price")
84
+ if record["direction"] == "buy":
85
+ valid_prices = (target is None or target > entry) and (stop is None or stop < entry)
86
+ else:
87
+ valid_prices = (target is None or target < entry) and (stop is None or stop > entry)
88
+ if not valid_prices:
89
+ raise ValueError("Signal target/stop prices conflict with direction")
56
90
  horizon = record.get("horizon_days")
57
91
  if isinstance(horizon, bool) or not isinstance(horizon, int) or horizon <= 0:
58
92
  raise ValueError("Signal horizon_days must be a positive integer")
@@ -66,29 +100,9 @@ def _validate_signal(record):
66
100
 
67
101
  @contextlib.contextmanager
68
102
  def _store_lock(store: Path):
69
- # Lock a separate file: replacing the JSONL must not replace the lock inode.
70
- store.parent.mkdir(parents=True, exist_ok=True)
71
- with store.with_name(store.name + ".lock").open("a+b") as lock:
72
- if sys.platform == "win32":
73
- import msvcrt
74
-
75
- if lock.seek(0, os.SEEK_END) == 0:
76
- lock.write(b"\0")
77
- lock.flush()
78
- lock.seek(0)
79
- msvcrt.locking(lock.fileno(), msvcrt.LK_LOCK, 1)
80
- else:
81
- import fcntl
82
-
83
- fcntl.flock(lock.fileno(), fcntl.LOCK_EX)
84
- try:
85
- yield
86
- finally:
87
- if sys.platform == "win32":
88
- lock.seek(0)
89
- msvcrt.locking(lock.fileno(), msvcrt.LK_UNLCK, 1)
90
- else:
91
- fcntl.flock(lock.fileno(), fcntl.LOCK_UN)
103
+ # Replacing the JSONL must not replace the lock inode.
104
+ with file_lock(store.with_name(store.name + ".lock")):
105
+ yield
92
106
 
93
107
 
94
108
  def _load_records(store: Path, strict: bool = False) -> list:
@@ -115,17 +129,8 @@ def _load_records(store: Path, strict: bool = False) -> list:
115
129
 
116
130
  def _save_records(store: Path, records: list):
117
131
  """Atomic rewrite; caller holds the store lock."""
118
- store.parent.mkdir(parents=True, exist_ok=True)
119
- tmp = None
120
- try:
121
- with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", dir=store.parent, delete=False) as stream:
122
- tmp = Path(stream.name)
123
- stream.write("".join(json.dumps(r, ensure_ascii=False) + "\n" for r in records))
124
- os.replace(tmp, store)
125
- finally:
126
- if tmp:
127
- with contextlib.suppress(OSError):
128
- tmp.unlink(missing_ok=True)
132
+ with atomic_write(store) as stream:
133
+ stream.write("".join(json.dumps(r, ensure_ascii=False) + "\n" for r in records))
129
134
 
130
135
 
131
136
  def _append_record(store: Path, record: dict):
@@ -141,16 +146,20 @@ def _append_record(store: Path, record: dict):
141
146
 
142
147
  def cmd_record(args):
143
148
  entry_price = args.entry_price
149
+ price_as_of = None
144
150
  if entry_price is None:
145
151
  quote = run_tool("stock_data.py", ["quote", args.symbol])
146
152
  if not isinstance(quote, dict) or "error" in quote or quote.get("price") is None:
147
153
  return {"error": f"cannot determine entry price for {args.symbol}: quote unavailable"}
148
154
  entry_price = quote["price"]
149
- date = args.date or datetime.now().strftime("%Y-%m-%d")
155
+ price_as_of = quote.get("data_as_of") if isinstance(quote.get("data_as_of"), str) else None
156
+ market = detect_market(args.symbol)
157
+ date = args.date or datetime.now(ZoneInfo(_MARKET_TIMEZONES[market])).date().isoformat()
158
+ price_date = _price_date(price_as_of, market) if price_as_of else None
150
159
  record = {
151
160
  "id": f"{date}-{args.symbol}-{uuid4().hex[:8]}",
152
161
  "symbol": args.symbol,
153
- "market": detect_market(args.symbol),
162
+ "market": market,
154
163
  "direction": args.direction,
155
164
  "entry_price": entry_price,
156
165
  "target_price": args.target_price,
@@ -159,12 +168,16 @@ def cmd_record(args):
159
168
  "source": args.source,
160
169
  "note": args.note,
161
170
  "record_date": date,
171
+ "price_as_of": price_as_of,
172
+ "price_date": price_date,
162
173
  "status": "open",
163
174
  "outcome": None,
164
175
  }
165
176
  store = _store_path(args)
166
177
  try:
167
178
  _validate_signal(record)
179
+ if price_date and price_date > date:
180
+ raise ValueError("price_as_of cannot be later than record_date; use an explicit historical entry price")
168
181
  with _store_lock(store):
169
182
  _load_records(store, strict=True)
170
183
  _append_record(store, record)
@@ -177,9 +190,9 @@ def _evaluate_bars(rec: dict, bars: list):
177
190
  """Judge one open record against daily bars after record_date.
178
191
 
179
192
  Returns (outcome, exit_price, exit_date), or None when the record stays open
180
- (fewer post-record bars than horizon_days and nothing triggered). Stop is
181
- checked before target within each bar — when both trigger on the same day
182
- the conservative stop_hit wins."""
193
+ (fewer post-record bars than horizon_days and nothing triggered). Opening
194
+ stops fill at the open and opening targets at the target. Otherwise stop is
195
+ checked before target within each bar."""
183
196
  entry = rec.get("entry_price")
184
197
  if not isinstance(entry, (int, float)) or entry <= 0:
185
198
  return None
@@ -192,13 +205,21 @@ def _evaluate_bars(rec: dict, bars: list):
192
205
  key=lambda b: str(b.get("date", "")),
193
206
  )
194
207
  for i, bar in enumerate(eligible, 1):
195
- high, low, close = bar.get("high"), bar.get("low"), bar.get("close")
208
+ opening, high, low, close = (bar.get(key) for key in ("open", "high", "low", "close"))
196
209
  if direction == "buy":
210
+ hit_opening_stop = stop is not None and opening is not None and opening <= stop
211
+ hit_opening_target = target is not None and opening is not None and opening >= target
197
212
  hit_stop = stop is not None and low is not None and low <= stop
198
213
  hit_target = target is not None and high is not None and high >= target
199
214
  else:
215
+ hit_opening_stop = stop is not None and opening is not None and opening >= stop
216
+ hit_opening_target = target is not None and opening is not None and opening <= target
200
217
  hit_stop = stop is not None and high is not None and high >= stop
201
218
  hit_target = target is not None and low is not None and low <= target
219
+ if hit_opening_stop:
220
+ return "stop_hit", opening, bar.get("date")
221
+ if hit_opening_target:
222
+ return "target_hit", target, bar.get("date")
202
223
  if hit_stop:
203
224
  return "stop_hit", stop, bar.get("date")
204
225
  if hit_target:
@@ -222,25 +243,53 @@ def _kline_count(rec: dict) -> int:
222
243
  return min(max(horizon + 40, elapsed + 15), 800)
223
244
 
224
245
 
225
- def _valid_bar(bar) -> bool:
246
+ def _valid_bar(bar, require_open=True) -> bool:
226
247
  if not isinstance(bar, dict):
227
248
  return False
228
249
  try:
229
250
  date = datetime.strptime(bar["date"], "%Y-%m-%d")
230
251
  except (KeyError, TypeError, ValueError):
231
252
  return False
253
+ keys = ("open", "high", "low", "close") if require_open or bar.get("open") is not None else ("high", "low", "close")
232
254
  return date.strftime("%Y-%m-%d") == bar["date"] and all(
233
- isinstance(bar.get(key), (int, float)) and math.isfinite(bar[key]) and bar[key] > 0
234
- for key in ("high", "low", "close")
255
+ not isinstance(bar.get(key), bool)
256
+ and isinstance(bar.get(key), (int, float))
257
+ and math.isfinite(bar[key])
258
+ and bar[key] > 0
259
+ for key in keys
235
260
  )
236
261
 
237
262
 
238
263
  def _price_factor(rec, bars):
264
+ market = rec.get("market") or detect_market(rec["symbol"])
265
+ price_date = _price_date(rec.get("price_date") or rec.get("price_as_of"), market)
266
+ if price_date and price_date > rec["record_date"]:
267
+ return None
239
268
  if all(bar.get("adjusted") == "none" for bar in bars):
240
269
  return 1.0
241
- anchor = next((bar for bar in bars if bar["date"] == rec["record_date"]), {})
242
- basis = anchor.get("adjusted")
270
+ anchor_date = price_date or rec["record_date"]
271
+ has_price_date = price_date is not None
272
+ anchor_idx = next((i for i, bar in enumerate(bars) if bar["date"] == anchor_date), None)
273
+ if anchor_idx is None:
274
+ anchor_idx = next((i for i in range(len(bars) - 1, -1, -1) if bars[i]["date"] <= anchor_date), None)
275
+ if anchor_idx is None:
276
+ return None
277
+ next_bar = next((bar for bar in bars if bar["date"] > anchor_date), None)
278
+ if (
279
+ not has_price_date
280
+ and next_bar is not None
281
+ and next_bar.get("adjustment_factor") != bars[anchor_idx].get("adjustment_factor")
282
+ ):
283
+ return None
284
+ elif (
285
+ anchor_idx
286
+ and not has_price_date
287
+ and bars[anchor_idx - 1].get("adjustment_factor") != bars[anchor_idx].get("adjustment_factor")
288
+ ):
289
+ return None
290
+ anchor = bars[anchor_idx]
243
291
  factor = anchor.get("adjustment_factor")
292
+ basis = anchor.get("adjusted")
244
293
  if (
245
294
  basis in ("qfq", "total_return")
246
295
  and all(bar.get("adjusted") == basis for bar in bars)
@@ -284,7 +333,8 @@ def cmd_evaluate(args):
284
333
  if (
285
334
  not isinstance(bars, list)
286
335
  or not bars
287
- or any(not _valid_bar(bar) for bar in bars)
336
+ or any(not _valid_bar(bar, require_open=rec.get("stop_price") is not None) for bar in bars)
337
+ or len({bar["date"] for bar in bars}) != len(bars)
288
338
  or min(str(b.get("date", ""))[:10] for b in bars) > rec["record_date"]
289
339
  ):
290
340
  entry.update({"outcome": "skipped_no_data", "return_pct": None, "exit_date": None})
@@ -296,16 +346,17 @@ def cmd_evaluate(args):
296
346
  warning="Adjusted or unknown prices lack a verified conversion to the original signal prices",
297
347
  )
298
348
  else:
349
+ decimal_factor = Decimal(str(factor))
299
350
  priced = dict(rec)
300
351
  for key in ("entry_price", "target_price", "stop_price"):
301
352
  if priced.get(key) is not None:
302
- priced[key] *= factor
353
+ priced[key] = float(Decimal(str(priced[key])) * decimal_factor)
303
354
  ev = _evaluate_bars(priced, bars)
304
355
  if ev is None:
305
356
  entry.update({"outcome": "open", "return_pct": None, "exit_date": None})
306
357
  else:
307
358
  outcome, exit_price, exit_date = ev
308
- exit_price /= factor
359
+ exit_price = float(Decimal(str(exit_price)) / decimal_factor)
309
360
  ret = round((exit_price - rec["entry_price"]) / rec["entry_price"] * 100, 2)
310
361
  if rec.get("direction") == "sell":
311
362
  ret = -ret