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:
175
nautilus_dolphin/test_pf_acb_head_aggressive.py
Executable file
175
nautilus_dolphin/test_pf_acb_head_aggressive.py
Executable file
@@ -0,0 +1,175 @@
|
||||
"""Test log_0.5 vs head-aggressive boost curve.
|
||||
|
||||
log_0.5: boost = 1.0 + 0.5 * ln(1+s) → s=2: 1.55x, s=2.5: 1.63x, s=3: 1.69x
|
||||
head_aggressive: s>=3: 2.5x, s>=2.5: 1.97x, else log_0.5
|
||||
"""
|
||||
import sys, time, math
|
||||
from pathlib import Path
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).parent))
|
||||
|
||||
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, -1, 0.01, 0.04)
|
||||
check_dc_nb(_p, 3, 1, 0.75)
|
||||
print(f" JIT: {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,
|
||||
)
|
||||
|
||||
acb = AdaptiveCircuitBreaker()
|
||||
parquet_files = sorted(VBT_DIR.glob("*.parquet"))
|
||||
acb_signals = {pf.stem: acb.get_cut_for_date(pf.stem)['signals'] for pf in parquet_files}
|
||||
|
||||
# Vol percentiles
|
||||
all_vols = []
|
||||
for pf in parquet_files[:2]:
|
||||
df = pd.read_parquet(pf)
|
||||
if 'BTCUSDT' not in df.columns: continue
|
||||
for i in range(60, len(df['BTCUSDT'].values)):
|
||||
seg = df['BTCUSDT'].values[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))
|
||||
|
||||
# Pre-load data
|
||||
pq_data = {}
|
||||
for pf in parquet_files:
|
||||
df = pd.read_parquet(pf)
|
||||
ac = [c for c in df.columns if c not in META_COLS]
|
||||
bp = df['BTCUSDT'].values if 'BTCUSDT' in df.columns else None
|
||||
dv = np.full(len(df), np.nan)
|
||||
if bp is not None:
|
||||
for i in range(50, len(bp)):
|
||||
seg = bp[max(0,i-50):i]
|
||||
if len(seg)<10: continue
|
||||
dv[i] = float(np.std(np.diff(seg)/seg[:-1]))
|
||||
pq_data[pf.stem] = (df, ac, dv)
|
||||
|
||||
# Boost curves
|
||||
def log05(s):
|
||||
return 1.0 + 0.5 * math.log1p(s) if s >= 1.0 else 1.0
|
||||
|
||||
def head_aggressive(s):
|
||||
if s >= 3.0: return 2.50
|
||||
elif s >= 2.5: return 1.97
|
||||
else: return log05(s)
|
||||
|
||||
# Print boost levels
|
||||
print("\nBoost levels:")
|
||||
for s in [0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]:
|
||||
print(f" signals={s:.1f}: log05={log05(s):.2f}x head_agg={head_aggressive(s):.2f}x")
|
||||
|
||||
def run(boost_fn):
|
||||
engine = NDAlphaEngine(**ENGINE_KWARGS)
|
||||
bar_idx = 0; ph = {}; peak = engine.capital; max_dd = 0.0; dstats = []
|
||||
for pf in parquet_files:
|
||||
ds = pf.stem; cs = engine.capital; ts = len(engine.trade_history)
|
||||
engine.regime_direction = -1
|
||||
engine.regime_size_mult = boost_fn(acb_signals[ds])
|
||||
engine.regime_dd_halt = False
|
||||
df, acols, dvol = pq_data[ds]
|
||||
bid = 0
|
||||
for ri in range(len(df)):
|
||||
row = df.iloc[ri]; vd = row.get("vel_div")
|
||||
if vd is None or not np.isfinite(vd): bar_idx+=1; bid+=1; continue
|
||||
prices = {}
|
||||
for ac in acols:
|
||||
p = row[ac]
|
||||
if p and p > 0 and np.isfinite(p):
|
||||
prices[ac] = float(p)
|
||||
if ac not in ph: ph[ac] = []
|
||||
ph[ac].append(float(p))
|
||||
if not prices: bar_idx+=1; bid+=1; continue
|
||||
vrok = False if bid < 100 else (np.isfinite(dvol[ri]) and dvol[ri] > vol_p60)
|
||||
engine.process_bar(bar_idx=bar_idx, vel_div=float(vd), prices=prices,
|
||||
vol_regime_ok=vrok, price_histories=ph)
|
||||
bar_idx+=1; bid+=1
|
||||
ce = engine.capital; peak = max(peak, ce)
|
||||
dd = (peak-ce)/peak*100 if peak>0 else 0; max_dd = max(max_dd, dd)
|
||||
dstats.append({'date':ds,'pnl':ce-cs,'cap':ce,'dd':dd,
|
||||
'mult':engine.regime_size_mult,'trades':len(engine.trade_history)-ts})
|
||||
tr = engine.trade_history
|
||||
w = [t for t in tr if t.pnl_absolute>0]; l = [t for t in tr if t.pnl_absolute<=0]
|
||||
gw = sum(t.pnl_absolute for t in w) if w else 0
|
||||
gl = abs(sum(t.pnl_absolute for t in l)) if l else 0
|
||||
dr = [s['pnl']/25000*100 for s in dstats]
|
||||
return {
|
||||
'roi':(engine.capital-25000)/25000*100, 'pf':gw/gl if gl>0 else 999,
|
||||
'dd':max_dd, 'sharpe':np.mean(dr)/np.std(dr)*np.sqrt(365) if np.std(dr)>0 else 0,
|
||||
'trades':len(tr), 'wr':len(w)/len(tr)*100 if tr else 0,
|
||||
'cap':engine.capital, 'fees':engine.total_fees,
|
||||
}, dstats
|
||||
|
||||
# Run all 3
|
||||
t0 = time.time()
|
||||
print("\n=== Running baseline ===")
|
||||
r_base, s_base = run(lambda s: 1.0)
|
||||
print(f" {time.time()-t0:.0f}s")
|
||||
|
||||
print("=== Running log_0.5 ===")
|
||||
t1 = time.time()
|
||||
r_log, s_log = run(log05)
|
||||
print(f" {time.time()-t1:.0f}s")
|
||||
|
||||
print("=== Running head_aggressive ===")
|
||||
t2 = time.time()
|
||||
r_head, s_head = run(head_aggressive)
|
||||
print(f" {time.time()-t2:.0f}s")
|
||||
|
||||
# Results
|
||||
print(f"\n{'='*80}")
|
||||
print(f"{'STRATEGY':<20} {'ROI%':>7} {'PF':>6} {'DD%':>6} {'SHARPE':>7} {'TRADES':>7} {'WR%':>6} {'CAPITAL':>10}")
|
||||
print(f"{'='*80}")
|
||||
for name, r in [("baseline", r_base), ("log_0.5", r_log), ("head_aggressive", r_head)]:
|
||||
print(f"{name:<20} {r['roi']:>+7.2f} {r['pf']:>6.3f} {r['dd']:>6.2f} {r['sharpe']:>7.2f} "
|
||||
f"{r['trades']:>7} {r['wr']:>6.1f} {r['cap']:>10.2f}")
|
||||
|
||||
# Per-date comparison for boost days only
|
||||
print(f"\n--- BOOST DAYS COMPARISON ---")
|
||||
print(f"{'DATE':<12} {'SIG':>4} {'BASE PnL':>10} {'LOG PnL':>10} {'HEAD PnL':>10} {'LOG mult':>9} {'HEAD mult':>10}")
|
||||
for sb, sl, sh in zip(s_base, s_log, s_head):
|
||||
if sl['mult'] > 1.0 or sh['mult'] > 1.0:
|
||||
print(f"{sb['date']:<12} {acb_signals[sb['date']]:>4.1f} {sb['pnl']:>+10.2f} {sl['pnl']:>+10.2f} "
|
||||
f"{sh['pnl']:>+10.2f} {sl['mult']:>8.2f}x {sh['mult']:>9.2f}x")
|
||||
|
||||
# Overfitting: half split
|
||||
mid = len(parquet_files) // 2
|
||||
print(f"\n--- OVERFITTING CHECK ---")
|
||||
for name, fn in [("baseline", lambda s:1.0), ("log_0.5", log05), ("head_aggressive", head_aggressive)]:
|
||||
# Just use full run stats split
|
||||
h1_pnl = sum(s['pnl'] for s in (s_base if name=="baseline" else s_log if name=="log_0.5" else s_head)[:mid])
|
||||
h2_pnl = sum(s['pnl'] for s in (s_base if name=="baseline" else s_log if name=="log_0.5" else s_head)[mid:])
|
||||
print(f" {name:<20} H1=${h1_pnl:>+8.2f} H2=${h2_pnl:>+8.2f} ratio={h2_pnl/h1_pnl:.2f}" if h1_pnl != 0 else f" {name}: H1=0")
|
||||
|
||||
print(f"\nDelta log_0.5: ROI {r_base['roi']:+.2f}% -> {r_log['roi']:+.2f}% ({r_log['roi']-r_base['roi']:+.2f}%)")
|
||||
print(f"Delta head_aggressive: ROI {r_base['roi']:+.2f}% -> {r_head['roi']:+.2f}% ({r_head['roi']-r_base['roi']:+.2f}%)")
|
||||
print(f"Total time: {time.time()-t0:.0f}s")
|
||||
Reference in New Issue
Block a user