@weaxs/openclaw-stock-analysis 0.2.5 → 0.2.7
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 +1 -1
- package/dist/index.js +322 -21
- package/index.ts +257 -16
- package/openclaw.plugin.json +11 -1
- package/package.json +1 -1
- package/schemas/market_review_schema.json +12 -5
- package/schemas/report_schema.json +68 -4
- package/scripts/cli-timeout.mjs +27 -0
- package/scripts/json-input.mjs +26 -0
- package/scripts/setup-python.mjs +6 -6
- package/skills/bull-trend/SKILL.md +34 -10
- package/skills/bull-trend/references/fat-tails.md +91 -0
- package/skills/bull-trend/scripts/tail_analysis.py +132 -0
- package/skills/event-driven/SKILL.md +6 -0
- package/skills/growth-quality/SKILL.md +6 -0
- package/skills/market-review/SKILL.md +2 -0
- package/skills/stock-analysis/SKILL.md +25 -5
- package/skills/stock-analysis/references/stateless-analysis.md +66 -0
- package/skills/stock-screener/SKILL.md +7 -1
- package/skills/strategy-backtest/SKILL.md +13 -0
- package/skills/wisburg-research/SKILL.md +2 -0
- package/templates/market_review/full.md.j2 +7 -0
- package/templates/stock_analysis/_evidence.md.j2 +11 -0
- package/templates/stock_analysis/brief.md.j2 +6 -1
- package/templates/stock_analysis/full.md.j2 +8 -3
- package/tools/_subproc.py +66 -11
- package/tools/alert_rules.py +32 -4
- package/tools/backtest.py +95 -18
- package/tools/capabilities.py +11 -0
- package/tools/cli_budgets.json +26 -0
- package/tools/gather.py +24 -16
- package/tools/lixinger.py +100 -0
- package/tools/market_regime.py +7 -3
- package/tools/market_review.py +37 -15
- package/tools/position_context.py +20 -2
- package/tools/report_renderer.py +2 -0
- package/tools/risk_screening.py +45 -17
- package/tools/screener.py +74 -24
- package/tools/sec_edgar.py +346 -0
- package/tools/sec_holders.py +263 -0
- package/tools/signal_tracker.py +218 -24
- package/tools/stateless_analysis.py +630 -0
- package/tools/stock_data.py +170 -32
- package/tools/technical.py +16 -2
- package/tools/value_data.py +159 -0
- package/tools/watchlist.py +24 -1
- package/tools/watchlist_context.py +25 -10
|
@@ -0,0 +1,630 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Explicit-input research calculations. No account, watchlist or history storage."""
|
|
3
|
+
|
|
4
|
+
import argparse
|
|
5
|
+
import json
|
|
6
|
+
import math
|
|
7
|
+
import sys
|
|
8
|
+
from bisect import bisect_left
|
|
9
|
+
from copy import deepcopy
|
|
10
|
+
from datetime import date
|
|
11
|
+
from difflib import SequenceMatcher
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
from _subproc import utf8_stdio
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def number(value, name, minimum=None, maximum=None):
|
|
18
|
+
if isinstance(value, bool) or not isinstance(value, (int, float)) or not math.isfinite(value):
|
|
19
|
+
raise ValueError(f"{name} must be a finite number")
|
|
20
|
+
if minimum is not None and value < minimum or maximum is not None and value > maximum:
|
|
21
|
+
raise ValueError(f"{name} outside allowed range")
|
|
22
|
+
return value
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def positive(value, name):
|
|
26
|
+
value = number(value, name, minimum=0)
|
|
27
|
+
if value == 0:
|
|
28
|
+
raise ValueError(f"{name} must be positive")
|
|
29
|
+
return value
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def integer(value, name, minimum, maximum):
|
|
33
|
+
value = number(value, name, minimum, maximum)
|
|
34
|
+
if int(value) != value:
|
|
35
|
+
raise ValueError(f"{name} must be an integer")
|
|
36
|
+
return int(value)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def position_size(p):
|
|
40
|
+
equity = positive(p["equity"], "equity")
|
|
41
|
+
cash = number(p["cash"], "cash", 0, equity)
|
|
42
|
+
entry = positive(p["entry_price"], "entry_price")
|
|
43
|
+
stop = positive(p["stop_price"], "stop_price")
|
|
44
|
+
if stop >= entry:
|
|
45
|
+
raise ValueError("long position requires stop_price < entry_price")
|
|
46
|
+
risk = number(p["risk_fraction"], "risk_fraction", 0, 1)
|
|
47
|
+
cap = number(p.get("max_position_fraction", 1), "max_position_fraction", 0, 1)
|
|
48
|
+
lot = integer(p["lot_size"], "lot_size", 1, 1000000)
|
|
49
|
+
fee = number(p.get("fee_rate", 0), "fee_rate", 0, 0.1)
|
|
50
|
+
slip = number(p.get("slippage_rate", 0), "slippage_rate", 0, 0.1)
|
|
51
|
+
buy = entry * (1 + slip) * (1 + fee)
|
|
52
|
+
sell = stop * (1 - slip) * (1 - fee)
|
|
53
|
+
loss = buy - sell
|
|
54
|
+
limits = {
|
|
55
|
+
"risk": math.floor(equity * risk / loss / lot) * lot,
|
|
56
|
+
"cash": math.floor(cash / buy / lot) * lot,
|
|
57
|
+
"position": math.floor(equity * cap / buy / lot) * lot,
|
|
58
|
+
}
|
|
59
|
+
quantity = min(limits.values())
|
|
60
|
+
return {
|
|
61
|
+
"quantity": quantity,
|
|
62
|
+
"risk_budget": equity * risk,
|
|
63
|
+
"cash_required": round(quantity * buy, 6),
|
|
64
|
+
"planned_loss": round(quantity * loss, 6),
|
|
65
|
+
"position_fraction": quantity * buy / equity,
|
|
66
|
+
"limits": limits,
|
|
67
|
+
"binding_constraints": [key for key, value in limits.items() if value == quantity],
|
|
68
|
+
"warnings": [
|
|
69
|
+
"Long-only scenario; stop fills are not guaranteed. Rates exclude unprovided taxes/minimum fees; all amounts must share one currency."
|
|
70
|
+
],
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def text(value, name):
|
|
75
|
+
if not isinstance(value, str) or not value.strip():
|
|
76
|
+
raise ValueError(f"{name} must be nonempty text")
|
|
77
|
+
return value.strip()
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def rows(value, name, minimum=1, maximum=100):
|
|
81
|
+
if not isinstance(value, list) or not minimum <= len(value) <= maximum:
|
|
82
|
+
raise ValueError(f"{name} has invalid row count")
|
|
83
|
+
if not all(isinstance(row, dict) for row in value):
|
|
84
|
+
raise ValueError(f"{name} must contain objects")
|
|
85
|
+
return value
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def prices(value):
|
|
89
|
+
result = {}
|
|
90
|
+
for row in rows(value, "prices", 2, 20000):
|
|
91
|
+
day = date.fromisoformat(row["date"]).isoformat()
|
|
92
|
+
if day != row["date"] or day in result:
|
|
93
|
+
raise ValueError("price dates must be unique YYYY-MM-DD")
|
|
94
|
+
result[day] = positive(row["close"], "close")
|
|
95
|
+
return dict(sorted(result.items()))
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def portfolio(p):
|
|
99
|
+
base = text(p["base_currency"], "base_currency")
|
|
100
|
+
cash = number(p["cash"], "cash", 0)
|
|
101
|
+
rates = {base: 1}
|
|
102
|
+
for row in rows(p.get("fx_rates", []), "fx_rates", 0):
|
|
103
|
+
currency = text(row["currency"], "currency")
|
|
104
|
+
if currency in rates:
|
|
105
|
+
raise ValueError("duplicate/base FX currency")
|
|
106
|
+
rates[currency] = positive(row["rate"], "rate")
|
|
107
|
+
positions = []
|
|
108
|
+
currencies, sectors = {}, {}
|
|
109
|
+
seen, dates = set(), set()
|
|
110
|
+
pnl, stop_risk = 0, 0
|
|
111
|
+
pnl_complete = stop_complete = True
|
|
112
|
+
for row in rows(p["positions"], "positions"):
|
|
113
|
+
symbol = text(row["symbol"], "symbol")
|
|
114
|
+
if symbol in seen:
|
|
115
|
+
raise ValueError("aggregate duplicate positions before analysis")
|
|
116
|
+
seen.add(symbol)
|
|
117
|
+
currency = text(row["currency"], "currency")
|
|
118
|
+
rate = rates[currency]
|
|
119
|
+
quantity = positive(row["quantity"], "quantity")
|
|
120
|
+
price = positive(row["price"], "price")
|
|
121
|
+
value = price * quantity * rate
|
|
122
|
+
cost = positive(row["cost"], "cost") if row.get("cost") is not None else None
|
|
123
|
+
stop = positive(row["stop_price"], "stop_price") if row.get("stop_price") is not None else None
|
|
124
|
+
pnl_complete &= cost is not None
|
|
125
|
+
stop_complete &= stop is not None
|
|
126
|
+
if cost is not None:
|
|
127
|
+
pnl += (price - cost) * quantity * rate
|
|
128
|
+
if stop is not None:
|
|
129
|
+
stop_risk += max(price - stop, 0) * quantity * rate
|
|
130
|
+
sector = text(row.get("sector", "unknown"), "sector")
|
|
131
|
+
sectors[sector] = sectors.get(sector, 0) + value
|
|
132
|
+
currencies[currency] = currencies.get(currency, 0) + value
|
|
133
|
+
dates.add(text(row.get("as_of", "unknown"), "as_of"))
|
|
134
|
+
positions.append({"symbol": symbol, "currency": currency, "sector": sector, "value_base": value})
|
|
135
|
+
equity = cash + sum(row["value_base"] for row in positions)
|
|
136
|
+
for row in positions:
|
|
137
|
+
row["weight"] = row["value_base"] / equity
|
|
138
|
+
currencies[base] = currencies.get(base, 0) + cash
|
|
139
|
+
correlations = []
|
|
140
|
+
histories = {}
|
|
141
|
+
for row in rows(p.get("histories", []), "histories", 0):
|
|
142
|
+
if row["symbol"] not in seen or row["symbol"] in histories:
|
|
143
|
+
raise ValueError("history symbol must match a unique position")
|
|
144
|
+
histories[row["symbol"]] = prices(row["prices"])
|
|
145
|
+
symbols = list(histories)
|
|
146
|
+
for i, a in enumerate(symbols):
|
|
147
|
+
for b in symbols[i + 1 :]:
|
|
148
|
+
import numpy as np
|
|
149
|
+
|
|
150
|
+
common = sorted(histories[a].keys() & histories[b].keys())
|
|
151
|
+
corr = None
|
|
152
|
+
if len(common) >= 4:
|
|
153
|
+
x = np.array([histories[a][day] for day in common])
|
|
154
|
+
y = np.array([histories[b][day] for day in common])
|
|
155
|
+
x, y = np.diff(x) / x[:-1], np.diff(y) / y[:-1]
|
|
156
|
+
if np.std(x) > 0 and np.std(y) > 0:
|
|
157
|
+
corr = float(np.corrcoef(x, y)[0, 1])
|
|
158
|
+
correlations.append({"symbols": [a, b], "correlation": corr, "return_samples": max(len(common) - 1, 0)})
|
|
159
|
+
warnings = [
|
|
160
|
+
"Price P&L uses current FX, excludes historical FX P&L, fees and distributions; stop risk is a scenario, not a guaranteed loss limit.",
|
|
161
|
+
"Correlations use local-price returns on common dates, exclude FX, and do not forecast future dependence.",
|
|
162
|
+
]
|
|
163
|
+
if len(dates) != 1 or "unknown" in dates:
|
|
164
|
+
warnings.append("Position valuation timestamps are missing or differ")
|
|
165
|
+
return {
|
|
166
|
+
"base_currency": base,
|
|
167
|
+
"total_equity": equity,
|
|
168
|
+
"cash_weight": cash / equity,
|
|
169
|
+
"positions": positions,
|
|
170
|
+
"price_pnl_base": pnl if pnl_complete else None,
|
|
171
|
+
"stop_loss_risk_base": stop_risk if stop_complete else None,
|
|
172
|
+
"currency_exposure": {k: v / equity for k, v in currencies.items()},
|
|
173
|
+
"sector_exposure": {k: v / equity for k, v in sectors.items()},
|
|
174
|
+
"position_hhi": sum(row["weight"] ** 2 for row in positions),
|
|
175
|
+
"correlations": correlations,
|
|
176
|
+
"warnings": warnings,
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def metric_map(company):
|
|
181
|
+
result = {}
|
|
182
|
+
for metric in rows(company["metrics"], "metrics", 0):
|
|
183
|
+
name = text(metric["name"], "metric name")
|
|
184
|
+
if name in result:
|
|
185
|
+
raise ValueError("duplicate metric name")
|
|
186
|
+
value = metric.get("value")
|
|
187
|
+
if value is not None:
|
|
188
|
+
number(value, "metric value")
|
|
189
|
+
result[name] = {
|
|
190
|
+
"value": value,
|
|
191
|
+
"unit": text(metric["unit"], "unit"),
|
|
192
|
+
"basis": text(metric["basis"], "basis"),
|
|
193
|
+
"period": text(company["period"], "period"),
|
|
194
|
+
"source": metric.get("source"),
|
|
195
|
+
}
|
|
196
|
+
return result
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def peers(p):
|
|
200
|
+
companies = {}
|
|
201
|
+
for row in rows(p["companies"], "companies", 2, 30):
|
|
202
|
+
symbol = text(row["symbol"], "symbol")
|
|
203
|
+
if symbol in companies:
|
|
204
|
+
raise ValueError("duplicate company")
|
|
205
|
+
companies[symbol] = metric_map(row)
|
|
206
|
+
metrics = {}
|
|
207
|
+
names = sorted({name for company in companies.values() for name in company})
|
|
208
|
+
for name in names:
|
|
209
|
+
values = {
|
|
210
|
+
symbol: company[name]
|
|
211
|
+
for symbol, company in companies.items()
|
|
212
|
+
if name in company and company[name]["value"] is not None
|
|
213
|
+
}
|
|
214
|
+
bases = {(v["unit"], v["basis"], v["period"]) for v in values.values()}
|
|
215
|
+
comparable = len(values) >= 2 and len(bases) == 1
|
|
216
|
+
metrics[name] = {
|
|
217
|
+
"values": values,
|
|
218
|
+
"missing": [symbol for symbol in companies if symbol not in values],
|
|
219
|
+
"comparable": comparable,
|
|
220
|
+
"ascending": sorted(values, key=lambda symbol: values[symbol]["value"]) if comparable else None,
|
|
221
|
+
"warning": None if comparable else "Need two values with identical unit, basis and period",
|
|
222
|
+
}
|
|
223
|
+
return {
|
|
224
|
+
"metrics": metrics,
|
|
225
|
+
"warnings": [
|
|
226
|
+
"Ascending order is not an investment ranking. Currency amounts require explicit currency in unit; no automatic currency or accounting conversion."
|
|
227
|
+
],
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def disclosure(p):
|
|
232
|
+
current, previous = p["current"], p["previous"]
|
|
233
|
+
for document in (current, previous):
|
|
234
|
+
text(document["source"], "source")
|
|
235
|
+
body = text(document["text"], "text")
|
|
236
|
+
if len(body) > 200000:
|
|
237
|
+
raise ValueError("document exceeds 200000 characters")
|
|
238
|
+
old, new = previous["text"].splitlines(), current["text"].splitlines()
|
|
239
|
+
changes = []
|
|
240
|
+
limit = integer(p.get("max_changes", 30), "max_changes", 1, 100)
|
|
241
|
+
for tag, i, j, a, b in SequenceMatcher(None, old, new).get_opcodes():
|
|
242
|
+
if tag == "equal":
|
|
243
|
+
continue
|
|
244
|
+
item = {"type": tag}
|
|
245
|
+
for key, document, lines, start, end in (("previous", previous, old, i, j), ("current", current, new, a, b)):
|
|
246
|
+
excerpt = "\n".join(lines[start:end])
|
|
247
|
+
item[key] = {
|
|
248
|
+
"source": document["source"],
|
|
249
|
+
"lines": [start + 1, end] if end > start else None,
|
|
250
|
+
"text": excerpt[:2000],
|
|
251
|
+
"truncated": len(excerpt) > 2000,
|
|
252
|
+
}
|
|
253
|
+
changes.append(item)
|
|
254
|
+
before, after = metric_map(previous), metric_map(current)
|
|
255
|
+
metric_changes = {}
|
|
256
|
+
for name in sorted(before.keys() | after.keys()):
|
|
257
|
+
x, y = before.get(name), after.get(name)
|
|
258
|
+
comparable = bool(
|
|
259
|
+
x
|
|
260
|
+
and y
|
|
261
|
+
and x["value"] is not None
|
|
262
|
+
and y["value"] is not None
|
|
263
|
+
and x["unit"] == y["unit"]
|
|
264
|
+
and x["basis"] == y["basis"]
|
|
265
|
+
)
|
|
266
|
+
metric_changes[name] = {
|
|
267
|
+
"previous": x,
|
|
268
|
+
"current": y,
|
|
269
|
+
"comparable": comparable,
|
|
270
|
+
"delta": y["value"] - x["value"] if comparable else None,
|
|
271
|
+
"growth_pct": (y["value"] / x["value"] - 1) * 100 if comparable and x["value"] > 0 else None,
|
|
272
|
+
}
|
|
273
|
+
return {
|
|
274
|
+
"text_changes": changes[:limit],
|
|
275
|
+
"total_changes": len(changes),
|
|
276
|
+
"truncated": len(changes) > limit,
|
|
277
|
+
"metric_changes": metric_changes,
|
|
278
|
+
"warnings": [
|
|
279
|
+
"Text changes are evidence, not economic conclusions. Inputs must be verified full text; OCR and PDF extraction are performed by the host. Metric comparability relies on caller-provided accounting basis and units; nonpositive prior values have no ordinary growth rate."
|
|
280
|
+
],
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def snapshot(p):
|
|
285
|
+
if not isinstance(p["previous"], dict) or not isinstance(p["current"], dict):
|
|
286
|
+
raise ValueError("snapshots must be objects")
|
|
287
|
+
changes = []
|
|
288
|
+
missing = object()
|
|
289
|
+
|
|
290
|
+
def unavailable(value):
|
|
291
|
+
return value is None or isinstance(value, dict) and "error" in value
|
|
292
|
+
|
|
293
|
+
def compare(old, new, path):
|
|
294
|
+
if old is missing:
|
|
295
|
+
status = "added"
|
|
296
|
+
elif new is missing:
|
|
297
|
+
status = "removed"
|
|
298
|
+
elif unavailable(new) and not unavailable(old):
|
|
299
|
+
status = "unavailable"
|
|
300
|
+
elif unavailable(old) and not unavailable(new):
|
|
301
|
+
status = "recovered"
|
|
302
|
+
elif isinstance(old, dict) and isinstance(new, dict):
|
|
303
|
+
for key in sorted(old.keys() | new.keys()):
|
|
304
|
+
escaped = key.replace("~", "~0").replace("/", "~1")
|
|
305
|
+
compare(old.get(key, missing), new.get(key, missing), f"{path}/{escaped}")
|
|
306
|
+
return
|
|
307
|
+
elif type(old) is type(new) and old == new:
|
|
308
|
+
return
|
|
309
|
+
else:
|
|
310
|
+
status = "changed"
|
|
311
|
+
changes.append(
|
|
312
|
+
{
|
|
313
|
+
"path": path,
|
|
314
|
+
"status": status,
|
|
315
|
+
"previous": None if old is missing else old,
|
|
316
|
+
"current": None if new is missing else new,
|
|
317
|
+
}
|
|
318
|
+
)
|
|
319
|
+
|
|
320
|
+
compare(p["previous"], p["current"], "")
|
|
321
|
+
return {
|
|
322
|
+
"changes": changes,
|
|
323
|
+
"warnings": [
|
|
324
|
+
"Lists are compared as whole values; align symbols and normalize units/timestamps before comparing. Removed keys describe input structure, not confirmed business events."
|
|
325
|
+
],
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def fetch_dataset(symbol, p):
|
|
330
|
+
from backtest import fetch_kline
|
|
331
|
+
|
|
332
|
+
symbol = text(symbol, "symbol")
|
|
333
|
+
start = date.fromisoformat(p["start_date"]).isoformat()
|
|
334
|
+
end = date.fromisoformat(p["end_date"]).isoformat()
|
|
335
|
+
if start != p["start_date"] or end != p["end_date"] or start >= end:
|
|
336
|
+
raise ValueError("start_date/end_date must be ordered YYYY-MM-DD")
|
|
337
|
+
frame = fetch_kline(symbol, start, end)
|
|
338
|
+
if frame.empty:
|
|
339
|
+
raise ValueError("No price data in requested range")
|
|
340
|
+
frame["date"] = frame["date"].dt.strftime("%Y-%m-%d")
|
|
341
|
+
bars = frame.to_dict("records")
|
|
342
|
+
basis = sorted({bar["adjusted"] for bar in bars if isinstance(bar.get("adjusted"), str) and bar["adjusted"]})
|
|
343
|
+
return {
|
|
344
|
+
"symbol": symbol,
|
|
345
|
+
"bars": bars,
|
|
346
|
+
"price_basis": ",".join(basis) if basis else "unspecified_provider_basis",
|
|
347
|
+
"coverage": {
|
|
348
|
+
"requested_start": start,
|
|
349
|
+
"requested_end": end,
|
|
350
|
+
"actual_start": bars[0]["date"],
|
|
351
|
+
"actual_end": bars[-1]["date"],
|
|
352
|
+
},
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
def event_study(p):
|
|
357
|
+
coverage = None
|
|
358
|
+
if "symbol" in p or "benchmark" in p:
|
|
359
|
+
from stock_data import detect_market
|
|
360
|
+
|
|
361
|
+
if "prices" in p or "benchmark_prices" in p:
|
|
362
|
+
raise ValueError("choose symbols or explicit prices, not both")
|
|
363
|
+
if detect_market(p["symbol"]) != detect_market(p["benchmark"]):
|
|
364
|
+
raise ValueError("stock and benchmark must belong to the same market")
|
|
365
|
+
loaded = fetch_dataset(p["symbol"], p)
|
|
366
|
+
benchmark = fetch_dataset(p["benchmark"], p)
|
|
367
|
+
coverage = {"stock": loaded["coverage"], "benchmark": benchmark["coverage"]}
|
|
368
|
+
p = {
|
|
369
|
+
**p,
|
|
370
|
+
"prices": loaded["bars"],
|
|
371
|
+
"benchmark_prices": benchmark["bars"],
|
|
372
|
+
"price_basis": f"stock:{loaded['price_basis']}; benchmark:{benchmark['price_basis']}",
|
|
373
|
+
}
|
|
374
|
+
stock, benchmark = prices(p["prices"]), prices(p["benchmark_prices"])
|
|
375
|
+
text(p["price_basis"], "price_basis")
|
|
376
|
+
horizons = p.get("horizons", [0, 1, 5, 10, 20])
|
|
377
|
+
if not isinstance(horizons, list) or not 1 <= len(horizons) <= 20:
|
|
378
|
+
raise ValueError("horizons must contain 1..20 integers")
|
|
379
|
+
horizons = sorted({integer(h, "horizon", 0, 250) for h in horizons})
|
|
380
|
+
dates = list(stock)
|
|
381
|
+
event_dates = p["event_dates"]
|
|
382
|
+
if (
|
|
383
|
+
not isinstance(event_dates, list)
|
|
384
|
+
or not 1 <= len(event_dates) <= 100
|
|
385
|
+
or len(set(event_dates)) != len(event_dates)
|
|
386
|
+
):
|
|
387
|
+
raise ValueError("event_dates must be unique, 1..100 dates")
|
|
388
|
+
events, intervals = [], []
|
|
389
|
+
for event_date in sorted(event_dates):
|
|
390
|
+
if date.fromisoformat(event_date).isoformat() != event_date:
|
|
391
|
+
raise ValueError("event date must be YYYY-MM-DD")
|
|
392
|
+
idx = bisect_left(dates, event_date)
|
|
393
|
+
event = {"event_date": event_date, "effective_date": dates[idx] if idx < len(dates) else None, "windows": []}
|
|
394
|
+
for horizon in horizons:
|
|
395
|
+
end = idx + horizon
|
|
396
|
+
window = {
|
|
397
|
+
"horizon": horizon,
|
|
398
|
+
"return_pct": None,
|
|
399
|
+
"benchmark_return_pct": None,
|
|
400
|
+
"excess_return_pct": None,
|
|
401
|
+
"reason": None,
|
|
402
|
+
}
|
|
403
|
+
if idx == 0 or end >= len(dates):
|
|
404
|
+
window["reason"] = "insufficient_stock_window"
|
|
405
|
+
else:
|
|
406
|
+
start_date, end_date = dates[idx - 1], dates[end]
|
|
407
|
+
window.update(
|
|
408
|
+
{
|
|
409
|
+
"start_date": start_date,
|
|
410
|
+
"end_date": end_date,
|
|
411
|
+
"return_pct": (stock[end_date] / stock[start_date] - 1) * 100,
|
|
412
|
+
}
|
|
413
|
+
)
|
|
414
|
+
if start_date not in benchmark or end_date not in benchmark:
|
|
415
|
+
window["reason"] = "missing_benchmark_endpoint"
|
|
416
|
+
else:
|
|
417
|
+
window["benchmark_return_pct"] = (benchmark[end_date] / benchmark[start_date] - 1) * 100
|
|
418
|
+
window["excess_return_pct"] = window["return_pct"] - window["benchmark_return_pct"]
|
|
419
|
+
event["windows"].append(window)
|
|
420
|
+
intervals.append((idx - 1, min(idx + max(horizons), len(dates) - 1)))
|
|
421
|
+
events.append(event)
|
|
422
|
+
for i, event in enumerate(events):
|
|
423
|
+
a, b = intervals[i]
|
|
424
|
+
event["overlaps_other_event"] = any(max(a, c) < min(b, d) for j, (c, d) in enumerate(intervals) if i != j)
|
|
425
|
+
summary = {}
|
|
426
|
+
for horizon in horizons:
|
|
427
|
+
values = [
|
|
428
|
+
w["excess_return_pct"]
|
|
429
|
+
for e in events
|
|
430
|
+
for w in e["windows"]
|
|
431
|
+
if w["horizon"] == horizon and w["excess_return_pct"] is not None
|
|
432
|
+
]
|
|
433
|
+
summary[str(horizon)] = {
|
|
434
|
+
"samples": len(values),
|
|
435
|
+
"mean_excess_return_pct": sum(values) / len(values) if values else None,
|
|
436
|
+
"positive_fraction": sum(v > 0 for v in values) / len(values) if values else None,
|
|
437
|
+
}
|
|
438
|
+
return {
|
|
439
|
+
"price_basis": p["price_basis"],
|
|
440
|
+
"data_coverage": coverage,
|
|
441
|
+
"events": events,
|
|
442
|
+
"summary": summary,
|
|
443
|
+
"warnings": [
|
|
444
|
+
"Event day is the first supplied stock trading date on/after the date; use the next session for after-close releases. Baseline is its prior close. Verify complete, consistently adjusted same-market series; missing rows are not detected as holidays. Excess return is stock minus benchmark buy-and-hold return, not causal attribution. Overlapping events are not independent samples."
|
|
445
|
+
],
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
def robustness(p):
|
|
450
|
+
import pandas as pd
|
|
451
|
+
from backtest import _resolve_refs, compute_metrics, get_indicator, simulate
|
|
452
|
+
|
|
453
|
+
capital = positive(p.get("capital", 1000000), "capital")
|
|
454
|
+
train = integer(p.get("train_bars", 126), "train_bars", 30, 1000)
|
|
455
|
+
test = integer(p.get("test_bars", 63), "test_bars", 30, 1000)
|
|
456
|
+
strategies, strategy_errors = {}, {}
|
|
457
|
+
for row in rows(p["strategies"], "strategies", 1, 5):
|
|
458
|
+
name = text(row["name"], "strategy name")
|
|
459
|
+
if name in strategies:
|
|
460
|
+
raise ValueError("strategy must have a unique name")
|
|
461
|
+
strategy = deepcopy(row["strategy"])
|
|
462
|
+
strategies[name] = strategy
|
|
463
|
+
try:
|
|
464
|
+
if not isinstance(strategy, dict) or any(
|
|
465
|
+
not isinstance(strategy.get(field, {}), dict)
|
|
466
|
+
for field in ("parameters", "simulation", "entry", "exit", "position")
|
|
467
|
+
):
|
|
468
|
+
raise ValueError("strategy sections must be objects")
|
|
469
|
+
_resolve_refs(strategy, strategy.get("parameters", {}))
|
|
470
|
+
if strategy.get("simulation", {}).get("execution", "next_open") != "next_open":
|
|
471
|
+
raise ValueError("robustness requires next_open execution")
|
|
472
|
+
except (ValueError, TypeError, KeyError, OverflowError):
|
|
473
|
+
strategy_errors[name] = "Invalid strategy structure or execution model"
|
|
474
|
+
if "symbols" in p:
|
|
475
|
+
symbols = p["symbols"]
|
|
476
|
+
if "datasets" in p or not isinstance(symbols, list) or not 1 <= len(symbols) <= 5:
|
|
477
|
+
raise ValueError("choose 1..5 symbols or explicit datasets, not both")
|
|
478
|
+
if len(set(symbols)) != len(symbols):
|
|
479
|
+
raise ValueError("duplicate symbols")
|
|
480
|
+
datasets = [fetch_dataset(symbol, p) for symbol in symbols]
|
|
481
|
+
else:
|
|
482
|
+
datasets = p["datasets"]
|
|
483
|
+
results, seen = [], set()
|
|
484
|
+
for dataset in rows(datasets, "datasets", 1, 5):
|
|
485
|
+
symbol = text(dataset["symbol"], "symbol")
|
|
486
|
+
if symbol in seen:
|
|
487
|
+
raise ValueError("duplicate dataset symbol")
|
|
488
|
+
seen.add(symbol)
|
|
489
|
+
text(dataset["price_basis"], "price_basis")
|
|
490
|
+
bars = rows(dataset["bars"], "bars", train + test, 2000)
|
|
491
|
+
prices(bars)
|
|
492
|
+
if [bar["date"] for bar in bars] != sorted(bar["date"] for bar in bars):
|
|
493
|
+
raise ValueError("bars must be ordered by date")
|
|
494
|
+
df = pd.DataFrame(bars)
|
|
495
|
+
df["date"] = pd.to_datetime(df["date"])
|
|
496
|
+
for field in ("open", "high", "low", "close", "volume"):
|
|
497
|
+
for value in df[field]:
|
|
498
|
+
number(value, field, 0)
|
|
499
|
+
if (df[["open", "high", "low", "close"]] <= 0).any().any():
|
|
500
|
+
raise ValueError("OHLC must be positive")
|
|
501
|
+
if ((df["low"] > df[["open", "close"]].min(axis=1)) | (df["high"] < df[["open", "close"]].max(axis=1))).any():
|
|
502
|
+
raise ValueError("inconsistent OHLC")
|
|
503
|
+
|
|
504
|
+
def evaluate(frame, name, symbol=symbol):
|
|
505
|
+
if name in strategy_errors:
|
|
506
|
+
return {"name": name, "error": strategy_errors[name]}
|
|
507
|
+
try:
|
|
508
|
+
strategy = strategies[name]
|
|
509
|
+
for side in ("entry", "exit"):
|
|
510
|
+
config = strategy.get(side, {})
|
|
511
|
+
if config.get("logic", "all" if side == "entry" else "any") not in ("all", "any"):
|
|
512
|
+
raise ValueError("condition logic must be all or any")
|
|
513
|
+
for condition in rows(config.get("conditions", []), "conditions", 0):
|
|
514
|
+
indicator = text(condition["indicator"], "indicator")
|
|
515
|
+
if condition["operator"] not in (">", "<", ">=", "<=", "==", "cross_above", "cross_below"):
|
|
516
|
+
raise ValueError("unknown condition operator")
|
|
517
|
+
periods = [
|
|
518
|
+
integer(condition[field], field, 1, 1000000) if field in condition else None
|
|
519
|
+
for field in ("period", "fast_period", "slow_period")
|
|
520
|
+
]
|
|
521
|
+
get_indicator(frame, indicator, *periods)
|
|
522
|
+
number(float(condition["value"]), "condition value")
|
|
523
|
+
for field in ("stop_loss", "take_profit", "trailing_stop"):
|
|
524
|
+
if config.get(field) is not None:
|
|
525
|
+
number(float(config[field]), field)
|
|
526
|
+
sim = simulate(frame.reset_index(drop=True), strategy, capital, symbol)
|
|
527
|
+
start, end = (frame["date"].iloc[i].strftime("%Y-%m-%d") for i in (0, -1))
|
|
528
|
+
metrics = compute_metrics(sim["trades"], sim["equity_curve"], capital, sim["final_equity"], start, end)
|
|
529
|
+
if math.isinf(metrics["profit_loss_ratio"]):
|
|
530
|
+
metrics["profit_loss_ratio"] = None
|
|
531
|
+
return {
|
|
532
|
+
"name": name,
|
|
533
|
+
"metrics": metrics,
|
|
534
|
+
"final_equity": sim["final_equity"],
|
|
535
|
+
"execution": sim["execution"],
|
|
536
|
+
"costs": sim["costs"],
|
|
537
|
+
}
|
|
538
|
+
except (ValueError, TypeError, KeyError, IndexError, OverflowError):
|
|
539
|
+
return {"name": name, "error": "Invalid strategy or incompatible data"}
|
|
540
|
+
|
|
541
|
+
folds = []
|
|
542
|
+
for end_train in range(train, len(df) - test + 1, test):
|
|
543
|
+
training = df.iloc[end_train - train : end_train]
|
|
544
|
+
testing = df.iloc[end_train : end_train + test]
|
|
545
|
+
candidates = [evaluate(training, name) for name in strategies]
|
|
546
|
+
valid = [row for row in candidates if "error" not in row]
|
|
547
|
+
selected = max(valid, key=lambda row: row["final_equity"])["name"] if valid else None
|
|
548
|
+
folds.append(
|
|
549
|
+
{
|
|
550
|
+
"train_start": training["date"].iloc[0].strftime("%Y-%m-%d"),
|
|
551
|
+
"train_end": training["date"].iloc[-1].strftime("%Y-%m-%d"),
|
|
552
|
+
"test_start": testing["date"].iloc[0].strftime("%Y-%m-%d"),
|
|
553
|
+
"test_end": testing["date"].iloc[-1].strftime("%Y-%m-%d"),
|
|
554
|
+
"candidates": candidates,
|
|
555
|
+
"selected": selected,
|
|
556
|
+
"out_of_sample": evaluate(testing, selected)
|
|
557
|
+
if selected
|
|
558
|
+
else {"error": "No valid training candidate"},
|
|
559
|
+
}
|
|
560
|
+
)
|
|
561
|
+
results.append(
|
|
562
|
+
{
|
|
563
|
+
"symbol": symbol,
|
|
564
|
+
"price_basis": dataset["price_basis"],
|
|
565
|
+
"data_coverage": dataset.get("coverage"),
|
|
566
|
+
"folds": folds,
|
|
567
|
+
"sensitivity": [evaluate(df, name) for name in strategies],
|
|
568
|
+
"unused_tail_bars": (len(df) - train) % test,
|
|
569
|
+
}
|
|
570
|
+
)
|
|
571
|
+
return {
|
|
572
|
+
"selection_metric": "training total_return",
|
|
573
|
+
"datasets": results,
|
|
574
|
+
"warnings": [
|
|
575
|
+
"Each fold starts in cash with independently initialized indicators; windows are not a continuous portfolio. Training selects only supplied candidates; full-window sensitivity is in-sample, not validation. Per-symbol selection is independent; no automatic universe selection. Supplied adjusted OHLC, costs, corporate actions and historical price-limit limitations of run_backtest still apply. Missing trading dates and survivorship bias cannot be verified from input alone."
|
|
576
|
+
],
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
COMMANDS = {
|
|
581
|
+
"position_size": position_size,
|
|
582
|
+
"portfolio": portfolio,
|
|
583
|
+
"peers": peers,
|
|
584
|
+
"disclosure": disclosure,
|
|
585
|
+
"snapshot": snapshot,
|
|
586
|
+
"event_study": event_study,
|
|
587
|
+
"robustness": robustness,
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
class JsonArgumentParser(argparse.ArgumentParser):
|
|
592
|
+
def error(self, message):
|
|
593
|
+
print(json.dumps({"error": "Invalid command arguments; see --help"}))
|
|
594
|
+
raise SystemExit(2)
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
def main():
|
|
598
|
+
utf8_stdio()
|
|
599
|
+
parser = JsonArgumentParser(description=__doc__)
|
|
600
|
+
parser.add_argument("command", choices=COMMANDS)
|
|
601
|
+
parser.add_argument("--input", help="JSON file; otherwise read stdin")
|
|
602
|
+
args = parser.parse_args()
|
|
603
|
+
try:
|
|
604
|
+
raw = Path(args.input).read_text(encoding="utf-8") if args.input else sys.stdin.read()
|
|
605
|
+
|
|
606
|
+
def reject_constant(_value):
|
|
607
|
+
raise ValueError("JSON numbers must be finite")
|
|
608
|
+
|
|
609
|
+
payload = json.loads(
|
|
610
|
+
raw, parse_constant=reject_constant, parse_float=lambda value: number(float(value), "JSON number")
|
|
611
|
+
)
|
|
612
|
+
if not isinstance(payload, dict):
|
|
613
|
+
raise ValueError("input must be a JSON object")
|
|
614
|
+
result = COMMANDS[args.command](payload)
|
|
615
|
+
output = json.dumps(result, ensure_ascii=False, allow_nan=False)
|
|
616
|
+
output.encode("utf-8")
|
|
617
|
+
except (ValueError, TypeError, KeyError, IndexError, OverflowError, OSError):
|
|
618
|
+
result = {
|
|
619
|
+
"error": "Invalid input: check required fields, types, finite values, dates and documented constraints"
|
|
620
|
+
}
|
|
621
|
+
output = json.dumps(result)
|
|
622
|
+
except Exception:
|
|
623
|
+
result = {"error": "Analysis failed; check input and installed dependencies"}
|
|
624
|
+
output = json.dumps(result)
|
|
625
|
+
print(output)
|
|
626
|
+
return 1 if "error" in result else 0
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
if __name__ == "__main__":
|
|
630
|
+
sys.exit(main())
|