Files
DOLPHIN/prod/money_stress_test.py

107 lines
3.6 KiB
Python
Raw Normal View History

import sys
import logging
import numpy as np
from pathlib import Path
from decimal import Decimal
from datetime import datetime, timezone
# 1. Environment Setup
_PROD_DIR = Path(__file__).resolve().parent
_ROOT_DIR = _PROD_DIR.parent
_DOLPHIN_DIR = _ROOT_DIR / "nautilus_dolphin"
sys.path.insert(0, str(_DOLPHIN_DIR))
try:
from nautilus_trader.model.objects import Money, Currency, Price, Quantity
from nautilus_dolphin.nautilus.dolphin_actor import DolphinActor
from nautilus_dolphin.nautilus.esf_alpha_orchestrator import NDAlphaEngine
LOAD_OK = True
except ImportError as e:
print(f"IMPORT ERROR: {e}")
LOAD_OK = False
def run_integrity_check():
if not LOAD_OK: return
print("=" * 70)
print("MONEY INTEGRITY & STRESS TEST HARNESS")
print("=" * 70)
usdt = Currency.from_str("USDT")
initial_val = 25000.0
# -- TEST 1: Type Compliance (Fuzzing) --
print("[1] Fuzzing Money Constructor Types...")
fuzz_types = [
("Float", 25000.0),
("String", "25000.0"),
("Decimal", Decimal("25000.0")),
("Int", 25000),
("numpy.float64", np.float64(25000.0)),
]
for label, val in fuzz_types:
try:
m = Money(val, usdt)
# Verify float conversion parity
if abs(float(m) - 25000.0) > 1e-9:
print(f" [FAIL] {label}: Precision Loss -> {float(m)}")
else:
print(f" [PASS] {label}")
except Exception as e:
print(f" [FAIL] {label}: {e}")
# -- TEST 2: Actor-Engine Coupling (Serialization & Drift) --
print("\n[2] Stressing Actor-Engine Capital Transfer...")
current_cap = Decimal("25000.0")
for day in range(1, 101): # Simulated 100 days
# Simulate PnL update (mix of Decimal and float)
# Random PnL between -500 and +1000
pnl_float = (np.random.random() * 1500) - 500
pnl_dec = Decimal(str(round(pnl_float, 8)))
# Current logic in harness:
# result['capital'] = getattr(actor.engine, 'capital', initial_capital)
# capital = Decimal(str(result['capital']))
# Test: Can we always re-initialize Money from this drift?
try:
m_test = Money(str(current_cap), usdt)
current_cap += pnl_dec
except Exception as e:
print(f" [CRIT] Day {day} Money construction failed: {e}")
break
print(f" [PASS] Completed 100-day simulation. Final Cap: {current_cap}")
# -- TEST 3: NaN/Inf Resilience --
print("\n[3] Checking Failure Modes (NaN/Inf)...")
bad_vals = [float('nan'), float('inf'), -float('inf')]
for bv in bad_vals:
try:
Money(bv, usdt)
print(f" [WARN] Money accepted {bv} (Expected Failure)")
except Exception:
print(f" [PASS] Money correctly rejected {bv}")
# -- TEST 4: Engine Numba Integration --
print("\n[4] Testing NDAlphaEngine float compliance...")
try:
# Ensure engine correctly handles a float input from a Decimal source
dec_source = Decimal("25000.12345678")
engine = NDAlphaEngine(initial_capital=float(dec_source))
if engine.capital != 25000.12345678:
print(f" [FAIL] engine.capital float precision loss: {engine.capital}")
else:
print(f" [PASS] NDAlphaEngine matches float64 precision")
except Exception as e:
print(f" [FAIL] Engine init error: {e}")
print("\n" + "=" * 70)
print("INTEGRITY CHECKS COMPLETE")
print("=" * 70)
if __name__ == "__main__":
run_integrity_check()