88 lines
2.9 KiB
Python
88 lines
2.9 KiB
Python
|
|
import sys
|
||
|
|
from pathlib import Path
|
||
|
|
import json
|
||
|
|
import numpy as np
|
||
|
|
import time
|
||
|
|
import gc
|
||
|
|
import pandas as pd
|
||
|
|
|
||
|
|
HCM_DIR = Path(r"C:\Users\Lenovo\Documents\- DOLPHIN NG HD HCM TSF Predict")
|
||
|
|
sys.path.insert(0, str(HCM_DIR / 'nautilus_dolphin'))
|
||
|
|
sys.path.insert(0, str(HCM_DIR / 'nautilus_dolphin' / 'dvae'))
|
||
|
|
|
||
|
|
from exp_shared import ensure_jit, load_data, ENGINE_KWARGS, META_COLS
|
||
|
|
from nautilus_dolphin.nautilus.proxy_boost_engine import create_d_liq_engine
|
||
|
|
from nautilus_dolphin.nautilus.adaptive_circuit_breaker import AdaptiveCircuitBreaker
|
||
|
|
|
||
|
|
def run_monte_carlo(n_iters=5):
|
||
|
|
print("="*60)
|
||
|
|
print(f"MONTE CARLO SIMULATION: 56-DAY DATA ({n_iters} iterations)")
|
||
|
|
print("="*60)
|
||
|
|
ensure_jit()
|
||
|
|
d = load_data()
|
||
|
|
|
||
|
|
# 1. Deterministic Baseline (Iteration 0)
|
||
|
|
# 2. Stochastic Range (Iterations 1-N)
|
||
|
|
# Target ROI Range from Silver/Gold: [55%, 181%]
|
||
|
|
|
||
|
|
mc_results = []
|
||
|
|
|
||
|
|
for i in range(n_iters):
|
||
|
|
seed = 42 + i
|
||
|
|
print(f"\nIteration {i+1}/{n_iters} (Seed: {seed})")
|
||
|
|
|
||
|
|
# Stochastic Unlock
|
||
|
|
kw = ENGINE_KWARGS.copy()
|
||
|
|
kw.update({
|
||
|
|
'seed': seed,
|
||
|
|
'sp_maker_entry_rate': 0.62,
|
||
|
|
'sp_maker_exit_rate': 0.50,
|
||
|
|
'use_sp_fees': True,
|
||
|
|
'use_sp_slippage': True
|
||
|
|
})
|
||
|
|
|
||
|
|
eng = create_d_liq_engine(**kw)
|
||
|
|
eng.set_ob_engine(d['ob_eng'])
|
||
|
|
|
||
|
|
acb = AdaptiveCircuitBreaker()
|
||
|
|
acb.preload_w750(d['date_strings'])
|
||
|
|
eng.set_acb(acb)
|
||
|
|
|
||
|
|
t0 = time.time()
|
||
|
|
for j, pf in enumerate(d['parquet_files']):
|
||
|
|
df = pd.read_parquet(pf)
|
||
|
|
acols = [c for c in df.columns if c not in META_COLS]
|
||
|
|
|
||
|
|
# Simple dvol for gating
|
||
|
|
bp = df['BTCUSDT'].values if 'BTCUSDT' in df.columns else None
|
||
|
|
dvol = np.zeros(len(df))
|
||
|
|
if bp is not None:
|
||
|
|
rets = np.diff(bp)/bp[:-1]
|
||
|
|
for k in range(50, len(bp)):
|
||
|
|
dvol[k] = np.std(rets[k-50:k])
|
||
|
|
|
||
|
|
vol_ok = dvol > d['vol_p60']
|
||
|
|
eng.process_day(pf.stem, df, acols, vol_regime_ok=vol_ok)
|
||
|
|
del df
|
||
|
|
gc.collect()
|
||
|
|
|
||
|
|
roi = (eng.capital - 25000.0) / 25000.0 * 100.0
|
||
|
|
n_trades = len(eng.trade_history)
|
||
|
|
elapsed = time.time() - t0
|
||
|
|
|
||
|
|
print(f" Result: ROI={roi:.2f}% | Trades={n_trades} | t={elapsed:.0f}s")
|
||
|
|
mc_results.append({'roi': roi, 'trades': n_trades, 'seed': seed})
|
||
|
|
|
||
|
|
rois = [r['roi'] for r in mc_results]
|
||
|
|
print("\n" + "="*60)
|
||
|
|
print("MONTE CARLO STATISTICS")
|
||
|
|
print("="*60)
|
||
|
|
print(f" Range: [{min(rois):.2f}%, {max(rois):.2f}%]")
|
||
|
|
print(f" Mean: {np.mean(rois):.2f}%")
|
||
|
|
print(f" Median: {np.median(rois):.2f}%")
|
||
|
|
print(f" StdDev: {np.std(rois):.2f}%")
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
# Reduced iterations to 3 for initial validation to avoid perceived lockup
|
||
|
|
run_monte_carlo(n_iters=3)
|