158 lines
5.6 KiB
Python
158 lines
5.6 KiB
Python
|
|
"""
|
||
|
|
diag_isolation.py — Isolate which agent change is causing the 12.83% failure.
|
||
|
|
|
||
|
|
Tests:
|
||
|
|
A. exp_shared.run_backtest as-is (hazard call + float32 + rolling vol_p60 + per-day OB)
|
||
|
|
→ Expected: ~12.83%/1739 [already confirmed]
|
||
|
|
|
||
|
|
B. Same as A but WITHOUT set_esoteric_hazard_multiplier call
|
||
|
|
→ Expected: closer to 111%? This isolates the hazard call.
|
||
|
|
|
||
|
|
C. Same as A but WITHOUT rolling vol_p60 (use static vol_p60 always)
|
||
|
|
→ Expected: somewhere between A and B?
|
||
|
|
|
||
|
|
Goal: confirm hazard call is the dominant regressor.
|
||
|
|
"""
|
||
|
|
import sys, time
|
||
|
|
from pathlib import Path
|
||
|
|
import numpy as np
|
||
|
|
import pandas as pd
|
||
|
|
import math, gc
|
||
|
|
|
||
|
|
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("exp_shared path:", exp_shared.__file__)
|
||
|
|
print()
|
||
|
|
|
||
|
|
# ── Shared data load ──────────────────────────────────────────────────────────
|
||
|
|
exp_shared.ensure_jit()
|
||
|
|
d = exp_shared.load_data()
|
||
|
|
|
||
|
|
def run_variant(label, use_hazard_call, use_rolling_vol):
|
||
|
|
print(f"\n{'='*60}")
|
||
|
|
print(f" {label}")
|
||
|
|
print(f" hazard_call={use_hazard_call} rolling_vol={use_rolling_vol}")
|
||
|
|
print(f"{'='*60}")
|
||
|
|
|
||
|
|
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(d['date_strings'])
|
||
|
|
|
||
|
|
eng = create_d_liq_engine(**kw)
|
||
|
|
eng.set_ob_engine(d['ob_eng'])
|
||
|
|
eng.set_acb(acb)
|
||
|
|
|
||
|
|
if use_hazard_call:
|
||
|
|
eng.set_esoteric_hazard_multiplier(0.0)
|
||
|
|
lev_after = getattr(eng, 'base_max_leverage', None)
|
||
|
|
print(f" After hazard call: base_max_leverage={lev_after}")
|
||
|
|
else:
|
||
|
|
lev_now = getattr(eng, 'base_max_leverage', None)
|
||
|
|
print(f" No hazard call: base_max_leverage={lev_now}")
|
||
|
|
|
||
|
|
daily_caps, daily_pnls = [], []
|
||
|
|
all_vols = []
|
||
|
|
t0 = time.time()
|
||
|
|
|
||
|
|
for i, pf in enumerate(d['parquet_files']):
|
||
|
|
ds = pf.stem
|
||
|
|
df = pd.read_parquet(pf)
|
||
|
|
for c in df.columns:
|
||
|
|
if df[c].dtype == 'float64':
|
||
|
|
df[c] = df[c].astype('float32')
|
||
|
|
acols = [c for c in df.columns if c not in exp_shared.META_COLS]
|
||
|
|
|
||
|
|
if eng.ob_engine is not None:
|
||
|
|
eng.ob_engine.preload_date(ds, d['OB_ASSETS'])
|
||
|
|
|
||
|
|
bp = df['BTCUSDT'].values if 'BTCUSDT' in df.columns else None
|
||
|
|
dvol = np.zeros(len(df), dtype=np.float32)
|
||
|
|
if bp is not None:
|
||
|
|
rets = np.diff(bp.astype('float64')) / (bp[:-1].astype('float64') + 1e-9)
|
||
|
|
for j in range(50, len(rets)):
|
||
|
|
v = np.std(rets[j-50:j])
|
||
|
|
dvol[j+1] = v
|
||
|
|
if v > 0: all_vols.append(v)
|
||
|
|
|
||
|
|
cap_before = eng.capital
|
||
|
|
|
||
|
|
if use_rolling_vol and len(all_vols) > 1000:
|
||
|
|
vp60 = np.percentile(all_vols, 60)
|
||
|
|
else:
|
||
|
|
vp60 = d['vol_p60']
|
||
|
|
|
||
|
|
vol_ok = np.where(dvol > 0, dvol > vp60, 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 eng.ob_engine is not None:
|
||
|
|
eng.ob_engine._preloaded_placement.clear()
|
||
|
|
eng.ob_engine._preloaded_signal.clear()
|
||
|
|
eng.ob_engine._preloaded_market.clear()
|
||
|
|
eng.ob_engine._ts_to_idx.clear()
|
||
|
|
|
||
|
|
del df
|
||
|
|
gc.collect()
|
||
|
|
|
||
|
|
if (i+1) % 10 == 0 or i == 0 or i == len(d['parquet_files'])-1:
|
||
|
|
elapsed = time.time() - t0
|
||
|
|
print(f" Day {i+1}/{len(d['parquet_files'])}: cap=${eng.capital:,.0f} trades={len(eng.trade_history)} ({elapsed:.0f}s)")
|
||
|
|
|
||
|
|
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
|
||
|
|
print(f"\n RESULT: ROI={roi:+.2f}% T={n} DD={max_dd:.2f}% ({elapsed:.0f}s)")
|
||
|
|
return {'label': label, 'roi': roi, 'trades': n, 'dd': max_dd}
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
results = []
|
||
|
|
|
||
|
|
# Variant B: no hazard call, rolling vol (isolate hazard call effect)
|
||
|
|
results.append(run_variant(
|
||
|
|
"B: No hazard call, rolling vol",
|
||
|
|
use_hazard_call=False,
|
||
|
|
use_rolling_vol=True,
|
||
|
|
))
|
||
|
|
|
||
|
|
# Variant C: hazard call, static vol (isolate rolling vol effect)
|
||
|
|
results.append(run_variant(
|
||
|
|
"C: Hazard call, static vol",
|
||
|
|
use_hazard_call=True,
|
||
|
|
use_rolling_vol=False,
|
||
|
|
))
|
||
|
|
|
||
|
|
# Variant D: no hazard call, static vol (cleanest comparison to replicate style)
|
||
|
|
results.append(run_variant(
|
||
|
|
"D: No hazard call, static vol",
|
||
|
|
use_hazard_call=False,
|
||
|
|
use_rolling_vol=False,
|
||
|
|
))
|
||
|
|
|
||
|
|
print(f"\n{'='*60}")
|
||
|
|
print(" ISOLATION SUMMARY")
|
||
|
|
print(f"{'='*60}")
|
||
|
|
print(f" {'Config':<45} {'ROI':>8} {'T':>6} {'DD':>7}")
|
||
|
|
print(f" {'-'*70}")
|
||
|
|
print(f" {'A: Hazard call + rolling vol (fork as-is)':<45} {'~+12.83%':>8} {'~1739':>6} {'~26.2%':>7} [prior run]")
|
||
|
|
for r in results:
|
||
|
|
print(f" {r['label']:<45} {r['roi']:>+7.2f}% {r['trades']:>6} {r['dd']:>6.2f}%")
|
||
|
|
print(f" {'replicate style (no hazard, float64, static)':<45} {'~+111.0%':>8} {'~1959':>6} {'~16.9%':>7} [prior run]")
|
||
|
|
print(f" {'GOLD target':<45} {'+181.81%':>8} {'2155':>6} {'17.65%':>7}")
|