"""Independent numerical checks for three Tradewink Learn studies. Run: python3 equity-research-labs.py Python standard library only. Fully synthetic; no network, credentials, brokers, files or trading actions. This checks arithmetic, not market risk calibration. It enumerates all full-horizon rotations; it does not reproduce the separate NumPy implementation's random stream or block-one simulation percentiles. """ import json import math def drawdown(values): peak = values[0] worst = 0.0 for value in values: peak = max(peak, value) worst = max(worst, 100 * (1 - value / peak)) return worst def check_studies(): closing = [100, 100, 100] intraday = [100, 70, 100, 70, 100] assert drawdown(closing) == 0 assert math.isclose(drawdown(intraday), 30) cash, requests = 1000, [800, 800] fraction = cash / sum(requests) quantities = [requests[0] * fraction / 10, requests[1] * fraction / 20] terminal = quantities[0] * 12 + quantities[1] * 18 assert fraction == 0.625 and quantities == [50, 25] and terminal == 1050 returns = [-0.1, -0.1, 0.25] + [0.0] * 17 rotations = [] for start in range(len(returns)): equity = [100.0] for offset in range(len(returns)): equity.append(equity[-1] * (1 + returns[(start + offset) % len(returns)])) terminal_pct = 100 * (equity[-1] / equity[0] - 1) assert math.isclose(terminal_pct, 1.25, abs_tol=1e-10) rotations.append({"start": start, "terminal_return_pct": terminal_pct, "max_drawdown_pct": drawdown(equity)}) depths = [row["max_drawdown_pct"] for row in rotations] assert math.isclose(min(depths), 10) and math.isclose(max(depths), 19) return { "synthetic_only": True, "closing_drawdown_pct": drawdown(closing), "intraday_drawdown_pct": drawdown(intraday), "shared_capital": {"fill_fraction": fraction, "quantities": quantities, "terminal_equity": terminal}, "full_horizon_rotations": rotations, } if __name__ == "__main__": print(json.dumps(check_studies(), indent=2, allow_nan=False))