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:
259
nautilus_dolphin/run_go1_ood.py
Executable file
259
nautilus_dolphin/run_go1_ood.py
Executable file
@@ -0,0 +1,259 @@
|
||||
"""GO1 / GO2 OOD Dress Rehearsal - Baseline run on all available parquet data.
|
||||
Run: python run_go1_ood.py
|
||||
python run_go1_ood.py --label GO2
|
||||
"""
|
||||
import sys, time, math, argparse
|
||||
from pathlib import Path
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
# Force UTF-8 output (Windows cp1252 safety)
|
||||
if hasattr(sys.stdout, 'reconfigure'):
|
||||
sys.stdout.reconfigure(encoding='utf-8')
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).parent))
|
||||
|
||||
# ── JIT warmup ─────────────────────────────────────────────────────────────
|
||||
print("Compiling numba kernels...")
|
||||
t0c = 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() - t0c:.1f}s")
|
||||
|
||||
from nautilus_dolphin.nautilus.alpha_orchestrator import NDAlphaEngine
|
||||
from nautilus_dolphin.nautilus.adaptive_circuit_breaker import AdaptiveCircuitBreaker
|
||||
|
||||
# ── Config ─────────────────────────────────────────────────────────────────
|
||||
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,
|
||||
)
|
||||
VD_THRESH = -0.02; VD_EXTREME = -0.05; CONVEXITY = 3.0
|
||||
PAPER_CAPITAL = 10000.0 # user paper-trade capital
|
||||
SIM_CAPITAL = 25000.0 # engine simulation capital
|
||||
SCALE = PAPER_CAPITAL / SIM_CAPITAL # 0.40
|
||||
|
||||
# ── Data load ──────────────────────────────────────────────────────────────
|
||||
parquet_files = sorted(VBT_DIR.glob("*.parquet"))
|
||||
parquet_files = [p for p in parquet_files if 'catalog' not in str(p)]
|
||||
|
||||
acb = AdaptiveCircuitBreaker()
|
||||
date_strings = [pf.stem for pf in parquet_files]
|
||||
acb.preload_w750(date_strings)
|
||||
|
||||
all_vols = []
|
||||
for pf in parquet_files[:2]:
|
||||
df = pd.read_parquet(pf)
|
||||
if 'BTCUSDT' not in df.columns: continue
|
||||
pr = df['BTCUSDT'].values
|
||||
for i in range(60, len(pr)):
|
||||
seg = pr[max(0, i-50):i]
|
||||
if len(seg) < 10: continue
|
||||
v = float(np.std(np.diff(seg)/seg[:-1]))
|
||||
if v > 0: all_vols.append(v)
|
||||
vol_p60 = float(np.percentile(all_vols, 60)) if all_vols else 0.0
|
||||
|
||||
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)
|
||||
|
||||
def strength_cubic(vel_div):
|
||||
if vel_div >= VD_THRESH: return 0.0
|
||||
raw = (VD_THRESH - vel_div) / (VD_THRESH - VD_EXTREME)
|
||||
return min(1.0, max(0.0, raw)) ** CONVEXITY
|
||||
|
||||
# ── Engine run ─────────────────────────────────────────────────────────────
|
||||
def run_engine():
|
||||
import gc; gc.collect()
|
||||
engine = NDAlphaEngine(**ENGINE_KWARGS)
|
||||
bar_idx = 0; ph = {}; dstats = []
|
||||
|
||||
for pf in parquet_files:
|
||||
ds = pf.stem; cs = engine.capital
|
||||
engine.regime_direction = -1
|
||||
engine.regime_dd_halt = False
|
||||
|
||||
acb_info = acb.get_dynamic_boost_for_date(ds, ob_engine=None)
|
||||
base_boost = acb_info['boost']
|
||||
beta = acb_info['beta']
|
||||
|
||||
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 len(ph[ac]) > 500: ph[ac] = ph[ac][-200:]
|
||||
if not prices: bar_idx+=1; bid+=1; continue
|
||||
vrok = False if bid < 100 else (np.isfinite(dvol[ri]) and dvol[ri] > vol_p60)
|
||||
|
||||
if beta > 0 and base_boost > 1.0:
|
||||
ss = strength_cubic(float(vd))
|
||||
engine.regime_size_mult = base_boost * (1.0 + beta * ss)
|
||||
else:
|
||||
engine.regime_size_mult = base_boost
|
||||
|
||||
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
|
||||
|
||||
dstats.append({'date': ds, 'pnl': engine.capital - cs, 'cap': engine.capital})
|
||||
|
||||
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.0
|
||||
gl = abs(sum(t.pnl_absolute for t in l)) if l else 0.0
|
||||
roi = (engine.capital - SIM_CAPITAL) / SIM_CAPITAL * 100
|
||||
pf_val = gw / gl if gl > 0 else 999.0
|
||||
dr = [s['pnl'] / SIM_CAPITAL * 100 for s in dstats]
|
||||
sharpe = np.mean(dr) / np.std(dr) * np.sqrt(365) if np.std(dr) > 0 else 0.0
|
||||
peak_cap = SIM_CAPITAL; max_dd = 0.0
|
||||
for s in dstats:
|
||||
peak_cap = max(peak_cap, s['cap'])
|
||||
dd = (peak_cap - s['cap']) / peak_cap * 100
|
||||
max_dd = max(max_dd, dd)
|
||||
wr = len(w) / len(tr) * 100 if tr else 0.0
|
||||
avg_win = float(np.mean([t.pnl_pct for t in w]) * 100) if w else 0.0
|
||||
avg_loss= float(np.mean([t.pnl_pct for t in l]) * 100) if l else 0.0
|
||||
|
||||
return {
|
||||
'roi': roi, 'pf': pf_val, 'dd': max_dd, 'sharpe': sharpe,
|
||||
'trades': len(tr), 'capital': engine.capital,
|
||||
'wr': wr, 'avg_win': avg_win, 'avg_loss': avg_loss,
|
||||
'n_wins': len(w), 'n_losses': len(l),
|
||||
'dstats': dstats,
|
||||
}
|
||||
|
||||
# ── Main ───────────────────────────────────────────────────────────────────
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--label', default='GO1', help='Run label (GO1 or GO2)')
|
||||
args = parser.parse_args()
|
||||
label = args.label.upper()
|
||||
|
||||
print(f"\n{'='*70}")
|
||||
print(f" DOLPHIN NG -- OOD DRESS REHEARSAL [{label}]")
|
||||
print(f"{'='*70}")
|
||||
print(f" Data: {date_strings[0]} to {date_strings[-1]} ({len(date_strings)} days)")
|
||||
print(f" Capital: ${PAPER_CAPITAL:,.0f} paper (sim at ${SIM_CAPITAL:,.0f}, scale={SCALE:.2f})")
|
||||
print(f" OB: BASELINE (no OB engine)")
|
||||
print(f" Mode: Eigenvalue signal only")
|
||||
print(f"{'='*70}")
|
||||
|
||||
t0 = time.time()
|
||||
r = run_engine()
|
||||
elapsed = time.time() - t0
|
||||
|
||||
net_pnl_paper = (r['capital'] - SIM_CAPITAL) * SCALE
|
||||
cap_paper = r['capital'] * SCALE
|
||||
|
||||
print(f"\n [{label}] RESULT ({elapsed:.0f}s)")
|
||||
print(f" {'─'*66}")
|
||||
print(f" ROI: {r['roi']:+.2f}%")
|
||||
print(f" PF: {r['pf']:.3f}")
|
||||
print(f" Sharpe: {r['sharpe']:.2f}")
|
||||
print(f" Max DD: {r['dd']:.2f}%")
|
||||
print(f" Trades: {r['trades']} WR: {r['wr']:.1f}% AvgW: {r['avg_win']:+.3f}% AvgL: {r['avg_loss']:+.3f}%")
|
||||
print(f" W/L: {r['n_wins']}/{r['n_losses']}")
|
||||
print(f" Capital: ${cap_paper:,.2f} (net {net_pnl_paper:+,.2f} on $10k paper)")
|
||||
print(f" {'─'*66}")
|
||||
|
||||
# Last 7 days breakdown
|
||||
tail = r['dstats'][-7:]
|
||||
print(f"\n Last {len(tail)} days daily P&L (paper $10k scale):")
|
||||
for s in tail:
|
||||
pnl_p = s['pnl'] * SCALE
|
||||
cap_p = s['cap'] * SCALE
|
||||
bar = '+' * int(abs(pnl_p) / 2) if pnl_p >= 0 else '-' * int(abs(pnl_p) / 2)
|
||||
sign = '+' if pnl_p >= 0 else ''
|
||||
print(f" {s['date']} {sign}${pnl_p:6.2f} cap ${cap_p:,.2f} {bar}")
|
||||
|
||||
print(f"\n{'='*70}\n")
|
||||
|
||||
# Save snapshot for GO2 delta calc
|
||||
import json
|
||||
snap = {
|
||||
'label': label,
|
||||
'dates': date_strings,
|
||||
'roi': r['roi'],
|
||||
'pf': r['pf'],
|
||||
'sharpe': r['sharpe'],
|
||||
'dd': r['dd'],
|
||||
'trades': r['trades'],
|
||||
'wr': r['wr'],
|
||||
'capital': r['capital'],
|
||||
'n_wins': r['n_wins'],
|
||||
'n_losses': r['n_losses'],
|
||||
'dstats': r['dstats'],
|
||||
}
|
||||
snap_path = Path(__file__).parent / f'ood_{label.lower()}_snap.json'
|
||||
with open(snap_path, 'w') as f:
|
||||
json.dump(snap, f, indent=2)
|
||||
print(f" Snapshot saved: {snap_path.name}")
|
||||
|
||||
# If GO2, load GO1 snap and print delta
|
||||
if label == 'GO2':
|
||||
go1_path = Path(__file__).parent / 'ood_go1_snap.json'
|
||||
if go1_path.exists():
|
||||
with open(go1_path) as f:
|
||||
g1 = json.load(f)
|
||||
new_days = [d for d in date_strings if d not in g1['dates']]
|
||||
d_roi = r['roi'] - g1['roi']
|
||||
d_trades = r['trades'] - g1['trades']
|
||||
d_wr = r['wr'] - g1['wr']
|
||||
d_cap = (r['capital'] - g1['capital']) * SCALE
|
||||
print(f" {'='*66}")
|
||||
print(f" DELTA vs GO1")
|
||||
print(f" {'─'*66}")
|
||||
print(f" New days ingested: {new_days if new_days else '(none -- same dataset)'}")
|
||||
print(f" dROI: {d_roi:+.2f}%")
|
||||
print(f" dTrades: {d_trades:+d}")
|
||||
print(f" dWR: {d_wr:+.2f}%")
|
||||
print(f" dCapital:{d_cap:+,.2f} (paper $10k)")
|
||||
if new_days:
|
||||
print(f"\n New day(s) P&L:")
|
||||
new_dstats = [s for s in r['dstats'] if s['date'] in new_days]
|
||||
for s in new_dstats:
|
||||
pnl_p = s['pnl'] * SCALE
|
||||
sign = '+' if pnl_p >= 0 else ''
|
||||
print(f" {s['date']} {sign}${pnl_p:.2f} (paper)")
|
||||
print(f" {'='*66}\n")
|
||||
else:
|
||||
print(" [GO2] No GO1 snapshot found -- run GO1 first.")
|
||||
Reference in New Issue
Block a user