@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/README.md +5 -2
- package/dist/index.js +368 -437
- package/index.ts +69 -2
- package/openclaw.plugin.json +2 -0
- package/package.json +3 -3
- package/scripts/profile-offline.py +127 -0
- package/scripts/profile-workflows.py +87 -0
- package/skills/growth-quality/SKILL.md +2 -0
- package/skills/stock-analysis/SKILL.md +14 -3
- package/skills/stock-analysis/references/stateless-analysis.md +16 -2
- package/skills/stock-screener/SKILL.md +4 -0
- package/skills/strategy-backtest/SKILL.md +8 -3
- package/tools/_cache.py +93 -21
- package/tools/_subproc.py +43 -8
- package/tools/alert_rules.py +63 -14
- package/tools/anomaly_detect.py +11 -6
- package/tools/backtest.py +225 -70
- package/tools/capabilities.py +1 -0
- package/tools/gather.py +63 -8
- package/tools/market_regime.py +26 -15
- package/tools/market_review.py +2 -2
- package/tools/name_resolver.py +16 -14
- package/tools/position_context.py +5 -2
- package/tools/requirements.txt +4 -4
- package/tools/risk_screening.py +40 -15
- package/tools/screener.py +44 -18
- package/tools/search_intel.py +95 -33
- package/tools/sec_holders.py +27 -25
- package/tools/signal_tracker.py +106 -54
- package/tools/stateless_analysis.py +600 -33
- package/tools/stock_data.py +366 -50
- package/tools/technical.py +42 -40
- package/tools/tiaa_disclosures.py +263 -0
- package/tools/trading_calendar.py +26 -21
- package/tools/value_data.py +6 -2
- package/tools/watchlist.py +24 -15
- package/tools/watchlist_context.py +63 -49
package/tools/search_intel.py
CHANGED
|
@@ -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=
|
|
78
|
+
timeout=_search_timeout(),
|
|
32
79
|
)
|
|
33
80
|
if not resp.ok:
|
|
34
|
-
raise
|
|
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=
|
|
104
|
+
timeout=_search_timeout(),
|
|
58
105
|
)
|
|
59
106
|
if not resp.ok:
|
|
60
|
-
raise
|
|
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=
|
|
134
|
+
timeout=_search_timeout(),
|
|
88
135
|
)
|
|
89
136
|
if not resp.ok:
|
|
90
|
-
raise
|
|
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=
|
|
160
|
+
timeout=_search_timeout(),
|
|
114
161
|
)
|
|
115
162
|
if not resp.ok:
|
|
116
|
-
raise
|
|
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
|
|
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=
|
|
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,35 +220,47 @@ 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
|
|
|
178
230
|
|
|
179
|
-
def search_news(query: str, max_results: int = 10) -> dict:
|
|
231
|
+
def search_news(query: str, max_results: int = 10, budget_seconds: int = 45) -> dict:
|
|
180
232
|
results = []
|
|
181
233
|
engines_tried = []
|
|
182
234
|
engines_available = []
|
|
183
235
|
engines_errors = {}
|
|
184
236
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
237
|
+
with _search_budget(budget_seconds) 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
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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
|
|
package/tools/sec_holders.py
CHANGED
|
@@ -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
|
|
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
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
size
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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):
|
package/tools/signal_tracker.py
CHANGED
|
@@ -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
|
-
#
|
|
70
|
-
store.
|
|
71
|
-
|
|
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,42 +129,38 @@ 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
|
|
119
|
-
|
|
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):
|
|
132
137
|
"""Append one complete JSON line; caller holds the store lock."""
|
|
133
138
|
payload = (json.dumps(record, ensure_ascii=False, default=str, allow_nan=False) + "\n").encode("utf-8")
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
+
previous = store.read_bytes() if store.exists() else b""
|
|
140
|
+
# Publish only complete records; a failed append must not leave a corrupt tail.
|
|
141
|
+
with atomic_write(store, binary=True) as stream:
|
|
142
|
+
stream.write(previous)
|
|
143
|
+
if previous and not previous.endswith(b"\n"):
|
|
144
|
+
stream.write(b"\n")
|
|
139
145
|
stream.write(payload)
|
|
140
146
|
|
|
141
147
|
|
|
142
148
|
def cmd_record(args):
|
|
143
149
|
entry_price = args.entry_price
|
|
150
|
+
price_as_of = None
|
|
144
151
|
if entry_price is None:
|
|
145
152
|
quote = run_tool("stock_data.py", ["quote", args.symbol])
|
|
146
153
|
if not isinstance(quote, dict) or "error" in quote or quote.get("price") is None:
|
|
147
154
|
return {"error": f"cannot determine entry price for {args.symbol}: quote unavailable"}
|
|
148
155
|
entry_price = quote["price"]
|
|
149
|
-
|
|
156
|
+
price_as_of = quote.get("data_as_of") if isinstance(quote.get("data_as_of"), str) else None
|
|
157
|
+
market = detect_market(args.symbol)
|
|
158
|
+
date = args.date or datetime.now(ZoneInfo(_MARKET_TIMEZONES[market])).date().isoformat()
|
|
159
|
+
price_date = _price_date(price_as_of, market) if price_as_of else None
|
|
150
160
|
record = {
|
|
151
161
|
"id": f"{date}-{args.symbol}-{uuid4().hex[:8]}",
|
|
152
162
|
"symbol": args.symbol,
|
|
153
|
-
"market":
|
|
163
|
+
"market": market,
|
|
154
164
|
"direction": args.direction,
|
|
155
165
|
"entry_price": entry_price,
|
|
156
166
|
"target_price": args.target_price,
|
|
@@ -159,12 +169,16 @@ def cmd_record(args):
|
|
|
159
169
|
"source": args.source,
|
|
160
170
|
"note": args.note,
|
|
161
171
|
"record_date": date,
|
|
172
|
+
"price_as_of": price_as_of,
|
|
173
|
+
"price_date": price_date,
|
|
162
174
|
"status": "open",
|
|
163
175
|
"outcome": None,
|
|
164
176
|
}
|
|
165
177
|
store = _store_path(args)
|
|
166
178
|
try:
|
|
167
179
|
_validate_signal(record)
|
|
180
|
+
if price_date and price_date > date:
|
|
181
|
+
raise ValueError("price_as_of cannot be later than record_date; use an explicit historical entry price")
|
|
168
182
|
with _store_lock(store):
|
|
169
183
|
_load_records(store, strict=True)
|
|
170
184
|
_append_record(store, record)
|
|
@@ -177,9 +191,9 @@ def _evaluate_bars(rec: dict, bars: list):
|
|
|
177
191
|
"""Judge one open record against daily bars after record_date.
|
|
178
192
|
|
|
179
193
|
Returns (outcome, exit_price, exit_date), or None when the record stays open
|
|
180
|
-
(fewer post-record bars than horizon_days and nothing triggered).
|
|
181
|
-
|
|
182
|
-
|
|
194
|
+
(fewer post-record bars than horizon_days and nothing triggered). Opening
|
|
195
|
+
stops fill at the open and opening targets at the target. Otherwise stop is
|
|
196
|
+
checked before target within each bar."""
|
|
183
197
|
entry = rec.get("entry_price")
|
|
184
198
|
if not isinstance(entry, (int, float)) or entry <= 0:
|
|
185
199
|
return None
|
|
@@ -192,13 +206,21 @@ def _evaluate_bars(rec: dict, bars: list):
|
|
|
192
206
|
key=lambda b: str(b.get("date", "")),
|
|
193
207
|
)
|
|
194
208
|
for i, bar in enumerate(eligible, 1):
|
|
195
|
-
high, low, close = bar.get("high"
|
|
209
|
+
opening, high, low, close = (bar.get(key) for key in ("open", "high", "low", "close"))
|
|
196
210
|
if direction == "buy":
|
|
211
|
+
hit_opening_stop = stop is not None and opening is not None and opening <= stop
|
|
212
|
+
hit_opening_target = target is not None and opening is not None and opening >= target
|
|
197
213
|
hit_stop = stop is not None and low is not None and low <= stop
|
|
198
214
|
hit_target = target is not None and high is not None and high >= target
|
|
199
215
|
else:
|
|
216
|
+
hit_opening_stop = stop is not None and opening is not None and opening >= stop
|
|
217
|
+
hit_opening_target = target is not None and opening is not None and opening <= target
|
|
200
218
|
hit_stop = stop is not None and high is not None and high >= stop
|
|
201
219
|
hit_target = target is not None and low is not None and low <= target
|
|
220
|
+
if hit_opening_stop:
|
|
221
|
+
return "stop_hit", opening, bar.get("date")
|
|
222
|
+
if hit_opening_target:
|
|
223
|
+
return "target_hit", target, bar.get("date")
|
|
202
224
|
if hit_stop:
|
|
203
225
|
return "stop_hit", stop, bar.get("date")
|
|
204
226
|
if hit_target:
|
|
@@ -222,25 +244,53 @@ def _kline_count(rec: dict) -> int:
|
|
|
222
244
|
return min(max(horizon + 40, elapsed + 15), 800)
|
|
223
245
|
|
|
224
246
|
|
|
225
|
-
def _valid_bar(bar) -> bool:
|
|
247
|
+
def _valid_bar(bar, require_open=True) -> bool:
|
|
226
248
|
if not isinstance(bar, dict):
|
|
227
249
|
return False
|
|
228
250
|
try:
|
|
229
251
|
date = datetime.strptime(bar["date"], "%Y-%m-%d")
|
|
230
252
|
except (KeyError, TypeError, ValueError):
|
|
231
253
|
return False
|
|
254
|
+
keys = ("open", "high", "low", "close") if require_open or bar.get("open") is not None else ("high", "low", "close")
|
|
232
255
|
return date.strftime("%Y-%m-%d") == bar["date"] and all(
|
|
233
|
-
isinstance(bar.get(key),
|
|
234
|
-
|
|
256
|
+
not isinstance(bar.get(key), bool)
|
|
257
|
+
and isinstance(bar.get(key), (int, float))
|
|
258
|
+
and math.isfinite(bar[key])
|
|
259
|
+
and bar[key] > 0
|
|
260
|
+
for key in keys
|
|
235
261
|
)
|
|
236
262
|
|
|
237
263
|
|
|
238
264
|
def _price_factor(rec, bars):
|
|
265
|
+
market = rec.get("market") or detect_market(rec["symbol"])
|
|
266
|
+
price_date = _price_date(rec.get("price_date") or rec.get("price_as_of"), market)
|
|
267
|
+
if price_date and price_date > rec["record_date"]:
|
|
268
|
+
return None
|
|
239
269
|
if all(bar.get("adjusted") == "none" for bar in bars):
|
|
240
270
|
return 1.0
|
|
241
|
-
|
|
242
|
-
|
|
271
|
+
anchor_date = price_date or rec["record_date"]
|
|
272
|
+
has_price_date = price_date is not None
|
|
273
|
+
anchor_idx = next((i for i, bar in enumerate(bars) if bar["date"] == anchor_date), None)
|
|
274
|
+
if anchor_idx is None:
|
|
275
|
+
anchor_idx = next((i for i in range(len(bars) - 1, -1, -1) if bars[i]["date"] <= anchor_date), None)
|
|
276
|
+
if anchor_idx is None:
|
|
277
|
+
return None
|
|
278
|
+
next_bar = next((bar for bar in bars if bar["date"] > anchor_date), None)
|
|
279
|
+
if (
|
|
280
|
+
not has_price_date
|
|
281
|
+
and next_bar is not None
|
|
282
|
+
and next_bar.get("adjustment_factor") != bars[anchor_idx].get("adjustment_factor")
|
|
283
|
+
):
|
|
284
|
+
return None
|
|
285
|
+
elif (
|
|
286
|
+
anchor_idx
|
|
287
|
+
and not has_price_date
|
|
288
|
+
and bars[anchor_idx - 1].get("adjustment_factor") != bars[anchor_idx].get("adjustment_factor")
|
|
289
|
+
):
|
|
290
|
+
return None
|
|
291
|
+
anchor = bars[anchor_idx]
|
|
243
292
|
factor = anchor.get("adjustment_factor")
|
|
293
|
+
basis = anchor.get("adjusted")
|
|
244
294
|
if (
|
|
245
295
|
basis in ("qfq", "total_return")
|
|
246
296
|
and all(bar.get("adjusted") == basis for bar in bars)
|
|
@@ -284,7 +334,8 @@ def cmd_evaluate(args):
|
|
|
284
334
|
if (
|
|
285
335
|
not isinstance(bars, list)
|
|
286
336
|
or not bars
|
|
287
|
-
or any(not _valid_bar(bar) for bar in bars)
|
|
337
|
+
or any(not _valid_bar(bar, require_open=rec.get("stop_price") is not None) for bar in bars)
|
|
338
|
+
or len({bar["date"] for bar in bars}) != len(bars)
|
|
288
339
|
or min(str(b.get("date", ""))[:10] for b in bars) > rec["record_date"]
|
|
289
340
|
):
|
|
290
341
|
entry.update({"outcome": "skipped_no_data", "return_pct": None, "exit_date": None})
|
|
@@ -296,16 +347,17 @@ def cmd_evaluate(args):
|
|
|
296
347
|
warning="Adjusted or unknown prices lack a verified conversion to the original signal prices",
|
|
297
348
|
)
|
|
298
349
|
else:
|
|
350
|
+
decimal_factor = Decimal(str(factor))
|
|
299
351
|
priced = dict(rec)
|
|
300
352
|
for key in ("entry_price", "target_price", "stop_price"):
|
|
301
353
|
if priced.get(key) is not None:
|
|
302
|
-
priced[key]
|
|
354
|
+
priced[key] = float(Decimal(str(priced[key])) * decimal_factor)
|
|
303
355
|
ev = _evaluate_bars(priced, bars)
|
|
304
356
|
if ev is None:
|
|
305
357
|
entry.update({"outcome": "open", "return_pct": None, "exit_date": None})
|
|
306
358
|
else:
|
|
307
359
|
outcome, exit_price, exit_date = ev
|
|
308
|
-
exit_price
|
|
360
|
+
exit_price = float(Decimal(str(exit_price)) / decimal_factor)
|
|
309
361
|
ret = round((exit_price - rec["entry_price"]) / rec["entry_price"] * 100, 2)
|
|
310
362
|
if rec.get("direction") == "sell":
|
|
311
363
|
ret = -ret
|