stock-analysis-plugin 0.2.8 → 0.2.9

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.
@@ -0,0 +1,466 @@
1
+ #!/usr/bin/env python3
2
+ """Deterministic rotation metrics and allocation ablation; no unspecified neural model."""
3
+
4
+ import argparse
5
+ import json
6
+ import math
7
+ import re
8
+ import stat
9
+ import statistics
10
+ import sys
11
+ from concurrent.futures import ThreadPoolExecutor
12
+ from datetime import date
13
+ from pathlib import Path
14
+
15
+ sys.path.insert(0, str(Path(__file__).resolve().parent))
16
+ from _subproc import run_tool, utf8_stdio
17
+
18
+ CALENDARS = {"A": "XSHG", "HK": "XHKG", "US": "XNYS", "JP": "XTKS", "KR": "XKRX", "TW": "XTAI"}
19
+ PRICE_BASES = {"qfq", "total_return", "split_adjusted"}
20
+
21
+
22
+ class DataQualityError(ValueError):
23
+ pass
24
+
25
+
26
+ class Parser(argparse.ArgumentParser):
27
+ def error(self, message):
28
+ raise ValueError(message)
29
+
30
+
31
+ def confidence(samples):
32
+ if not isinstance(samples, list) or not 2 <= len(samples) <= 100000:
33
+ raise ValueError("Require 2..100000 externally generated MC-dropout predictions")
34
+ if any(type(value) not in (int, float) or not math.isfinite(value) for value in samples):
35
+ raise ValueError("Predictions must be finite JSON numbers")
36
+ ordered = sorted(samples)
37
+ median = statistics.mean(ordered[(len(ordered) - 1) // 2 : len(ordered) // 2 + 1])
38
+ sigma = statistics.pstdev(samples)
39
+ mass = sum(abs(value - median) <= sigma for value in samples) / len(samples)
40
+ return {
41
+ "sample_count": len(samples),
42
+ "median": median,
43
+ "population_std": sigma,
44
+ "mass_within_one_std_of_median": mass,
45
+ "concentration_criterion_met": mass >= 0.8 if sigma > 0 else None,
46
+ "unavailable_reason": "zero_dispersion_cannot_verify_dropout" if sigma == 0 else None,
47
+ "is_buy_signal": False,
48
+ "method": "Empirical mass within median +/- population SD; inclusive boundaries, threshold >= 0.80",
49
+ "limitations": "Concentration is not profitability or calibration; MC sampling settings are caller supplied",
50
+ }
51
+
52
+
53
+ def allocation(data):
54
+ if not isinstance(data, list) or not 1 <= len(data) <= 100:
55
+ raise ValueError("Require 1..100 symbol trade-count records")
56
+ rows, symbols = [], set()
57
+ for row in data:
58
+ if not isinstance(row, dict) or set(row) != {"symbol", "buys", "wins", "streak"}:
59
+ raise ValueError("Each record requires only symbol, buys, wins and streak")
60
+ symbol = row["symbol"]
61
+ if not isinstance(symbol, str) or not symbol.strip() or symbol != symbol.strip() or symbol in symbols:
62
+ raise ValueError("Symbol labels must be nonempty, distinct and trimmed")
63
+ symbols.add(symbol)
64
+ buys, wins, streak = (row[key] for key in ("buys", "wins", "streak"))
65
+ if any(type(value) is not int or not 0 <= value <= 1000000000 for value in (buys, wins, streak)):
66
+ raise ValueError("Counts must be nonnegative integers up to one billion")
67
+ if not streak <= wins <= buys:
68
+ raise ValueError("Require streak <= wins <= buys")
69
+ rows.append(
70
+ {
71
+ **row,
72
+ "raw_weight": 1.0 + wins / buys + streak / (wins + 1) if buys else None,
73
+ "unavailable_reason": "no_paper_initialization_for_zero_buys" if not buys else None,
74
+ }
75
+ )
76
+ return {
77
+ "assets": rows,
78
+ "is_allocation_plan": False,
79
+ "method": "Section 2.3: w = 1 + wins/buys + streak/(wins+1)",
80
+ "limitations": "Counts must come from closed trades before the decision; no model selection or cash allocation",
81
+ }
82
+
83
+
84
+ def analyze(payload):
85
+ if not isinstance(payload, dict) or not payload or set(payload) - {"prediction_samples", "trade_counts"}:
86
+ raise ValueError("Provide prediction_samples and/or trade_counts")
87
+ result = {
88
+ "model_status": "not_implemented_missing_specification",
89
+ "paper_reproduction": "partial_calculations_only",
90
+ "is_buy_signal": False,
91
+ }
92
+ if "prediction_samples" in payload:
93
+ rows = payload["prediction_samples"]
94
+ if not isinstance(rows, list) or not 1 <= len(rows) <= 100:
95
+ raise ValueError("Require 1..100 prediction sample groups")
96
+ groups, total = {}, 0
97
+ for row in rows:
98
+ if not isinstance(row, dict) or set(row) != {"symbol", "samples"}:
99
+ raise ValueError("Each sample group requires symbol and samples")
100
+ symbol = row["symbol"]
101
+ if not isinstance(symbol, str) or not symbol.strip() or symbol != symbol.strip() or symbol in groups:
102
+ raise ValueError("Require distinct nonempty symbol labels")
103
+ groups[symbol] = confidence(row["samples"])
104
+ total += groups[symbol]["sample_count"]
105
+ if total > 100000:
106
+ raise ValueError("At most 100000 predictions per request")
107
+ result["confidence"] = groups
108
+ if "trade_counts" in payload:
109
+ result["allocation"] = allocation(payload["trade_counts"])
110
+ return result
111
+
112
+
113
+ def iso_date(value):
114
+ if not isinstance(value, str) or date.fromisoformat(value).isoformat() != value:
115
+ raise ValueError("Require YYYY-MM-DD")
116
+ return value
117
+
118
+
119
+ def number(value, minimum, maximum):
120
+ if type(value) not in (int, float) or not math.isfinite(value) or not minimum <= value <= maximum:
121
+ raise ValueError("Number outside documented bounds")
122
+ return value
123
+
124
+
125
+ def symbol_code(value):
126
+ from stock_data import detect_market
127
+ from trend_strategy import request_symbol
128
+
129
+ if not isinstance(value, str) or not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9.-]{0,19}", value):
130
+ raise ValueError("Require a stock/ETF code")
131
+ value = request_symbol(value)
132
+ patterns = {
133
+ "A": r"\d{6}",
134
+ "HK": r"\d{4,5}\.HK",
135
+ "US": r"[A-Z][A-Z0-9-]{0,14}",
136
+ "JP": r"[A-Z0-9]{4,5}\.T",
137
+ "KR": r"\d{6}\.(KS|KQ)",
138
+ "TW": r"\d{4,6}\.(TW|TWO)",
139
+ }
140
+ if not re.fullmatch(patterns[detect_market(value)], value):
141
+ raise ValueError("Use bare six-digit A codes or canonical exchange suffixes")
142
+ return value
143
+
144
+
145
+ def fetch_dataset(symbol, count):
146
+ from stock_data import detect_market
147
+
148
+ bars = run_tool("stock_data.py", ["kline", symbol, "--period", "daily", "--count", str(count)], timeout=30)
149
+ if not isinstance(bars, list) or not bars or len(bars) > count:
150
+ raise ValueError("History unavailable")
151
+ if detect_market(symbol) == "A":
152
+ currency = "CNY"
153
+ else:
154
+ import yfinance as yf
155
+
156
+ ticker = yf.Ticker(symbol)
157
+ ticker.history(period="5d", timeout=10)
158
+ currency = ticker.get_history_metadata().get("currency")
159
+ bases = {row.get("adjusted") for row in bars if isinstance(row, dict)}
160
+ if len(bases) != 1:
161
+ raise DataQualityError("Mixed adjustment basis")
162
+ return {"symbol": symbol, "currency": currency, "price_basis": bases.pop(), "bars": bars}
163
+
164
+
165
+ def backtest(payload):
166
+ import exchange_calendars as xcals
167
+ import pandas as pd
168
+ from stock_data import detect_market
169
+
170
+ fields = {
171
+ "datasets",
172
+ "symbols",
173
+ "start_date",
174
+ "end_date",
175
+ "history_start_date",
176
+ "warmup_weeks",
177
+ "capital",
178
+ "cost_per_side",
179
+ }
180
+ if not isinstance(payload, dict) or set(payload) - fields or ("symbols" in payload) == ("datasets" in payload):
181
+ raise ValueError("Provide symbols or datasets, not both")
182
+ start, end = (iso_date(payload[k]) for k in ("start_date", "end_date"))
183
+ if start >= end or end > str(pd.Timestamp.now(tz="UTC").date()):
184
+ raise ValueError("Require a past ordered evaluation interval")
185
+ capital = number(payload.get("capital", 1000000), 0.01, 1e12)
186
+ cost = number(payload.get("cost_per_side", 0.001), 0, 0.1)
187
+ warmup = number(payload.get("warmup_weeks", 26), 1, 104)
188
+ if int(warmup) != warmup:
189
+ raise ValueError("warmup_weeks must be an integer")
190
+ if "symbols" in payload:
191
+ symbols = payload["symbols"]
192
+ if not isinstance(symbols, list) or not 1 <= len(symbols) <= 20:
193
+ raise ValueError("Require 1..20 symbols")
194
+ symbols = [symbol_code(s) for s in symbols]
195
+ if len(set(symbols)) != len(symbols):
196
+ raise ValueError("Duplicate symbol aliases")
197
+ history_start = iso_date(
198
+ payload.get("history_start_date", str((pd.Timestamp(start) - pd.DateOffset(years=1)).date()))
199
+ )
200
+ if history_start >= start:
201
+ raise ValueError("history_start_date must precede evaluation")
202
+ count = (pd.Timestamp.now(tz="UTC").date() - date.fromisoformat(history_start)).days + 60
203
+ if not 1 <= count <= 20000 or count * len(symbols) > 100000:
204
+ raise ValueError("Requested history exceeds bounded acquisition")
205
+
206
+ def fetch(symbol):
207
+ try:
208
+ return fetch_dataset(symbol, count)
209
+ except Exception:
210
+ return {"symbol": symbol, "error": "History/currency metadata unavailable"}
211
+
212
+ with ThreadPoolExecutor(max_workers=min(4, len(symbols))) as pool:
213
+ datasets = list(pool.map(fetch, symbols))
214
+ failed = [d["symbol"] for d in datasets if "error" in d]
215
+ if failed:
216
+ return {
217
+ "error": "Cannot load the complete pool; no backtest produced",
218
+ "error_code": "incomplete_pool",
219
+ "failed_symbols": failed,
220
+ }
221
+ datasets = [{**d, "bars": [b for b in d["bars"] if b["date"] >= history_start]} for d in datasets]
222
+ else:
223
+ if "history_start_date" in payload:
224
+ raise ValueError("Explicit datasets supply their own warmup history")
225
+ datasets = payload["datasets"]
226
+ if not isinstance(datasets, list) or not 1 <= len(datasets) <= 20:
227
+ raise ValueError("Require 1..20 datasets")
228
+ groups, seen, total = {}, set(), 0
229
+ now = pd.Timestamp.now(tz="UTC")
230
+ for dataset in datasets:
231
+ if not isinstance(dataset, dict) or set(dataset) != {"symbol", "currency", "price_basis", "bars"}:
232
+ raise ValueError("Dataset requires symbol, currency, price_basis and bars")
233
+ symbol = symbol_code(dataset["symbol"])
234
+ if symbol in seen:
235
+ raise ValueError("Duplicate symbol aliases")
236
+ seen.add(symbol)
237
+ market, currency = detect_market(symbol), dataset["currency"]
238
+ if not isinstance(currency, str) or not re.fullmatch(r"[A-Z]{3}", currency):
239
+ raise ValueError("Require confirmed quote currency, not an inferred country currency")
240
+ if dataset["price_basis"] not in PRICE_BASES:
241
+ raise DataQualityError("Unverified adjusted prices")
242
+ bars = dataset["bars"]
243
+ if not isinstance(bars, list) or not 2 <= len(bars) <= 20000:
244
+ raise ValueError("Require 2..20000 dated OHLCV bars")
245
+ total += len(bars)
246
+ if total > 100000:
247
+ raise ValueError("At most 100000 price rows per request")
248
+ dates = [iso_date(row["date"]) for row in bars]
249
+ if dates != sorted(set(dates)):
250
+ raise DataQualityError("Unordered or duplicate dates")
251
+ frame = pd.DataFrame(bars).loc[[d <= end for d in dates]].copy()
252
+ if frame.empty:
253
+ raise DataQualityError("No prices in interval")
254
+ for row in frame.to_dict("records"):
255
+ values = {key: number(row[key], 0, 1e16) for key in ("open", "high", "low", "close", "volume")}
256
+ lower, upper = min(values["open"], values["close"]), max(values["open"], values["close"])
257
+ if (
258
+ values["low"] <= 0
259
+ or (values["low"] > lower and not math.isclose(values["low"], lower, rel_tol=1e-12))
260
+ or (values["high"] < upper and not math.isclose(values["high"], upper, rel_tol=1e-12))
261
+ ):
262
+ raise DataQualityError("Invalid OHLC range")
263
+ if "adjusted" in row and row["adjusted"] != dataset["price_basis"]:
264
+ raise DataQualityError("Mixed price basis")
265
+ frame.index = pd.to_datetime(frame.date)
266
+ ratios = frame.close / frame.close.shift(1)
267
+ # shortcut: legitimate extreme moves are rejected; add source-backed exceptions when a verified pool needs them.
268
+ if ((ratios > 1.5) | (ratios < 1 / 1.5) | (frame.high / frame.low > 1.5)).any():
269
+ raise DataQualityError("Price discontinuity requires independent corporate-action review")
270
+ calendar = xcals.get_calendar(
271
+ CALENDARS[market],
272
+ start=str(frame.index.min().to_period("W-FRI").start_time.date()),
273
+ end=str((pd.Timestamp(end) + pd.Timedelta(days=8)).date()),
274
+ )
275
+ for day in frame.index:
276
+ if not calendar.is_session(day):
277
+ raise DataQualityError("Prices on a non-session date")
278
+ closed = calendar.session_close(day) + (pd.Timedelta(minutes=10) if market == "HK" else pd.Timedelta(0))
279
+ if closed >= now:
280
+ raise DataQualityError("Unclosed price bar")
281
+ groups.setdefault((market, currency), {})[symbol] = (frame, dataset["price_basis"])
282
+ results = []
283
+ for (market, currency), members in sorted(groups.items()):
284
+ results.append(simulate_allocation(market, currency, members, start, end, int(warmup), capital, cost))
285
+ return {
286
+ "paper_reproduction": "allocation_ablation_only",
287
+ "model_status": "not_implemented_missing_specification",
288
+ "capital_scope": "independent_per_market_and_quote_currency",
289
+ "capital_per_group": capital,
290
+ "requested_period": {"start": start, "end": end},
291
+ "cost_per_side": cost,
292
+ "groups": results,
293
+ "warnings": [
294
+ "Both arms buy every pool member each full exchange week; this is not the neural model or its selection, ROC, MC-dropout or loss filters.",
295
+ "Historical raw scores are normalized to budgets after explicit observed-trade warmup; this initialization and normalization are experimental conventions, not a full paper reproduction.",
296
+ "Fractional adjusted-price notional units and proportional entry/exit costs; settlement, FX, lots, price limits, order queues, liquidity and taxes are not simulated. No real orders or holdings are stored.",
297
+ "Groups use independent starting capital and confirmed quote currencies; there is no combined cross-currency portfolio return.",
298
+ "Price basis and currency evidence are caller/provider supplied; discontinuity checks flag suspicious data but do not prove correctness or absence of survivorship bias.",
299
+ ],
300
+ }
301
+
302
+
303
+ def simulate_allocation(market, currency, members, start, end, warmup, capital, cost):
304
+ import exchange_calendars as xcals
305
+ import pandas as pd
306
+ from backtest import compute_metrics
307
+
308
+ first = min(frame.index.min() for frame, _basis in members.values())
309
+ first = first.to_period("W-FRI").start_time
310
+ boundary = pd.Timestamp(end).to_period("W-FRI").end_time.normalize()
311
+ calendar = xcals.get_calendar(
312
+ CALENDARS[market],
313
+ start=str((first - pd.Timedelta(days=7)).date()),
314
+ end=str((boundary + pd.Timedelta(days=7)).date()),
315
+ )
316
+ sessions = calendar.sessions_in_range(first, boundary)
317
+ weeks = {}
318
+ for session in sessions:
319
+ day = session.tz_localize(None) if session.tzinfo else session
320
+ weeks.setdefault(str(day.to_period("W-FRI")), []).append(day)
321
+ counts = {n: {"symbol": n, "buys": 0, "wins": 0, "streak": 0} for n in sorted(members)}
322
+ names = list(counts)
323
+ arms = {mode: {"equity": capital, "curve": [], "costs_paid": 0.0} for mode in ("equal_weight", "historical_weight")}
324
+ allocations, skipped, observed, last_close = [], [], 0, None
325
+ for week, days in weeks.items():
326
+ if days[-1] > pd.Timestamp(end) or days[0] < pd.Timestamp(start) <= days[-1]:
327
+ skipped.append({"week": week, "reason": "partial_evaluation_boundary"})
328
+ continue
329
+ if any(
330
+ not set(days) <= set(frame.index) or (frame.loc[frame.index.intersection(days), "volume"] <= 0).any()
331
+ for frame, _ in members.values()
332
+ ):
333
+ if days[0] >= pd.Timestamp(start):
334
+ raise DataQualityError(
335
+ "Incomplete evaluation week; cannot select trades using future data availability"
336
+ )
337
+ skipped.append({"week": week, "reason": "incomplete_or_zero_volume_pool_week"})
338
+ continue
339
+ opens = {n: float(members[n][0].loc[days[0], "open"]) for n in names}
340
+ gross = {n: float(members[n][0].loc[days[-1], "close"]) / opens[n] - 1 for n in names}
341
+ if days[0] >= pd.Timestamp(start):
342
+ if observed < warmup:
343
+ raise ValueError("Insufficient complete pre-evaluation warmup weeks")
344
+ raw = allocation(list(counts.values()))["assets"]
345
+ total = math.fsum(row["raw_weight"] for row in raw)
346
+ weights = {row["symbol"]: row["raw_weight"] / total for row in raw}
347
+ allocations.append(
348
+ {
349
+ "observation_date": last_close,
350
+ "entry_date": str(days[0].date()),
351
+ "exit_date": str(days[-1].date()),
352
+ "weights": weights,
353
+ }
354
+ )
355
+ for mode, arm in arms.items():
356
+ w = dict.fromkeys(names, 1 / len(names)) if mode == "equal_weight" else weights
357
+ initial = arm["equity"]
358
+ arm["costs_paid"] += initial * cost / (1 + cost)
359
+ for i, day in enumerate(days):
360
+ equity = (
361
+ initial
362
+ / (1 + cost)
363
+ * math.fsum(w[n] * float(members[n][0].loc[day, "close"]) / opens[n] for n in names)
364
+ )
365
+ if i == len(days) - 1:
366
+ arm["costs_paid"] += equity * cost
367
+ equity *= 1 - cost
368
+ arm["curve"].append({"date": str(day.date()), "equity": equity})
369
+ arm["equity"] = equity
370
+ for name, value in gross.items():
371
+ counts[name]["buys"] += 1
372
+ counts[name]["wins"] += int(value > 0)
373
+ counts[name]["streak"] = counts[name]["streak"] + 1 if value > 0 else 0
374
+ observed += 1
375
+ last_close = str(days[-1].date())
376
+ if not allocations:
377
+ raise ValueError("No complete evaluation week")
378
+ result = {
379
+ "market": market,
380
+ "currency": currency,
381
+ "symbols": names,
382
+ "period": {"start": allocations[0]["entry_date"], "end": allocations[-1]["exit_date"]},
383
+ "price_basis": {n: members[n][1] for n in names},
384
+ "data_coverage": {
385
+ n: {
386
+ "first": str(frame.index.min().date()),
387
+ "last": str(frame.index.max().date()),
388
+ "source": sorted(set(frame["source"].dropna())) if "source" in frame else [],
389
+ }
390
+ for n, (frame, _basis) in members.items()
391
+ },
392
+ "trade_counts": list(counts.values()),
393
+ "evaluated_weeks": len(allocations),
394
+ "skipped_weeks": skipped,
395
+ "allocations": allocations,
396
+ }
397
+ for mode, arm in arms.items():
398
+ metrics = compute_metrics(
399
+ [], arm["curve"], capital, arm["equity"], result["period"]["start"], result["period"]["end"]
400
+ )
401
+ result[mode] = {
402
+ "final_equity": arm["equity"],
403
+ "costs_paid": arm["costs_paid"],
404
+ "closed_trades": len(allocations) * len(names),
405
+ "metrics": {key: metrics[key] for key in ("total_return", "annual_return", "max_drawdown")},
406
+ }
407
+ result["delta_annual_return"] = (
408
+ result["historical_weight"]["metrics"]["annual_return"] - result["equal_weight"]["metrics"]["annual_return"]
409
+ )
410
+ return result
411
+
412
+
413
+ def unique_object(pairs):
414
+ result = {}
415
+ for key, value in pairs:
416
+ if key in result:
417
+ raise ValueError("Duplicate JSON object key")
418
+ result[key] = value
419
+ return result
420
+
421
+
422
+ def main():
423
+ utf8_stdio()
424
+ try:
425
+ parser = Parser(description=__doc__)
426
+ parser.add_argument("command", choices=["confidence", "allocation", "analyze", "backtest"])
427
+ parser.add_argument("input_file", nargs="?", type=Path)
428
+ parser.add_argument("--input", type=Path)
429
+ args = parser.parse_args()
430
+ if (args.input is None) == (args.input_file is None):
431
+ raise ValueError("Provide exactly one JSON input file")
432
+ path = args.input or args.input_file
433
+ info = path.stat()
434
+ if not stat.S_ISREG(info.st_mode) or info.st_size > 8 * 1024 * 1024:
435
+ raise ValueError("Input must be a regular JSON file up to 8 MiB")
436
+ with path.open("rb") as source:
437
+ raw = source.read(8 * 1024 * 1024 + 1)
438
+ if len(raw) > 8 * 1024 * 1024:
439
+ raise ValueError("Input exceeds 8 MiB")
440
+ data = json.loads(raw.decode("utf-8"), object_pairs_hook=unique_object)
441
+ result = {"confidence": confidence, "allocation": allocation, "analyze": analyze, "backtest": backtest}[
442
+ args.command
443
+ ](data)
444
+ result.setdefault("paper_reproduction", "partial_calculations_only")
445
+ print(json.dumps(result, ensure_ascii=True, allow_nan=False))
446
+ return 1 if "error" in result else 0
447
+ except DataQualityError:
448
+ print(
449
+ json.dumps(
450
+ {
451
+ "error": "Market data failed adjustment, chronology or session checks; review the full pool",
452
+ "error_code": "market_data_requires_review",
453
+ }
454
+ )
455
+ )
456
+ return 1
457
+ except (OSError, ValueError, TypeError, OverflowError, RecursionError):
458
+ error = "Invalid arguments or input; check --help and the skill input contract"
459
+ except Exception:
460
+ error = "Rotation calculation failed unexpectedly"
461
+ print(json.dumps({"error": error}))
462
+ return 1
463
+
464
+
465
+ if __name__ == "__main__":
466
+ sys.exit(main())