171 lines
6.6 KiB
Python
171 lines
6.6 KiB
Python
|
|
"""PF test with numba-optimized engine against real vbt_cache parquet data."""
|
||
|
|
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
|
||
|
|
# Warm up
|
||
|
|
_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
|
||
|
|
|
||
|
|
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'}
|
||
|
|
|
||
|
|
# Vol percentiles from first 2 days
|
||
|
|
parquet_files = sorted(VBT_DIR.glob("*.parquet"))
|
||
|
|
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))
|
||
|
|
print(f"Vol p60={vol_p60:.6f}")
|
||
|
|
|
||
|
|
engine = NDAlphaEngine(
|
||
|
|
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,
|
||
|
|
)
|
||
|
|
|
||
|
|
bar_idx = 0
|
||
|
|
price_histories = {}
|
||
|
|
t0 = time.time()
|
||
|
|
|
||
|
|
for pf in parquet_files:
|
||
|
|
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)
|
||
|
|
|
||
|
|
engine.process_bar(
|
||
|
|
bar_idx=bar_idx, vel_div=float(vel_div),
|
||
|
|
prices=prices, vol_regime_ok=vol_regime_ok,
|
||
|
|
price_histories=price_histories,
|
||
|
|
)
|
||
|
|
bar_idx += 1
|
||
|
|
bars_in_date += 1
|
||
|
|
|
||
|
|
elapsed = time.time() - t0
|
||
|
|
print(f" {pf.name}: bar {bar_idx}, trades={len(engine.trade_history)}, {elapsed:.0f}s")
|
||
|
|
|
||
|
|
trades = engine.trade_history
|
||
|
|
if not trades:
|
||
|
|
print(f"\nTrades: 0")
|
||
|
|
sys.exit(0)
|
||
|
|
|
||
|
|
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")
|
||
|
|
|
||
|
|
print(f"\nTrades: {len(trades)}")
|
||
|
|
print(f"Wins: {len(wins)}, WR: {len(wins)/len(trades)*100:.1f}%")
|
||
|
|
print(f"Avg win PnL%: {np.mean([t.pnl_pct for t in wins]):.4f}" if wins else "")
|
||
|
|
print(f"Avg loss PnL%: {np.mean([t.pnl_pct for t in losses]):.4f}" if losses else "")
|
||
|
|
print(f"Avg win $: {np.mean([t.pnl_absolute for t in wins]):.2f}" if wins else "")
|
||
|
|
print(f"Avg loss $: {np.mean([t.pnl_absolute for t in losses]):.2f}" if losses else "")
|
||
|
|
print(f"Gross win: {gross_win:.2f}")
|
||
|
|
print(f"Gross loss: {-gross_loss:.2f}")
|
||
|
|
print(f"PF: {pf_val:.3f}")
|
||
|
|
print(f"Fees: {engine.total_fees:.2f}")
|
||
|
|
print(f"Final capital: ${engine.capital:.2f}")
|
||
|
|
print(f"Return: {(engine.capital - 25000) / 25000 * 100:.2f}%")
|
||
|
|
|
||
|
|
exit_dist = Counter(t.exit_reason for t in trades)
|
||
|
|
print(f"\nExit distribution: {dict(exit_dist)}")
|
||
|
|
leverages = [t.leverage for t in trades]
|
||
|
|
print(f"Avg leverage: {np.mean(leverages):.2f}")
|
||
|
|
print(f"Median leverage: {np.median(leverages):.2f}")
|
||
|
|
asset_counts = Counter(t.asset for t in trades)
|
||
|
|
print(f"Top 5 assets: {asset_counts.most_common(5)}")
|
||
|
|
print(f"Unique assets traded: {len(asset_counts)}")
|
||
|
|
|
||
|
|
tp = [t for t in trades if t.exit_reason == "FIXED_TP"]
|
||
|
|
hd = [t for t in trades if t.exit_reason == "MAX_HOLD"]
|
||
|
|
if tp:
|
||
|
|
print(f"\nTP trades: {len(tp)}, avg pnl%: {np.mean([t.pnl_pct for t in tp]):.4f}")
|
||
|
|
if hd:
|
||
|
|
hw = [t for t in hd if t.pnl_absolute > 0]
|
||
|
|
hl = [t for t in hd if t.pnl_absolute <= 0]
|
||
|
|
print(f"Hold trades: {len(hd)}, avg pnl%: {np.mean([t.pnl_pct for t in hd]):.4f}")
|
||
|
|
if hw: print(f"Hold wins: {len(hw)}, avg%: {np.mean([t.pnl_pct for t in hw]):.4f}")
|
||
|
|
if hl: print(f"Hold losses: {len(hl)}, avg%: {np.mean([t.pnl_pct for t in hl]):.4f}")
|
||
|
|
|
||
|
|
print(f"\nTotal time: {time.time() - t0:.0f}s")
|
||
|
|
print(f"Legacy ref (same data): 1774 trades, 48.5% WR, PF=1.135, +30.8%")
|