stock-analysis-plugin 0.2.7 → 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.
package/tools/backtest.py CHANGED
@@ -17,7 +17,16 @@ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
17
17
 
18
18
  from _subproc import json_safe, run_tool, utf8_stdio
19
19
  from stock_data import detect_market
20
- from technical import bollinger_series, ema_series, ma_series, macd_series, rsi_series
20
+ from technical import (
21
+ bollinger_series,
22
+ ema_series,
23
+ ma_series,
24
+ macd_series,
25
+ monthly_market_entry_series,
26
+ monthly_momentum_series,
27
+ rsi_series,
28
+ trend_score_series,
29
+ )
21
30
 
22
31
  # --------------- Strategy Loading ---------------
23
32
 
@@ -175,6 +184,10 @@ def get_indicator(
175
184
  "macd": lambda: compute_macd_hist(close),
176
185
  "ma_diff": lambda: compute_ma_diff(close, period, fast, slow),
177
186
  "ema_diff": lambda: compute_ema_diff(close, period, fast, slow),
187
+ "trend_score": lambda: trend_score_series(
188
+ df, fast if fast is not None else 20, slow if slow is not None else 60, period if period is not None else 14
189
+ ),
190
+ "monthly_momentum": lambda: monthly_momentum_series(df, period if period is not None else 63),
178
191
  "volume_ratio": lambda: compute_volume_ratio(df["volume"], period or 5),
179
192
  "price_change": lambda: compute_price_change(close),
180
193
  "bollinger_position": lambda: compute_bollinger_position(close, period or 20),
@@ -227,9 +240,11 @@ def evaluate_conditions(df: pd.DataFrame, conditions: list, logic: str, idx: int
227
240
  reasons = []
228
241
  for cond in conditions:
229
242
  ind_name = cond["indicator"]
230
- period = _coerce_int(cond.get("period"))
231
- fast = _coerce_int(cond.get("fast_period"))
232
- slow = _coerce_int(cond.get("slow_period"))
243
+ parameters = (cond.get("period"), cond.get("fast_period"), cond.get("slow_period"))
244
+ if ind_name.lower() in ("trend_score", "monthly_momentum"):
245
+ period, fast, slow = (int(v) if isinstance(v, str) else v for v in parameters)
246
+ else:
247
+ period, fast, slow = map(_coerce_int, parameters)
233
248
 
234
249
  cache_key = f"{ind_name}_{period}_{fast}_{slow}"
235
250
  if cache_key not in cache:
@@ -253,7 +268,9 @@ def evaluate_conditions(df: pd.DataFrame, conditions: list, logic: str, idx: int
253
268
  # --------------- Simulation ---------------
254
269
 
255
270
 
256
- def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> dict:
271
+ def simulate(
272
+ df: pd.DataFrame, strategy: dict, capital: float, symbol: str, *, entry_gate: pd.Series | None = None
273
+ ) -> dict:
257
274
  if not math.isfinite(capital) or capital <= 0:
258
275
  raise ValueError("capital must be finite and positive")
259
276
  if not np.isfinite(df["close"]).all() or (df["close"] <= 0).any():
@@ -261,6 +278,10 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
261
278
  dates = pd.to_datetime(df["date"], errors="coerce")
262
279
  if dates.isna().any() or dates.duplicated().any() or not dates.is_monotonic_increasing:
263
280
  raise ValueError("backtest dates must be valid, unique and increasing")
281
+ if entry_gate is not None:
282
+ if not entry_gate.index.equals(df.index) or not (entry_gate.isna() | entry_gate.isin([True, False])).all():
283
+ raise ValueError("entry_gate must contain aligned booleans or unavailable values")
284
+ entry_gate = entry_gate.fillna(False).astype(bool)
264
285
  market = detect_market(symbol)
265
286
  lot_size = 100 if market == "A" else 1
266
287
  sim_cfg = strategy.get("simulation", {})
@@ -330,6 +351,8 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
330
351
  hit, reason = evaluate_conditions(df, entry_conditions, entry_logic, signal_idx, cache)
331
352
  if pending:
332
353
  hit, reason, signal_date = True, pending[0], pending[1]
354
+ if entry_gate is not None and not entry_gate.iloc[signal_idx]:
355
+ hit, pending = False, None
333
356
  if hit and not tradable(row, "buy"):
334
357
  pending = (reason, signal_date)
335
358
  blocked_orders += 1
@@ -464,6 +487,48 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
464
487
  }
465
488
 
466
489
 
490
+ def simulate_monthly_market(
491
+ df: pd.DataFrame, market: pd.DataFrame, capital: float, symbol: str, start: str, allocation: float = 1.0
492
+ ) -> dict:
493
+ """Fixed research protocol: three drifting sleeves, market-gated entries and explicit warmup."""
494
+ if isinstance(allocation, bool) or not math.isfinite(allocation) or not 0 < allocation <= 1:
495
+ raise ValueError("allocation must be finite and in (0, 1]")
496
+ if not math.isfinite(capital) or capital <= 0:
497
+ raise ValueError("capital must be finite and positive")
498
+ start_date = pd.Timestamp(start)
499
+ dates = pd.to_datetime(df["date"], errors="coerce")
500
+ before = dates[(dates < start_date) & (df["volume"] > 0)] if "volume" in df else dates[dates < start_date]
501
+ if pd.isna(start_date) or before.empty or not (dates >= start_date).any():
502
+ raise ValueError("start requires both prehistory and evaluation bars")
503
+ sleeve_capital = math.floor(capital * allocation / 3 * 100) / 100
504
+ curves, trades, paid = [], [], 0.0
505
+ for horizon in (21, 63, 252):
506
+ gate = monthly_market_entry_series(df, market, horizon, activation=before.iloc[-1])
507
+ condition = {"indicator": "monthly_momentum", "period": horizon, "value": 0}
508
+ strategy = {
509
+ "entry": {"conditions": [{**condition, "operator": ">"}]},
510
+ "exit": {"conditions": [{**condition, "operator": "<"}], "stop_loss": -1, "take_profit": 1e9},
511
+ "simulation": {"execution": "next_open", "commission": 0.001, "slippage": 0.001, "stamp_tax": 0},
512
+ }
513
+ result = simulate(df, strategy, sleeve_capital, symbol, entry_gate=gate)
514
+ curves.append(pd.Series({row["date"]: row["equity"] for row in result["equity_curve"]}))
515
+ trades.extend({**trade, "horizon": horizon} for trade in result["trades"])
516
+ paid += result["costs"]["commission_paid"]
517
+ total = pd.concat(curves, axis=1).sum(axis=1) + capital - sleeve_capital * 3
518
+ return {
519
+ "model": "monthly_momentum_market_entry",
520
+ "validation_status": "not_validated",
521
+ "experimental": True,
522
+ "allocation": allocation,
523
+ "start": str(start_date.date()),
524
+ "warmup_start": str(dates.iloc[0].date()),
525
+ "trades": sorted(trades, key=lambda trade: (trade["date"], trade["horizon"])),
526
+ "equity_curve": [{"date": date, "equity": round(float(value), 2)} for date, value in total.items()],
527
+ "final_equity": round(float(total.iloc[-1]), 2),
528
+ "costs": {"commission": 0.001, "slippage": 0.001, "stamp_tax": 0, "commission_paid": round(paid, 2)},
529
+ }
530
+
531
+
467
532
  # --------------- Metrics ---------------
468
533
 
469
534
 
@@ -18,6 +18,12 @@ ALL_MARKETS = ["A", "HK", "US", "JP", "KR", "TW"]
18
18
 
19
19
  # tool_name -> {markets: [supported markets], reason: str for unsupported}
20
20
  TOOL_MATRIX = {
21
+ "calculate_rotation_metrics": {"markets": ALL_MARKETS, "reason": None},
22
+ "backtest_rotation_allocation": {"markets": ALL_MARKETS, "reason": None},
23
+ "analyze_trend_strategy": {
24
+ "markets": ["A", "HK", "US"],
25
+ "reason": "Monthly trend strategy supports A/HK/US pools only",
26
+ },
21
27
  # Explicit-input analytics; data acquisition retains each provider's market limits.
22
28
  "compare_stocks": {"markets": ALL_MARKETS, "reason": None},
23
29
  "analyze_disclosure_changes": {"markets": ALL_MARKETS, "reason": None},
@@ -22,5 +22,7 @@
22
22
  "l2_workers": 6,
23
23
  "watchlist_item": 480,
24
24
  "stateless_analysis.py:event_study": 300,
25
- "stateless_analysis.py:robustness": 660
25
+ "stateless_analysis.py:robustness": 660,
26
+ "trend_strategy.py:fetch": 660,
27
+ "sector_rotation.py:backtest": 300
26
28
  }
@@ -95,11 +95,13 @@ def main():
95
95
  p.add_argument("--template", default="full", choices=["brief", "full"])
96
96
  p.add_argument("--input", "-i", default="-", help="JSON file path or '-' for stdin")
97
97
  p.add_argument("--input-b64", help="Base64-encoded JSON (avoids shell escaping)")
98
+ p.add_argument("--final", action="store_true", help="Mark a completed report for final reply delivery by the host")
98
99
 
99
100
  p2 = sub.add_parser("market", help="Render a market review report")
100
101
  p2.add_argument("--input", "-i", default="-", help="JSON file path or '-' for stdin")
101
102
  p2.add_argument("--input-b64", help="Base64-encoded JSON (avoids shell escaping)")
102
103
  p2.add_argument("--save", action="store_true", help="Append the report object to the review JSONL archive")
104
+ p2.add_argument("--final", action="store_true", help="Mark a completed report for final reply delivery by the host")
103
105
 
104
106
  args = parser.parse_args()
105
107
  if not args.command:
@@ -110,6 +112,8 @@ def main():
110
112
  result = render(args.command, getattr(args, "template", "full"), report)
111
113
  if args.command == "market" and args.save and "error" not in result:
112
114
  result["archived"] = save_review(report)
115
+ if args.final and "error" not in result and "error" not in result.get("archived", {}):
116
+ result["final"] = True
113
117
  json.dump(result, sys.stdout, ensure_ascii=False, indent=2, default=str)
114
118
  print()
115
119
  if "error" in result:
@@ -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())