""" TradeSense AI — Microstructure Conditioning Unit Tests Validates: - Point-in-time ex-ante microstructure profiles calculation - Conditional vs Unconditional reversal evaluation """ from datetime import date, timedelta import numpy as np import pandas as pd import pytest from quant.research.microstructure_conditioning import ( MicrostructureConditioningEngine, MicrostructureProfile, ConditionalReversalComparison, ) def generate_synthetic_universe(tickers: list, n_rows: int = 250) -> dict: np.random.seed(42) start_date = date(2020, 1, 1) dates = [start_date + timedelta(days=i) for i in range(n_rows)] universe = {} for idx, tk in enumerate(tickers): drift = 0.0004 + (idx * 0.0001) vol = 0.012 + (idx * 0.002) rets = np.random.normal(drift, vol, n_rows) close = 100.0 * np.cumprod(1.0 + rets) high = close * 1.01 low = close * 0.99 open_p = (high + low) / 2.0 vol_shares = np.random.randint(1000000, 5000000, n_rows) universe[tk] = pd.DataFrame({ "session": dates, "Open": open_p, "High": high, "Low": low, "Close": close, "Volume": vol_shares, }) return universe class TestMicrostructureConditioning: def test_compute_ex_ante_microstructure_profiles(self): tickers = ["AAPL", "MSFT", "NVDA", "AMZN", "GOOGL", "SPY"] universe = generate_synthetic_universe(tickers, n_rows=150) profiles = MicrostructureConditioningEngine.compute_ex_ante_microstructure_profiles( universe_dfs=universe, t=100, spy_df=universe["SPY"], ) assert len(profiles) == 5 for tk, p in profiles.items(): assert p.adv_20_millions > 0 assert p.idiosyncratic_vol_annualized > 0 assert p.turnover_ratio_proxy > 0 assert isinstance(p.is_liquid_high_vol_eligible, bool) def test_evaluate_conditional_vs_unconditional_execution(self): tickers = ["AAPL", "MSFT", "NVDA", "AMZN", "GOOGL", "META", "SPY"] universe = generate_synthetic_universe(tickers, n_rows=200) res: ConditionalReversalComparison = MicrostructureConditioningEngine.evaluate_conditional_vs_unconditional( universe_dfs=universe, horizon_sessions=5, spy_df=universe["SPY"], ) assert res.n_total_assets == 6 assert np.isfinite(res.unconditional_net_sharpe) assert np.isfinite(res.conditional_net_sharpe) assert isinstance(res.is_mechanism_hypothesis_validated, bool)