initial: import DOLPHIN baseline 2026-04-21 from dolphinng5_predict working tree
Includes core prod + GREEN/BLUE subsystems: - prod/ (BLUE harness, configs, scripts, docs) - nautilus_dolphin/ (GREEN Nautilus-native impl + dvae/ preserved) - adaptive_exit/ (AEM engine + models/bucket_assignments.pkl) - Observability/ (EsoF advisor, TUI, dashboards) - external_factors/ (EsoF producer) - mc_forewarning_qlabs_fork/ (MC regime/envelope) Excludes runtime caches, logs, backups, and reproducible artifacts per .gitignore.
This commit is contained in:
255
nautilus_dolphin/test_pf_acb_v2.py
Executable file
255
nautilus_dolphin/test_pf_acb_v2.py
Executable file
@@ -0,0 +1,255 @@
|
||||
"""ACB v2 test — measures drawdown and per-date P&L, not just ROI.
|
||||
|
||||
Key insight from legacy data: ACB's value is DRAWDOWN PROTECTION.
|
||||
Feb 6 crash day: SHORT strategy LOST -8.07% (whipsaw kills shorts).
|
||||
ACB cut max DD from 18.3% to 5.6%, Sharpe from 1.50 to 1.88.
|
||||
"""
|
||||
import sys, time
|
||||
from pathlib import Path
|
||||
from collections import Counter
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).parent))
|
||||
|
||||
# Pre-compile numba kernels
|
||||
print("Compiling numba kernels...")
|
||||
t_jit = time.time()
|
||||
from nautilus_dolphin.nautilus.alpha_asset_selector import compute_irp_nb, compute_ars_nb, rank_assets_irp_nb
|
||||
from nautilus_dolphin.nautilus.alpha_bet_sizer import compute_sizing_nb
|
||||
from nautilus_dolphin.nautilus.alpha_signal_generator import check_dc_nb
|
||||
_p = np.array([1.0, 2.0, 3.0], dtype=np.float64)
|
||||
compute_irp_nb(_p, -1)
|
||||
compute_ars_nb(1.0, 0.5, 0.01)
|
||||
rank_assets_irp_nb(np.ones((10, 2), dtype=np.float64), 8, -1, 5, 500.0, 20, 0.20)
|
||||
compute_sizing_nb(-0.03, -0.02, -0.05, 3.0, 0.5, 5.0, 0.20, True, True, 0.0,
|
||||
np.zeros(4, dtype=np.int64), np.zeros(4, dtype=np.int64),
|
||||
np.zeros(5, dtype=np.float64), 0)
|
||||
check_dc_nb(_p, 3, 1, 0.75)
|
||||
print(f" JIT compile: {time.time() - t_jit:.1f}s")
|
||||
|
||||
from nautilus_dolphin.nautilus.alpha_orchestrator import NDAlphaEngine
|
||||
from nautilus_dolphin.nautilus.adaptive_circuit_breaker import AdaptiveCircuitBreaker
|
||||
|
||||
VBT_DIR = Path(r"C:\Users\Lenovo\Documents\- DOLPHIN NG HD HCM TSF Predict\vbt_cache")
|
||||
META_COLS = {'timestamp', 'scan_number', 'v50_lambda_max_velocity', 'v150_lambda_max_velocity',
|
||||
'v300_lambda_max_velocity', 'v750_lambda_max_velocity', 'vel_div',
|
||||
'instability_50', 'instability_150'}
|
||||
|
||||
ENGINE_KWARGS = dict(
|
||||
initial_capital=25000.0,
|
||||
vel_div_threshold=-0.02, vel_div_extreme=-0.05,
|
||||
min_leverage=0.5, max_leverage=5.0, leverage_convexity=3.0,
|
||||
fraction=0.20, fixed_tp_pct=0.0099, stop_pct=1.0, max_hold_bars=120,
|
||||
use_direction_confirm=True, dc_lookback_bars=7, dc_min_magnitude_bps=0.75,
|
||||
dc_skip_contradicts=True, dc_leverage_boost=1.0, dc_leverage_reduce=0.5,
|
||||
use_asset_selection=True, min_irp_alignment=0.45,
|
||||
use_sp_fees=True, use_sp_slippage=True,
|
||||
sp_maker_entry_rate=0.62, sp_maker_exit_rate=0.50,
|
||||
use_ob_edge=True, ob_edge_bps=5.0, ob_confirm_rate=0.40,
|
||||
lookback=100, use_alpha_layers=True, use_dynamic_leverage=True, seed=42,
|
||||
)
|
||||
|
||||
# Initialize ACB
|
||||
acb = AdaptiveCircuitBreaker()
|
||||
|
||||
# Pre-load ACB cuts
|
||||
parquet_files = sorted(VBT_DIR.glob("*.parquet"))
|
||||
acb_cuts = {}
|
||||
for pf in parquet_files:
|
||||
acb_cuts[pf.stem] = acb.get_cut_for_date(pf.stem)
|
||||
|
||||
# Vol percentiles from first 2 days
|
||||
all_vols = []
|
||||
for pf in parquet_files[:2]:
|
||||
df = pd.read_parquet(pf)
|
||||
if 'BTCUSDT' not in df.columns:
|
||||
continue
|
||||
prices = df['BTCUSDT'].values
|
||||
for i in range(60, len(prices)):
|
||||
seg = prices[max(0, i-50):i]
|
||||
if len(seg) < 10:
|
||||
continue
|
||||
rets = np.diff(seg) / seg[:-1]
|
||||
v = float(np.std(rets))
|
||||
if v > 0:
|
||||
all_vols.append(v)
|
||||
vol_p60 = float(np.percentile(all_vols, 60))
|
||||
|
||||
|
||||
def run_backtest(use_acb=False, label=""):
|
||||
"""Run full backtest, return engine + per-date stats."""
|
||||
engine = NDAlphaEngine(**ENGINE_KWARGS)
|
||||
bar_idx = 0
|
||||
price_histories = {}
|
||||
|
||||
date_stats = []
|
||||
peak_capital = engine.capital
|
||||
max_dd = 0.0
|
||||
|
||||
for pf in parquet_files:
|
||||
date_str = pf.stem
|
||||
acb_cut = acb_cuts[date_str]['cut'] if use_acb else 0.0
|
||||
cap_start = engine.capital
|
||||
trades_start = len(engine.trade_history)
|
||||
|
||||
df = pd.read_parquet(pf)
|
||||
asset_cols = [c for c in df.columns if c not in META_COLS]
|
||||
btc_prices = df['BTCUSDT'].values if 'BTCUSDT' in df.columns else None
|
||||
date_vol = np.full(len(df), np.nan)
|
||||
if btc_prices is not None:
|
||||
for i in range(50, len(btc_prices)):
|
||||
seg = btc_prices[max(0, i-50):i]
|
||||
if len(seg) < 10:
|
||||
continue
|
||||
rets = np.diff(seg) / seg[:-1]
|
||||
date_vol[i] = float(np.std(rets))
|
||||
|
||||
bars_in_date = 0
|
||||
for row_i in range(len(df)):
|
||||
row = df.iloc[row_i]
|
||||
vel_div = row.get("vel_div")
|
||||
if vel_div is None or not np.isfinite(vel_div):
|
||||
bar_idx += 1
|
||||
bars_in_date += 1
|
||||
continue
|
||||
prices = {}
|
||||
for ac in asset_cols:
|
||||
p = row[ac]
|
||||
if p and p > 0 and np.isfinite(p):
|
||||
prices[ac] = float(p)
|
||||
if ac not in price_histories:
|
||||
price_histories[ac] = []
|
||||
price_histories[ac].append(float(p))
|
||||
if not prices:
|
||||
bar_idx += 1
|
||||
bars_in_date += 1
|
||||
continue
|
||||
if bars_in_date < 100:
|
||||
vol_regime_ok = False
|
||||
else:
|
||||
v = date_vol[row_i]
|
||||
vol_regime_ok = (np.isfinite(v) and v > vol_p60)
|
||||
|
||||
# Apply ACB fraction reduction
|
||||
if acb_cut > 0 and engine.position is None:
|
||||
engine.bet_sizer.base_fraction = 0.20 * (1.0 - acb_cut)
|
||||
|
||||
engine.process_bar(
|
||||
bar_idx=bar_idx, vel_div=float(vel_div),
|
||||
prices=prices, vol_regime_ok=vol_regime_ok,
|
||||
price_histories=price_histories,
|
||||
)
|
||||
|
||||
# Restore fraction
|
||||
if engine.bet_sizer.base_fraction != 0.20:
|
||||
engine.bet_sizer.base_fraction = 0.20
|
||||
|
||||
bar_idx += 1
|
||||
bars_in_date += 1
|
||||
|
||||
# Per-date stats
|
||||
cap_end = engine.capital
|
||||
date_pnl = cap_end - cap_start
|
||||
date_trades = len(engine.trade_history) - trades_start
|
||||
|
||||
# Track drawdown
|
||||
peak_capital = max(peak_capital, cap_end)
|
||||
dd = (peak_capital - cap_end) / peak_capital * 100 if peak_capital > 0 else 0
|
||||
max_dd = max(max_dd, dd)
|
||||
|
||||
date_stats.append({
|
||||
'date': date_str,
|
||||
'pnl': date_pnl,
|
||||
'roi_pct': date_pnl / cap_start * 100 if cap_start > 0 else 0,
|
||||
'capital': cap_end,
|
||||
'trades': date_trades,
|
||||
'dd_pct': dd,
|
||||
'acb_cut': acb_cut * 100,
|
||||
})
|
||||
|
||||
return engine, date_stats, max_dd, peak_capital
|
||||
|
||||
|
||||
# Run both
|
||||
print("\n=== Running BASELINE (no ACB) ===")
|
||||
t0 = time.time()
|
||||
eng_base, stats_base, dd_base, peak_base = run_backtest(use_acb=False, label="baseline")
|
||||
print(f" Done: {time.time()-t0:.0f}s")
|
||||
|
||||
print("\n=== Running WITH ACB ===")
|
||||
t1 = time.time()
|
||||
eng_acb, stats_acb, dd_acb, peak_acb = run_backtest(use_acb=True, label="acb")
|
||||
print(f" Done: {time.time()-t1:.0f}s")
|
||||
|
||||
|
||||
# === Per-date comparison ===
|
||||
print(f"\n{'='*90}")
|
||||
print(f"{'DATE':<12} {'BASE PnL':>10} {'ACB PnL':>10} {'DELTA':>10} {'BASE CAP':>10} {'ACB CAP':>10} {'CUT%':>6} {'BASE DD%':>9} {'ACB DD%':>9}")
|
||||
print(f"{'='*90}")
|
||||
|
||||
for sb, sa in zip(stats_base, stats_acb):
|
||||
marker = ""
|
||||
if abs(sb['pnl']) > 200:
|
||||
marker = " ***" if sb['pnl'] < 0 else " ++"
|
||||
print(f"{sb['date']:<12} {sb['pnl']:>+10.2f} {sa['pnl']:>+10.2f} {sa['pnl']-sb['pnl']:>+10.2f} "
|
||||
f"{sb['capital']:>10.2f} {sa['capital']:>10.2f} {sa['acb_cut']:>5.0f}% "
|
||||
f"{sb['dd_pct']:>8.2f}% {sa['dd_pct']:>8.2f}%{marker}")
|
||||
|
||||
# Identify loss days where ACB helped
|
||||
print(f"\n--- LOSS DAYS WHERE ACB HELPED ---")
|
||||
for sb, sa in zip(stats_base, stats_acb):
|
||||
if sb['pnl'] < 0 and sa['pnl'] > sb['pnl']:
|
||||
saved = sa['pnl'] - sb['pnl']
|
||||
print(f" {sb['date']}: base={sb['pnl']:+.2f}, acb={sa['pnl']:+.2f}, SAVED ${saved:+.2f}, cut={sa['acb_cut']:.0f}%")
|
||||
|
||||
print(f"\n--- WIN DAYS WHERE ACB HURT ---")
|
||||
for sb, sa in zip(stats_base, stats_acb):
|
||||
if sb['pnl'] > 0 and sa['pnl'] < sb['pnl']:
|
||||
cost = sa['pnl'] - sb['pnl']
|
||||
print(f" {sb['date']}: base={sb['pnl']:+.2f}, acb={sa['pnl']:+.2f}, COST ${cost:+.2f}, cut={sa['acb_cut']:.0f}%")
|
||||
|
||||
|
||||
# === Summary ===
|
||||
def summarize(label, engine, max_dd, peak, stats):
|
||||
trades = engine.trade_history
|
||||
wins = [t for t in trades if t.pnl_absolute > 0]
|
||||
losses = [t for t in trades if t.pnl_absolute <= 0]
|
||||
gross_win = sum(t.pnl_absolute for t in wins) if wins else 0
|
||||
gross_loss = abs(sum(t.pnl_absolute for t in losses)) if losses else 0
|
||||
pf_val = gross_win / gross_loss if gross_loss > 0 else float("inf")
|
||||
|
||||
win_days = sum(1 for s in stats if s['pnl'] > 0)
|
||||
loss_days = sum(1 for s in stats if s['pnl'] < 0)
|
||||
|
||||
# Sharpe (annualized from daily returns)
|
||||
daily_rets = [s['roi_pct'] for s in stats]
|
||||
sharpe = np.mean(daily_rets) / np.std(daily_rets) * np.sqrt(365) if np.std(daily_rets) > 0 else 0
|
||||
|
||||
print(f"\n{'='*50}")
|
||||
print(f" {label}")
|
||||
print(f"{'='*50}")
|
||||
print(f"Trades: {len(trades)}, WR: {len(wins)/len(trades)*100:.1f}%")
|
||||
print(f"PF: {pf_val:.3f}")
|
||||
print(f"ROI: {(engine.capital - 25000) / 25000 * 100:+.2f}%")
|
||||
print(f"Final capital: ${engine.capital:.2f}")
|
||||
print(f"Peak capital: ${peak:.2f}")
|
||||
print(f"MAX DRAWDOWN: {max_dd:.2f}%")
|
||||
print(f"Sharpe (ann.): {sharpe:.2f}")
|
||||
print(f"Win days: {win_days}, Loss days: {loss_days}")
|
||||
print(f"Fees: {engine.total_fees:.2f}")
|
||||
|
||||
summarize("BASELINE (no ACB)", eng_base, dd_base, peak_base, stats_base)
|
||||
summarize("WITH ACB v5", eng_acb, dd_acb, peak_acb, stats_acb)
|
||||
|
||||
# Delta summary
|
||||
base_roi = (eng_base.capital - 25000) / 25000 * 100
|
||||
acb_roi = (eng_acb.capital - 25000) / 25000 * 100
|
||||
print(f"\n{'='*50}")
|
||||
print(f" DELTA SUMMARY")
|
||||
print(f"{'='*50}")
|
||||
print(f"ROI: {base_roi:+.2f}% -> {acb_roi:+.2f}% ({acb_roi-base_roi:+.2f}%)")
|
||||
print(f"Max DD: {dd_base:.2f}% -> {dd_acb:.2f}% ({dd_acb-dd_base:+.2f}%)")
|
||||
print(f"Capital: ${eng_base.capital:.2f} -> ${eng_acb.capital:.2f} (${eng_acb.capital-eng_base.capital:+.2f})")
|
||||
|
||||
print(f"\nTotal time: {time.time() - t0:.0f}s")
|
||||
Reference in New Issue
Block a user