stock-analysis-plugin 0.2.8 → 0.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/README.md +34 -26
  2. package/package.json +15 -8
  3. package/pi/index.ts +446 -13
  4. package/scripts/profile-offline.py +127 -0
  5. package/scripts/profile-workflows.py +87 -0
  6. package/scripts/setup-python.mjs +11 -4
  7. package/scripts/venv-python.mjs +7 -0
  8. package/skills/event-driven/SKILL.md +2 -0
  9. package/skills/growth-quality/SKILL.md +2 -0
  10. package/skills/market-review/SKILL.md +1 -1
  11. package/skills/sector-rotation/SKILL.md +57 -0
  12. package/skills/sector-rotation/references/computation.md +83 -0
  13. package/skills/sector-rotation/references/strategy_rules.md +62 -0
  14. package/skills/sector-rotation/scripts/rotation_metrics.py +11 -0
  15. package/skills/stock-analysis/SKILL.md +15 -4
  16. package/skills/stock-analysis/references/host-workflows.md +45 -0
  17. package/skills/stock-analysis/references/stateless-analysis.md +16 -2
  18. package/skills/stock-screener/SKILL.md +6 -0
  19. package/skills/strategy-backtest/SKILL.md +10 -3
  20. package/tools/_cache.py +93 -21
  21. package/tools/_subproc.py +43 -8
  22. package/tools/anomaly_detect.py +11 -6
  23. package/tools/backtest.py +225 -70
  24. package/tools/capabilities.py +3 -0
  25. package/tools/cli_budgets.json +2 -1
  26. package/tools/gather.py +63 -8
  27. package/tools/market_regime.py +26 -15
  28. package/tools/market_review.py +2 -2
  29. package/tools/position_context.py +5 -2
  30. package/tools/report_renderer.py +4 -0
  31. package/tools/requirements.txt +2 -2
  32. package/tools/risk_screening.py +40 -15
  33. package/tools/screener.py +44 -18
  34. package/tools/search_intel.py +94 -32
  35. package/tools/sec_holders.py +27 -25
  36. package/tools/sector_rotation.py +466 -0
  37. package/tools/signal_tracker.py +100 -49
  38. package/tools/stateless_analysis.py +600 -33
  39. package/tools/stock_data.py +111 -26
  40. package/tools/technical.py +42 -40
  41. package/tools/trading_calendar.py +26 -21
  42. package/tools/value_data.py +6 -2
  43. package/tools/watchlist.py +24 -15
  44. package/tools/watchlist_context.py +63 -49
@@ -8,13 +8,119 @@ import sys
8
8
  from bisect import bisect_left
9
9
  from copy import deepcopy
10
10
  from datetime import date
11
+ from decimal import ROUND_DOWN, Decimal, localcontext
11
12
  from difflib import SequenceMatcher
13
+ from fractions import Fraction
12
14
  from pathlib import Path
13
15
 
14
16
  from _subproc import utf8_stdio
15
17
 
16
18
 
19
+ class RawNumber(float):
20
+ def __new__(cls, value):
21
+ instance = super().__new__(cls, value)
22
+ instance.raw = value
23
+ return instance
24
+
25
+ def __str__(self):
26
+ return self.raw
27
+
28
+ def __repr__(self):
29
+ return self.raw
30
+
31
+
32
+ class JsonExactNumber:
33
+ def __init__(self, raw):
34
+ self.raw = raw
35
+
36
+
37
+ def int_to_decimal(value):
38
+ if value.bit_length() < 14000 or not hasattr(sys, "set_int_max_str_digits"):
39
+ return str(value)
40
+ previous = sys.get_int_max_str_digits()
41
+ try:
42
+ sys.set_int_max_str_digits(0)
43
+ return str(value)
44
+ finally:
45
+ sys.set_int_max_str_digits(previous)
46
+
47
+
48
+ def json_dumps_exact(value, cache=None):
49
+ cache = {} if cache is None else cache
50
+ if isinstance(value, RawNumber):
51
+ return value.raw
52
+ if isinstance(value, JsonExactNumber):
53
+ return value.raw
54
+ if isinstance(value, Fraction):
55
+ return json_dumps_exact(fraction_to_json_number(value), cache)
56
+ if isinstance(value, dict):
57
+ return (
58
+ "{"
59
+ + ",".join(
60
+ f"{json.dumps(key, ensure_ascii=False)}:{json_dumps_exact(item, cache)}" for key, item in value.items()
61
+ )
62
+ + "}"
63
+ )
64
+ if isinstance(value, list):
65
+ return "[" + ",".join(json_dumps_exact(item, cache) for item in value) + "]"
66
+ if isinstance(value, int) and not isinstance(value, bool):
67
+ if value not in cache:
68
+ cache[value] = int_to_decimal(value)
69
+ return cache[value]
70
+ return json.dumps(value, ensure_ascii=False, allow_nan=False)
71
+
72
+
73
+ def fraction_value(value):
74
+ if isinstance(value, RawNumber):
75
+ raw = value.raw
76
+ if "e" in raw or "E" in raw:
77
+ mantissa, exponent_text = raw.lower().split("e", 1)
78
+ exponent = int(exponent_text)
79
+ sign = -1 if mantissa.startswith("-") else 1
80
+ whole, _, fraction = mantissa.lstrip("+-").partition(".")
81
+ digits = whole + fraction
82
+ if len(digits) < 4000:
83
+ numerator = int(digits or "0")
84
+ denominator = 10 ** len(fraction)
85
+ if exponent >= 0:
86
+ numerator *= 10**exponent
87
+ else:
88
+ denominator *= 10**-exponent
89
+ return sign * Fraction(numerator, denominator)
90
+ if len(raw) < 4000 and raw.lstrip("-").isdigit():
91
+ return Fraction(int(raw))
92
+ return Fraction(Decimal(raw))
93
+ if isinstance(value, str):
94
+ return Fraction(Decimal(value))
95
+ if isinstance(value, (int, Decimal, Fraction)):
96
+ return Fraction(value)
97
+ return Fraction(Decimal(str(value)))
98
+
99
+
100
+ def fraction_decimal_places(value):
101
+ denominator = value.denominator
102
+ twos = (denominator & -denominator).bit_length() - 1
103
+ remainder = denominator >> twos
104
+ if remainder == 1:
105
+ return twos
106
+ estimate = int(remainder.bit_length() / 2.321928094887362)
107
+ for fives in range(max(0, estimate - 2), estimate + 3):
108
+ if 5**fives == remainder:
109
+ return max(twos, fives)
110
+ return None
111
+
112
+
17
113
  def number(value, name, minimum=None, maximum=None):
114
+ if isinstance(value, RawNumber):
115
+ exact = fraction_value(value)
116
+ if (
117
+ minimum is not None
118
+ and exact < fraction_value(minimum)
119
+ or maximum is not None
120
+ and exact > fraction_value(maximum)
121
+ ):
122
+ raise ValueError(f"{name} outside allowed range")
123
+ return value
18
124
  if isinstance(value, bool) or not isinstance(value, (int, float)) or not math.isfinite(value):
19
125
  raise ValueError(f"{name} must be a finite number")
20
126
  if minimum is not None and value < minimum or maximum is not None and value > maximum:
@@ -22,15 +128,28 @@ def number(value, name, minimum=None, maximum=None):
22
128
  return value
23
129
 
24
130
 
131
+ def exact_value(value):
132
+ return fraction_value(value) if isinstance(value, RawNumber) else value
133
+
134
+
25
135
  def positive(value, name):
136
+ if isinstance(value, Fraction):
137
+ if value <= 0:
138
+ raise ValueError(f"{name} must be positive")
139
+ return value
26
140
  value = number(value, name, minimum=0)
27
- if value == 0:
141
+ if exact_value(value) == 0:
28
142
  raise ValueError(f"{name} must be positive")
29
143
  return value
30
144
 
31
145
 
32
- def integer(value, name, minimum, maximum):
146
+ def integer(value, name, minimum, maximum=None):
33
147
  value = number(value, name, minimum, maximum)
148
+ if isinstance(value, RawNumber):
149
+ exact = fraction_value(value)
150
+ if exact.denominator != 1:
151
+ raise ValueError(f"{name} must be an integer")
152
+ return int(exact)
34
153
  if int(value) != value:
35
154
  raise ValueError(f"{name} must be an integer")
36
155
  return int(value)
@@ -95,15 +214,114 @@ def prices(value):
95
214
  return dict(sorted(result.items()))
96
215
 
97
216
 
98
- def portfolio(p):
99
- base = text(p["base_currency"], "base_currency")
100
- cash = number(p["cash"], "cash", 0)
217
+ def history_returns(history, common, cache):
218
+ state = cache.get(id(history))
219
+ if state is None:
220
+ state = {"previous": {}, "adjacent": {}}
221
+ for previous, day in zip(history, list(history)[1:]):
222
+ state["previous"][day] = previous
223
+ state["adjacent"][day] = (
224
+ fraction_value(history[day]) - fraction_value(history[previous])
225
+ ) / fraction_value(history[previous])
226
+ cache[id(history)] = state
227
+ values = []
228
+ for previous, day in zip(common, common[1:]):
229
+ if state["previous"].get(day) == previous:
230
+ values.append(state["adjacent"][day])
231
+ else:
232
+ values.append(
233
+ (fraction_value(history[day]) - fraction_value(history[previous])) / fraction_value(history[previous])
234
+ )
235
+ return values
236
+
237
+
238
+ def shock_map(value, key):
239
+ result = {}
240
+ for row in rows(value, f"{key}_shocks", 0):
241
+ label = text(row[key], key)
242
+ if label in result:
243
+ raise ValueError(f"duplicate {key} shock")
244
+ result[label] = number(row["shock"], "shock", -1, 1)
245
+ return result
246
+
247
+
248
+ def stress(scenarios, positions, cash, base):
249
+ results = []
250
+ names = set()
251
+ known = {
252
+ "symbol": {row["symbol"] for row in positions},
253
+ "sector": {row["sector"] for row in positions},
254
+ "currency": {row["currency"] for row in positions},
255
+ }
256
+ for scenario in rows(scenarios, "stress_scenarios", 1, 20):
257
+ if set(scenario) - {"name", "market_shock", "sector_shocks", "symbol_shocks", "currency_shocks"}:
258
+ raise ValueError("unknown stress scenario field")
259
+ name = text(scenario["name"], "stress scenario name")
260
+ if name in names:
261
+ raise ValueError("duplicate stress scenario name")
262
+ names.add(name)
263
+ market = number(scenario.get("market_shock", 0), "market_shock", -1, 1)
264
+ sectors = shock_map(scenario.get("sector_shocks", []), "sector")
265
+ symbols = shock_map(scenario.get("symbol_shocks", []), "symbol")
266
+ currencies = shock_map(scenario.get("currency_shocks", []), "currency")
267
+ if base in currencies:
268
+ raise ValueError("base currency cannot be shocked against itself")
269
+ for key, values in (("sector", sectors), ("symbol", symbols), ("currency", currencies)):
270
+ if not set(values) <= known[key]:
271
+ raise ValueError(f"stress scenario contains unknown {key}")
272
+ rows_after, before_values, after_values, pnl_values = [], [], [], []
273
+ for row in positions:
274
+ symbol = row["symbol"]
275
+ asset = symbols.get(symbol, sectors.get(row["sector"], market))
276
+ fx = currencies.get(row["currency"], 0)
277
+ before_value = fraction_value(row["value_base"])
278
+ combined_shock = fraction_value(asset) + fraction_value(fx) + fraction_value(asset) * fraction_value(fx)
279
+ pnl = before_value * combined_shock
280
+ after = before_value + pnl
281
+ before_values.append(before_value)
282
+ after_values.append(after)
283
+ pnl_values.append(pnl)
284
+ rows_after.append(
285
+ {
286
+ "symbol": symbol,
287
+ "asset_shock": asset,
288
+ "fx_shock": fx,
289
+ "value_before": fraction_to_json_number(before_value),
290
+ "value_after": fraction_to_json_number(after),
291
+ "pnl_base": fraction_to_json_number(pnl),
292
+ }
293
+ )
294
+ before_total = fraction_value(cash) + sum(before_values, Fraction(0))
295
+ after_total = fraction_value(cash) + sum(after_values, Fraction(0))
296
+ pnl_total = sum(pnl_values, Fraction(0))
297
+ results.append(
298
+ {
299
+ "name": name,
300
+ "equity_before": fraction_to_json_number(before_total),
301
+ "equity_after": fraction_to_json_number(after_total),
302
+ "pnl_base": fraction_to_json_number(pnl_total),
303
+ "return_pct": fraction_to_json_number(pnl_total / before_total * 100) if before_total else None,
304
+ "positions": rows_after,
305
+ }
306
+ )
307
+ return results
308
+
309
+
310
+ def currency_rates(p, base):
101
311
  rates = {base: 1}
102
312
  for row in rows(p.get("fx_rates", []), "fx_rates", 0):
103
313
  currency = text(row["currency"], "currency")
104
314
  if currency in rates:
105
315
  raise ValueError("duplicate/base FX currency")
106
316
  rates[currency] = positive(row["rate"], "rate")
317
+ return rates
318
+
319
+
320
+ def portfolio(p):
321
+ base = text(p["base_currency"], "base_currency")
322
+ cash = number(p["cash"], "cash", 0)
323
+
324
+ rates = currency_rates(p, base)
107
325
  positions = []
108
326
  currencies, sectors = {}, {}
109
327
  seen, dates = set(), set()
@@ -118,24 +336,34 @@ def portfolio(p):
118
336
  rate = rates[currency]
119
337
  quantity = positive(row["quantity"], "quantity")
120
338
  price = positive(row["price"], "price")
121
- value = price * quantity * rate
339
+ local_value = fraction_value(price) * fraction_value(quantity)
340
+ value = local_value * fraction_value(rate)
122
341
  cost = positive(row["cost"], "cost") if row.get("cost") is not None else None
123
342
  stop = positive(row["stop_price"], "stop_price") if row.get("stop_price") is not None else None
124
343
  pnl_complete &= cost is not None
125
344
  stop_complete &= stop is not None
126
345
  if cost is not None:
127
- pnl += (price - cost) * quantity * rate
346
+ pnl += (fraction_value(price) - fraction_value(cost)) * fraction_value(quantity) * fraction_value(rate)
128
347
  if stop is not None:
129
- stop_risk += max(price - stop, 0) * quantity * rate
348
+ stop_risk += (
349
+ max(fraction_value(price) - fraction_value(stop), Fraction(0))
350
+ * fraction_value(quantity)
351
+ * fraction_value(rate)
352
+ )
130
353
  sector = text(row.get("sector", "unknown"), "sector")
131
- sectors[sector] = sectors.get(sector, 0) + value
132
- currencies[currency] = currencies.get(currency, 0) + value
354
+ sectors.setdefault(sector, []).append(value)
355
+ currencies.setdefault(currency, []).append(value)
133
356
  dates.add(text(row.get("as_of", "unknown"), "as_of"))
134
357
  positions.append({"symbol": symbol, "currency": currency, "sector": sector, "value_base": value})
135
- equity = cash + sum(row["value_base"] for row in positions)
358
+ equity = fraction_value(cash) + sum((fraction_value(row["value_base"]) for row in positions), Fraction(0))
136
359
  for row in positions:
137
360
  row["weight"] = row["value_base"] / equity
138
- currencies[base] = currencies.get(base, 0) + cash
361
+ currencies = {
362
+ key: sum((fraction_value(item) for item in values), Fraction(0)) for key, values in currencies.items()
363
+ }
364
+ sectors = {key: sum((fraction_value(item) for item in values), Fraction(0)) for key, values in sectors.items()}
365
+ currencies[base] = currencies.get(base, Fraction(0)) + fraction_value(cash)
366
+
139
367
  correlations = []
140
368
  histories = {}
141
369
  for row in rows(p.get("histories", []), "histories", 0):
@@ -143,29 +371,100 @@ def portfolio(p):
143
371
  raise ValueError("history symbol must match a unique position")
144
372
  histories[row["symbol"]] = prices(row["prices"])
145
373
  symbols = list(histories)
146
- for i, a in enumerate(symbols):
147
- for b in symbols[i + 1 :]:
148
- import numpy as np
374
+ returns_cache = {}
375
+ history_data = {}
376
+ if len(symbols) > 1:
377
+ import numpy as np
378
+
379
+ for history in histories.values():
380
+ days = list(history)
381
+ values = history_returns(history, days, returns_cache)
382
+ center = values[0]
383
+ deviations = [value - center for value in values]
384
+ scale = max((abs(value) for value in deviations), default=0)
385
+ raw_vector = np.array([float(value / scale) if scale else 0.0 for value in deviations])
386
+ centered = raw_vector - raw_vector.mean()
387
+ history_data[id(history)] = {
388
+ "days": days,
389
+ "days_array": np.array(days, dtype=str),
390
+ "center": center,
391
+ "scale": scale,
392
+ "raw": raw_vector,
393
+ "centered": centered,
394
+ "norm": np.linalg.norm(centered),
395
+ }
396
+
397
+ def scaled_vector(history, common):
398
+ data = history_data[id(history)]
399
+ if common == data["days"]:
400
+ return data["centered"], data["norm"]
149
401
 
150
- common = sorted(histories[a].keys() & histories[b].keys())
402
+ def exact_common_vector():
403
+ values = history_returns(history, common, returns_cache)
404
+ center = values[0]
405
+ deviations = [value - center for value in values]
406
+ scale = max((abs(value) for value in deviations), default=0)
407
+ vector = np.array([float(value / scale) if scale else 0.0 for value in deviations])
408
+ vector -= vector.mean()
409
+ return vector, np.linalg.norm(vector)
410
+
411
+ center, scale = data["center"], data["scale"]
412
+ if scale == 0 or scale > 1000000 or scale < Fraction(1, 1000000):
413
+ return exact_common_vector()
414
+ indices = np.searchsorted(data["days_array"], common)
415
+ subset = data["raw"][indices[1:] - 1].copy()
416
+ for index in np.flatnonzero(np.diff(indices) != 1):
417
+ gap = history_returns(history, [common[index], common[index + 1]], returns_cache)[0]
418
+ try:
419
+ gap_value = float((gap - center) / scale)
420
+ except OverflowError:
421
+ return exact_common_vector()
422
+ if not math.isfinite(gap_value):
423
+ return exact_common_vector()
424
+ subset[index] = gap_value
425
+ if np.all(subset == subset[0]):
426
+ return exact_common_vector()
427
+ pre_demean_max_abs = float(np.max(np.abs(subset)))
428
+ subset -= subset.mean()
429
+ if not np.isfinite(subset).all():
430
+ return exact_common_vector()
431
+ norm = np.linalg.norm(subset)
432
+ noise_floor = pre_demean_max_abs * 1e-12
433
+ if norm <= noise_floor or not math.isfinite(norm):
434
+ return exact_common_vector()
435
+ return subset, norm
436
+
437
+ def correlation_row(pair):
438
+ a, b = pair
439
+ common = [day for day in history_data[id(histories[a])]["days"] if day in histories[b]]
151
440
  corr = None
152
441
  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)})
442
+ x, x_norm = scaled_vector(histories[a], common)
443
+ y, y_norm = scaled_vector(histories[b], common)
444
+ if x_norm and y_norm:
445
+ correlation = float(np.dot(x, y) / (x_norm * y_norm))
446
+ corr = max(-1.0, min(1.0, correlation))
447
+ return {"symbols": [a, b], "correlation": corr, "return_samples": max(len(common) - 1, 0)}
448
+
449
+ pairs = [(a, b) for i, a in enumerate(symbols) for b in symbols[i + 1 :]]
450
+ if pairs and max(len(history) for history in histories.values()) * len(pairs) > 200000:
451
+ import os
452
+ from concurrent.futures import ThreadPoolExecutor
453
+
454
+ with ThreadPoolExecutor(max_workers=min(2, os.cpu_count() or 1)) as pool:
455
+ correlations = list(pool.map(correlation_row, pairs))
456
+ else:
457
+ correlations = [correlation_row(pair) for pair in pairs]
159
458
  warnings = [
160
459
  "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
460
  "Correlations use local-price returns on common dates, exclude FX, and do not forecast future dependence.",
162
461
  ]
163
462
  if len(dates) != 1 or "unknown" in dates:
164
463
  warnings.append("Position valuation timestamps are missing or differ")
165
- return {
464
+ result = {
166
465
  "base_currency": base,
167
- "total_equity": equity,
168
- "cash_weight": cash / equity,
466
+ "total_equity": fraction_to_json_number(equity),
467
+ "cash_weight": fraction_to_json_number(fraction_value(cash) / equity),
169
468
  "positions": positions,
170
469
  "price_pnl_base": pnl if pnl_complete else None,
171
470
  "stop_loss_risk_base": stop_risk if stop_complete else None,
@@ -175,6 +474,257 @@ def portfolio(p):
175
474
  "correlations": correlations,
176
475
  "warnings": warnings,
177
476
  }
477
+ if "stress_scenarios" in p:
478
+ result["stress_tests"] = stress(p["stress_scenarios"], positions, cash, base)
479
+ warnings.append(
480
+ "Stress scenarios are deterministic what-if calculations, not forecasts; symbol shocks override sector shocks, then market shock, and FX shocks compound separately."
481
+ )
482
+ return result
483
+
484
+
485
+ def fraction_to_json_number(value, rounding=None):
486
+ value = fraction_value(value)
487
+ if value.denominator == 1:
488
+ return value.numerator
489
+ places = fraction_decimal_places(value)
490
+ if places is not None:
491
+ scaled = value.numerator * (10**places // value.denominator)
492
+ digits = int_to_decimal(abs(scaled)).rjust(places + 1, "0")
493
+ sign = "-" if scaled < 0 else ""
494
+ return JsonExactNumber(f"{sign}{digits[:-places]}.{digits[-places:]}" if places else f"{sign}{digits}")
495
+ numerator_text = int_to_decimal(value.numerator)
496
+ denominator_text = int_to_decimal(value.denominator)
497
+ with localcontext() as ctx:
498
+ ctx.prec = 50
499
+ ctx.Emax = max(ctx.Emax, len(numerator_text) + len(denominator_text) + 10)
500
+ ctx.Emin = min(ctx.Emin, -ctx.Emax)
501
+ if rounding is not None:
502
+ ctx.rounding = rounding
503
+ raw = format(Decimal(numerator_text) / Decimal(denominator_text), "e")
504
+ return JsonExactNumber(raw)
505
+
506
+
507
+ def lot_parts(value, lot, divisor=1):
508
+ """Return the exact-ratio ideal quantity and its lot-floor without binary boundary drift."""
509
+ exact = fraction_value(value) / fraction_value(divisor)
510
+ return exact, int(exact / lot) * lot
511
+
512
+
513
+ def rebalance(p):
514
+ base = text(p["base_currency"], "base_currency")
515
+ cash = number(p["cash"], "cash", 0)
516
+ rates = currency_rates(p, base)
517
+ fee = number(p.get("fee_rate", 0), "fee_rate", 0, 0.1)
518
+ slip = number(p.get("slippage_rate", 0), "slippage_rate", 0, 0.1)
519
+ turnover_limit = (
520
+ number(p["max_turnover_fraction"], "max_turnover_fraction", 0) if "max_turnover_fraction" in p else None
521
+ )
522
+ current, dates = {}, set()
523
+ for row in rows(p["positions"], "positions", 0):
524
+ symbol = text(row["symbol"], "symbol")
525
+ if symbol in current:
526
+ raise ValueError("duplicate current position")
527
+ currency = text(row["currency"], "currency")
528
+ as_of = text(row.get("as_of", "unknown"), "as_of")
529
+ current[symbol] = {
530
+ "symbol": symbol,
531
+ "quantity": positive(row["quantity"], "quantity"),
532
+ "price": positive(row["price"], "price"),
533
+ "currency": currency,
534
+ "sector": text(row.get("sector", "unknown"), "sector"),
535
+ "as_of": as_of,
536
+ }
537
+ dates.add(as_of)
538
+ targets = {}
539
+ for row in rows(p["targets"], "targets", 1):
540
+ symbol = text(row["symbol"], "symbol")
541
+ if symbol in targets:
542
+ raise ValueError("duplicate rebalance target")
543
+ old = current.get(symbol, {})
544
+ weight = number(row["weight"], "target weight", 0, 1)
545
+ currency = text(row.get("currency", old.get("currency")), "currency")
546
+ as_of = text(row.get("as_of", old.get("as_of", "unknown")), "as_of")
547
+ targets[symbol] = {
548
+ "symbol": symbol,
549
+ "weight": weight,
550
+ "price": positive(row.get("price", old.get("price")), "price"),
551
+ "currency": currency,
552
+ "rate": rates[currency],
553
+ "sector": text(row.get("sector", old.get("sector", "unknown")), "sector"),
554
+ "as_of": as_of,
555
+ "lot_size": integer(row["lot_size"], "lot_size", 1)
556
+ if "lot_size" in row or exact_value(weight) > 0
557
+ else None,
558
+ }
559
+ dates.add(as_of)
560
+ if sum((fraction_value(row["weight"]) for row in targets.values()), Fraction(0)) > 1:
561
+ raise ValueError("target weights must sum to at most 1")
562
+ for symbol, old in current.items():
563
+ if symbol not in targets:
564
+ old["rate"] = rates[old["currency"]]
565
+ targets[symbol] = {**old, "weight": 0, "lot_size": None}
566
+ equity_fraction = fraction_value(cash) + sum(
567
+ (
568
+ fraction_value(current[symbol]["quantity"])
569
+ * fraction_value(targets[symbol]["price"])
570
+ * fraction_value(targets[symbol]["rate"])
571
+ for symbol in current
572
+ ),
573
+ Fraction(0),
574
+ )
575
+ for row in targets.values():
576
+ row["unit_value"] = fraction_value(row["price"]) * fraction_value(row["rate"])
577
+ if exact_value(row["weight"]) == 0:
578
+ row["ideal_quantity"] = row["target_quantity"] = 0
579
+ continue
580
+ positive(equity_fraction * fraction_value(row["weight"]), "target value")
581
+ positive(row["unit_value"], "target unit value")
582
+ row["ideal_quantity"], row["target_quantity"] = lot_parts(
583
+ fraction_value(equity_fraction) * fraction_value(row["weight"]), row["lot_size"], row["unit_value"]
584
+ )
585
+ if row["target_quantity"] == 0:
586
+ raise ValueError("positive target weight is below one lot")
587
+
588
+ sells, buys, unfilled = [], [], []
589
+ for symbol, row in sorted(targets.items()):
590
+ held = current.get(symbol, {}).get("quantity", 0)
591
+ raw_delta = row["ideal_quantity"] - fraction_value(held)
592
+ side = "buy" if raw_delta > 0 else "sell"
593
+ raw_desired = abs(raw_delta)
594
+ if raw_desired == 0:
595
+ quantity = 0
596
+ elif side == "sell" and exact_value(row["weight"]) == 0:
597
+ quantity = fraction_value(held)
598
+ else:
599
+ _, quantity = lot_parts(raw_desired, row["lot_size"])
600
+ if raw_desired - fraction_value(quantity) > 0:
601
+ unfilled.append(
602
+ {
603
+ "symbol": symbol,
604
+ "side": "buy" if raw_delta > 0 else "sell",
605
+ "quantity": fraction_to_json_number(raw_desired - fraction_value(quantity), rounding=ROUND_DOWN),
606
+ "reason": "lot_rounding",
607
+ }
608
+ )
609
+ if quantity:
610
+ order = {
611
+ "symbol": symbol,
612
+ "side": side,
613
+ "quantity": quantity,
614
+ "requested_quantity": quantity,
615
+ "lot_size": row["lot_size"],
616
+ "price": row["price"],
617
+ "currency": row["currency"],
618
+ "unit_value": row["unit_value"],
619
+ }
620
+ (buys if side == "buy" else sells).append(order)
621
+
622
+ def cash_flow(order):
623
+ gross = fraction_value(order["quantity"]) * fraction_value(order["unit_value"])
624
+ if order["side"] == "sell":
625
+ return gross * (1 - fraction_value(slip)) * (1 - fraction_value(fee))
626
+ return -gross * (1 + fraction_value(slip)) * (1 + fraction_value(fee))
627
+
628
+ available = fraction_value(cash) + sum((cash_flow(order) for order in sells), Fraction(0))
629
+ desired_buy_cost = -sum((cash_flow(order) for order in buys), Fraction(0))
630
+ cash_limited = desired_buy_cost > available
631
+ if cash_limited and desired_buy_cost:
632
+ scale = max(Fraction(0), available / desired_buy_cost)
633
+ for order in buys:
634
+ _, quantity = lot_parts(order["requested_quantity"] * scale, order["lot_size"])
635
+ if quantity < order["requested_quantity"]:
636
+ unfilled.append(
637
+ {
638
+ "symbol": order["symbol"],
639
+ "side": "buy",
640
+ "quantity": order["requested_quantity"] - quantity,
641
+ "reason": "cash",
642
+ }
643
+ )
644
+ order["quantity"] = quantity
645
+ gross = sum(
646
+ (fraction_value(order["quantity"]) * fraction_value(order["unit_value"]) for order in sells + buys),
647
+ Fraction(0),
648
+ )
649
+ if equity_fraction == 0 and gross:
650
+ raise ValueError("turnover requires positive equity")
651
+ gross_fraction = gross / equity_fraction if equity_fraction else Fraction(0)
652
+ if turnover_limit is not None and gross_fraction > fraction_value(turnover_limit):
653
+ return {
654
+ "error": "Rebalance exceeds max_turnover_fraction",
655
+ "error_code": "turnover_limit_exceeded",
656
+ "required_turnover_fraction": fraction_to_json_number(gross_fraction),
657
+ "max_turnover_fraction": turnover_limit,
658
+ }
659
+
660
+ post_quantities = {symbol: fraction_value(row["quantity"]) for symbol, row in current.items()}
661
+ for order in sells + buys:
662
+ change = order["quantity"] * (1 if order["side"] == "buy" else -1)
663
+ post_quantities[order["symbol"]] = post_quantities.get(order["symbol"], 0) + change
664
+ cash_after = available + sum((cash_flow(order) for order in buys), Fraction(0))
665
+ if cash_after < 0:
666
+ raise ValueError("rebalance cash reconciliation failed")
667
+ positions_after = []
668
+ for symbol, row in sorted(targets.items()):
669
+ quantity = post_quantities.get(symbol, 0)
670
+ if quantity <= 0:
671
+ continue
672
+ positions_after.append(
673
+ {
674
+ "symbol": symbol,
675
+ "quantity": fraction_to_json_number(quantity),
676
+ "price": row["price"],
677
+ "currency": row["currency"],
678
+ "sector": row["sector"],
679
+ "as_of": row["as_of"],
680
+ "target_weight": row["weight"],
681
+ "value_base": fraction_value(quantity) * row["unit_value"],
682
+ }
683
+ )
684
+ equity_after = cash_after + sum((row["value_base"] for row in positions_after), Fraction(0))
685
+ for row in positions_after:
686
+ row["weight"] = fraction_to_json_number(row["value_base"] / equity_after)
687
+ row["value_base"] = fraction_to_json_number(row["value_base"])
688
+ orders = []
689
+ for order in sells + buys:
690
+ if not order["quantity"]:
691
+ continue
692
+ orders.append(
693
+ {
694
+ "symbol": order["symbol"],
695
+ "side": order["side"],
696
+ "quantity": fraction_to_json_number(order["quantity"]),
697
+ "reference_price": order["price"],
698
+ "currency": order["currency"],
699
+ "lot_size": order["lot_size"],
700
+ "estimated_value_base": fraction_to_json_number(
701
+ fraction_value(order["quantity"]) * fraction_value(order["unit_value"])
702
+ ),
703
+ "estimated_cash_flow_base": fraction_to_json_number(cash_flow(order)),
704
+ }
705
+ )
706
+ warnings = [
707
+ "Reference-price plan only; sells are ordered before buys. Actual fills, unprovided taxes/minimum fees, settlement and liquidity require confirmation."
708
+ ]
709
+ if len(dates) != 1 or "unknown" in dates:
710
+ warnings.append("Position and target valuation timestamps are missing or differ")
711
+ if cash_limited:
712
+ warnings.append("Cash was insufficient for all target buys; buy quantities were reduced proportionally by lot")
713
+ return {
714
+ "base_currency": base,
715
+ "initial_equity": fraction_to_json_number(equity_fraction),
716
+ "equity_after": fraction_to_json_number(equity_after),
717
+ "cash_after": fraction_to_json_number(cash_after),
718
+ "cash_weight_after": fraction_to_json_number(cash_after / equity_after) if equity_after else None,
719
+ "gross_turnover_fraction": fraction_to_json_number(gross_fraction),
720
+ "turnover_basis": "gross reference buy plus sell value / initial equity",
721
+ "estimated_cost_base": fraction_to_json_number(equity_fraction - equity_after),
722
+ "cash_limited": cash_limited,
723
+ "orders": orders,
724
+ "unfilled": unfilled,
725
+ "positions_after": positions_after,
726
+ "warnings": warnings,
727
+ }
178
728
 
179
729
 
180
730
  def metric_map(company):
@@ -417,11 +967,18 @@ def event_study(p):
417
967
  window["benchmark_return_pct"] = (benchmark[end_date] / benchmark[start_date] - 1) * 100
418
968
  window["excess_return_pct"] = window["return_pct"] - window["benchmark_return_pct"]
419
969
  event["windows"].append(window)
420
- intervals.append((idx - 1, min(idx + max(horizons), len(dates) - 1)))
970
+ valid_horizons = [window["horizon"] for window in event["windows"] if window["excess_return_pct"] is not None]
971
+ intervals.append((idx - 1, idx + max(valid_horizons)) if valid_horizons else None)
421
972
  events.append(event)
422
973
  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)
974
+ interval = intervals[i]
975
+ if interval is None:
976
+ event["overlaps_other_event"] = False
977
+ continue
978
+ a, b = interval
979
+ event["overlaps_other_event"] = any(
980
+ other is not None and max(a, other[0]) < min(b, other[1]) for j, other in enumerate(intervals) if i != j
981
+ )
425
982
  summary = {}
426
983
  for horizon in horizons:
427
984
  values = [
@@ -448,7 +1005,7 @@ def event_study(p):
448
1005
 
449
1006
  def robustness(p):
450
1007
  import pandas as pd
451
- from backtest import _resolve_refs, compute_metrics, get_indicator, simulate
1008
+ from backtest import CONDITION_OPERATORS, _resolve_refs, compute_metrics, get_indicator, simulate
452
1009
 
453
1010
  capital = positive(p.get("capital", 1000000), "capital")
454
1011
  train = integer(p.get("train_bars", 126), "train_bars", 30, 1000)
@@ -512,7 +1069,7 @@ def robustness(p):
512
1069
  raise ValueError("condition logic must be all or any")
513
1070
  for condition in rows(config.get("conditions", []), "conditions", 0):
514
1071
  indicator = text(condition["indicator"], "indicator")
515
- if condition["operator"] not in (">", "<", ">=", "<=", "==", "cross_above", "cross_below"):
1072
+ if condition["operator"] not in CONDITION_OPERATORS:
516
1073
  raise ValueError("unknown condition operator")
517
1074
  periods = [
518
1075
  integer(condition[field], field, 1, 1000000) if field in condition else None
@@ -526,7 +1083,7 @@ def robustness(p):
526
1083
  sim = simulate(frame.reset_index(drop=True), strategy, capital, symbol)
527
1084
  start, end = (frame["date"].iloc[i].strftime("%Y-%m-%d") for i in (0, -1))
528
1085
  metrics = compute_metrics(sim["trades"], sim["equity_curve"], capital, sim["final_equity"], start, end)
529
- if math.isinf(metrics["profit_loss_ratio"]):
1086
+ if not math.isfinite(metrics["profit_loss_ratio"]):
530
1087
  metrics["profit_loss_ratio"] = None
531
1088
  return {
532
1089
  "name": name,
@@ -580,6 +1137,7 @@ def robustness(p):
580
1137
  COMMANDS = {
581
1138
  "position_size": position_size,
582
1139
  "portfolio": portfolio,
1140
+ "rebalance": rebalance,
583
1141
  "peers": peers,
584
1142
  "disclosure": disclosure,
585
1143
  "snapshot": snapshot,
@@ -607,12 +1165,21 @@ def main():
607
1165
  raise ValueError("JSON numbers must be finite")
608
1166
 
609
1167
  payload = json.loads(
610
- raw, parse_constant=reject_constant, parse_float=lambda value: number(float(value), "JSON number")
1168
+ raw,
1169
+ parse_constant=reject_constant,
1170
+ parse_float=RawNumber
1171
+ if args.command in {"rebalance", "portfolio"}
1172
+ else lambda value: number(float(value), "JSON number"),
1173
+ parse_int=RawNumber if args.command in {"rebalance", "portfolio"} else None,
611
1174
  )
612
1175
  if not isinstance(payload, dict):
613
1176
  raise ValueError("input must be a JSON object")
614
1177
  result = COMMANDS[args.command](payload)
615
- output = json.dumps(result, ensure_ascii=False, allow_nan=False)
1178
+ output = (
1179
+ json_dumps_exact(result)
1180
+ if args.command in {"rebalance", "portfolio"}
1181
+ else json.dumps(result, ensure_ascii=False, allow_nan=False)
1182
+ )
616
1183
  output.encode("utf-8")
617
1184
  except (ValueError, TypeError, KeyError, IndexError, OverflowError, OSError):
618
1185
  result = {