""" r1_only.py — Run ONLY R1: exact certification conditions. ceiling=6.0, hazard call, static vol_p60, float64 pq_data, np.isfinite condition. This tests if the 181.81% gold standard is reproducible from the ORIGINAL conditions. """ import sys, time, gc, math from pathlib import Path import numpy as np import pandas as pd ROOT = Path(r"C:\Users\Lenovo\Documents\- DOLPHIN NG HD HCM TSF Predict") sys.path.insert(0, str(ROOT / 'nautilus_dolphin')) sys.path.insert(0, str(ROOT / 'nautilus_dolphin' / 'dvae')) import exp_shared from nautilus_dolphin.nautilus.proxy_boost_engine import create_d_liq_engine from nautilus_dolphin.nautilus.adaptive_circuit_breaker import AdaptiveCircuitBreaker print("Ensuring JIT...", flush=True) exp_shared.ensure_jit() VBT_DIR = exp_shared.VBT_DIR parquet_files = sorted(VBT_DIR.glob("*.parquet")) parquet_files = [p for p in parquet_files if 'catalog' not in str(p)] date_strings = [p.stem for p in parquet_files] print(f"Found {len(parquet_files)} parquet files", flush=True) # Static vol_p60 from first 3 files (original approach) all_vols = [] for pf in parquet_files[:3]: tmp = pd.read_parquet(pf) if 'BTCUSDT' in tmp.columns: bp = tmp['BTCUSDT'].values diffs = np.diff(bp) / bp[:-1] for i in range(50, len(diffs)): all_vols.append(np.std(diffs[i-50:i])) del tmp vol_p60_static = float(np.percentile(all_vols, 60)) if all_vols else 0.0002 print(f"Static vol_p60: {vol_p60_static:.8f}", flush=True) # Load ALL data into pq_data (the old approach — float64) print("Loading all parquet files (float64, old style)...", flush=True) pq_data = {} for i, pf in enumerate(parquet_files): ds = pf.stem df = pd.read_parquet(pf) # float64, no casting acols = [c for c in df.columns if c not in exp_shared.META_COLS] bp = df['BTCUSDT'].values if 'BTCUSDT' in df.columns else None dvol = np.full(len(df), np.nan) if bp is not None: diffs = np.zeros(len(bp), dtype=np.float64) diffs[1:] = np.diff(bp) / bp[:-1] for j in range(50, len(bp)): dvol[j] = np.std(diffs[j-50:j]) pq_data[ds] = (df, acols, dvol) if (i+1) % 10 == 0: print(f" Loaded {i+1}/{len(parquet_files)}", flush=True) print(f"All {len(pq_data)} days loaded.", flush=True) # OB setup (once only, like old path) from nautilus_dolphin.nautilus.ob_features import OBFeatureEngine from nautilus_dolphin.nautilus.ob_provider import MockOBProvider OB_ASSETS = ["BTCUSDT", "ETHUSDT"] _mock_ob = MockOBProvider( imbalance_bias=-0.09, depth_scale=1.0, assets=OB_ASSETS, imbalance_biases={"BTCUSDT":-0.086,"ETHUSDT":-0.092,"BNBUSDT":+0.05,"SOLUSDT":+0.05}, ) ob_eng = OBFeatureEngine(_mock_ob) ob_eng.preload_date("mock", OB_ASSETS) # ── R1: Exact certification conditions ─────────────────────────────────────── print("\n" + "="*65, flush=True) print(" R1: ceiling=6.0 + hazard call + float64 + static vol + isfinite", flush=True) print("="*65, flush=True) ceiling_lev = 6.0 kw = exp_shared.ENGINE_KWARGS.copy() kw.update({'sp_maker_entry_rate': 1.0, 'sp_maker_exit_rate': 1.0, 'use_sp_slippage': False}) acb = AdaptiveCircuitBreaker() acb.preload_w750(date_strings) eng = create_d_liq_engine(**kw) eng.set_ob_engine(ob_eng) eng.set_acb(acb) # Patch hazard multiplier to use ceiling=6.0 import types, nautilus_dolphin.nautilus.esf_alpha_orchestrator as orch_mod def patched_hazard(self, hazard_score): floor_lev = 3.0 c_lev = ceiling_lev range_lev = c_lev - floor_lev target_lev = c_lev - (hazard_score * range_lev) step = range_lev / 8.0 import math as _math stepped_lev = _math.ceil(target_lev / step) * step self.base_max_leverage = max(floor_lev, min(c_lev, stepped_lev)) self.bet_sizer.max_leverage = self.base_max_leverage if hasattr(self, '_day_mc_status'): if self._day_mc_status == 'RED': self.bet_sizer.max_leverage = self.base_max_leverage * 0.8 eng.set_esoteric_hazard_multiplier = types.MethodType(patched_hazard, eng) eng.set_esoteric_hazard_multiplier(0.0) base_after = getattr(eng, 'base_max_leverage', None) abs_max = getattr(eng, 'abs_max_leverage', None) print(f"After hazard call: base_max={base_after} abs_max={abs_max}", flush=True) print(f"Expected: base=6.0 (ceiling), abs=9.0 (D_LIQ hard cap)", flush=True) daily_caps, daily_pnls = [], [] t0 = time.time() for i, pf in enumerate(parquet_files): ds = pf.stem df, acols, dvol = pq_data[ds] cap_before = eng.capital vol_ok = np.where(np.isfinite(dvol), dvol > vol_p60_static, False) eng.process_day(ds, df, acols, vol_regime_ok=vol_ok) daily_caps.append(eng.capital) daily_pnls.append(eng.capital - cap_before) if (i+1) % 10 == 0 or i == 0 or i == len(parquet_files)-1: elapsed = time.time() - t0 print(f" Day {i+1}/{len(parquet_files)}: cap=${eng.capital:,.0f} trades={len(eng.trade_history)} ({elapsed:.0f}s)", flush=True) tr = eng.trade_history n = len(tr) roi = (eng.capital - 25000.0) / 25000.0 * 100.0 peak_cap, max_dd = 25000.0, 0.0 for cap in daily_caps: peak_cap = max(peak_cap, cap) max_dd = max(max_dd, (peak_cap - cap) / peak_cap * 100.0) elapsed = time.time() - t0 def _abs(t): return t.pnl_absolute if hasattr(t,'pnl_absolute') else t.pnl_pct*250. if n > 0: wins = [t for t in tr if _abs(t) > 0] pf_val = sum(_abs(t) for t in wins) / max(abs(sum(_abs(t) for t in tr if _abs(t)<=0)), 1e-9) dr = np.array([p/25000.*100. for p in daily_pnls]) sharpe = float(dr.mean()/(dr.std()+1e-9)*math.sqrt(365)) calmar = roi / max(max_dd, 0.01) else: pf_val, sharpe, calmar = 0, 0, 0 print(f"\n{'='*65}", flush=True) print(f"R1 RESULT: ROI={roi:+.2f}% T={n} DD={max_dd:.2f}% PF={pf_val:.3f} Calmar={calmar:.2f} ({elapsed:.0f}s)", flush=True) print(f"GOLD: ROI=+181.81% T=2155 DD=17.65%", flush=True) roi_ok = abs(roi - 181.81) <= 2.0 t_ok = abs(n - 2155) <= 10 print(f"ROI match: {'PASS ✓' if roi_ok else 'FAIL ✗'} (diff={roi-181.81:+.2f}pp)", flush=True) print(f"T match: {'PASS ✓' if t_ok else 'FAIL ✗'} (diff={n-2155:+d})", flush=True) if roi_ok and t_ok: print("\n>>> GOLD STANDARD IS LEGITIMATE. The 181.81% was real.", flush=True) print(">>> The agent's ceiling=10.0 change killed it by forcing 9.0x always.", flush=True) elif roi > 150: print(f"\n>>> GOLD IS APPROXIMATELY REAL (ROI={roi:.1f}% vs 181.81%). Minor discrepancy.", flush=True) else: print(f"\n>>> GOLD CANNOT BE REPRODUCED from cert conditions. Deeper regression exists.", flush=True)