From acf3a556ece3014a65f4f7642d2efe517b1275b7 Mon Sep 17 00:00:00 2001 From: ramseshk Date: Tue, 4 Aug 2026 04:54:26 +0000 Subject: [PATCH] Regime-switching Avellaneda-Stoikov + Advanced Strategies research doc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implemented regime detection in paper trader: - Rolling 30-tick volatility classifies market as LOW_VOL/NORMAL/HIGH_VOL - A-S fill probability adapts: 25% (low vol), 15% (normal), 8% (high vol) - HIGH_VOL with spreads >$30: skip trading (adverse selection protection) - Regime shown on dashboard header with color-coded badge Added docs/ADVANCED_STRATEGIES.md — comprehensive research covering: 1. Deep Learning LOB Prediction (Transformers/TLOB) 2. Latency Arbitrage in Fragmented Markets 3. Hawkes Process OFI Modeling 4. Cross-Chain MEV Arbitrage 5. Institutional Capital Flow Arbitrage (ETF flows) 6. Hybrid Transformer + Hawkes Fusion 7. Implementation Roadmap (Phase 1-4) All strategies referenced with papers from arXiv, SSRN, and empirical studies. --- dashboard/static/index.html | 10 +- docs/ADVANCED_STRATEGIES.md | 200 ++++++++++++++++++++++++++++++++++++ live/paper_trader.py | 67 ++++++++++-- 3 files changed, 267 insertions(+), 10 deletions(-) create mode 100644 docs/ADVANCED_STRATEGIES.md diff --git a/dashboard/static/index.html b/dashboard/static/index.html index ad42a24..2081c9f 100644 --- a/dashboard/static/index.html +++ b/dashboard/static/index.html @@ -66,6 +66,9 @@ td{font-family:var(--mono);font-size:11px;padding:6px 12px;border-bottom:1px sol .data-badge{display:inline-block;font-size:9px;padding:3px 8px;border-radius:4px;font-weight:500;margin-left:8px} .data-badge.mainnet{background:rgba(168,85,247,0.15);color:var(--purple)} .data-badge.testnet{background:rgba(245,158,11,0.15);color:var(--amber)} +.data-badge.normal{background:rgba(34,197,94,0.15);color:var(--green)} +.data-badge.low_vol{background:rgba(59,130,246,0.15);color:var(--blue)} +.data-badge.high_vol{background:rgba(239,68,68,0.15);color:var(--red)} footer{text-align:center;padding:20px;font-size:10px;color:#3f3f46}footer a{color:#52525b;text-decoration:none}footer a:hover{color:var(--text)} @media(max-width:640px){ .wrap{padding:12px 8px}.top{flex-direction:column;align-items:flex-start}.totals{text-align:left;width:100%} @@ -100,7 +103,7 @@ footer{text-align:center;padding:20px;font-size:10px;color:#3f3f46}footer a{colo
-

Equity Curve per-strategy · Hyperliquid Mainnet (simulated)

+

Equity Curves per-strategy ·

Trade Log

TimeStrategySideSizePriceFeePnL
@@ -210,6 +213,11 @@ function renPaper(d){ if(!d)return; var pnl=d.total_pnl||0;document.getElementById('stpnl').textContent=(pnl>=0?'+':'')+'$'+Math.abs(pnl).toFixed(2);document.getElementById('stpnl').className='pnl '+(pnl>=0?'up':'dn'); document.getElementById('stpct').textContent='Mainnet Paper · Equity: $'+(d.total_equity||100000).toFixed(0)+' · BTC: $'+(d.btc_price||0).toLocaleString('en-US',{maximumFractionDigits:0}); + // Regime badge + var regime = d.regime || 'NORMAL'; + var regimeLabel = regime.replace('_',' ').toLowerCase(); + var regimeClass = regime.toLowerCase().replace('_','-'); + document.getElementById('paper-regime').innerHTML = 'Regime: '+regimeLabel+''; renGrid('paper-grid',d.strategies||{},d.base_equity||100000,d.reserve||30000,'paper-stats','paper-tb',paperChart,null,d.equity_history||[],d.trades||[]); // Per-strategy equity curves var seq = d.strategy_equity || {}; diff --git a/docs/ADVANCED_STRATEGIES.md b/docs/ADVANCED_STRATEGIES.md new file mode 100644 index 0000000..cdde6be --- /dev/null +++ b/docs/ADVANCED_STRATEGIES.md @@ -0,0 +1,200 @@ +# Advanced Strategy Research + +> Forward-looking strategies for next-generation quantitative trading. +> Implemented status: Regime-Switching A-S (live), others (research). + +--- + +## 1. Regime-Switching Adaptive Market Making ✅ IMPLEMENTED + +**Status:** Live in paper trading engine. + +### Concept +The classic Avellaneda-Stoikov model assumes constant market parameters (volatility, liquidity). In reality, market regimes shift — a flash crash requires radically different behavior than a quiet Sunday. + +### Implementation +- **Regime detection:** 30-tick rolling volatility classifies market as LOW_VOL (<15% ann.), NORMAL (15-60%), or HIGH_VOL (>60%) +- **Adaptive behavior:** + - LOW_VOL: fill probability 25%, aggressive spread capture + - NORMAL: baseline 15% fill probability + - HIGH_VOL: 8% fill probability, skip when spread >$30 (adverse selection protection) +- Based on research showing static makers lost catastrophically during Oct 2025 flash crash while adaptive taker strategies profited from the directional move + +### Where to find it +- `live/paper_trader.py` → `detect_regime()`, `simulate_avellaneda()` +- Dashboard → Paper Trading tab → Avellaneda-Stoikov card shows regime-dependent behavior + +--- + +## 2. Deep Learning LOB Prediction 🔬 RESEARCH + +### Concept +Transformer-based model (e.g., TLOB architecture) to forecast short-term mid-price movements from the full Limit Order Book. The collective structure of resting orders contains latent information about future liquidity provision and adverse selection risk. + +### Architecture +``` +Input: LOB snapshots (10 levels × 100 timesteps) → Transformer Encoder → Price Direction (UP/DOWN/FLAT) +``` + +### Key Papers +- TLOB: A Novel Transformer Model with Dual Attention for Stock Price Trend Prediction (arXiv) +- CatBoost on LOB features showed statistically significant alpha vs buy-and-hold + +### Implementation Path +1. Collect tick-level LOB data from Hyperliquid (via WebSocket or API) +2. Feature engineering: price levels, volumes, queue positions, order flow imbalance +3. Train Transformer on GPU (A100 recommended) +4. Deploy with ONNX Runtime for inference +5. FPGA acceleration for sub-ms parsing → order placement pipeline + +### Challenges +- GPU resources for training (estimated 40h on A100 for 1 month of data) +- Real-time inference latency must be <1ms for HFT +- Model drift requires periodic retraining + +--- + +## 3. Latency Arbitrage in Fragmented Markets 🔬 RESEARCH + +### Concept +Exploit microsecond/nanosecond price discrepancies across multiple trading venues. Modern equity markets have 12+ lit venues for the same security. + +### Economics +- Cboe recently reduced latency from 50μs to 20μs — the arms race continues +- Latency arbitrage contributes to tighter spreads and faster price discovery but may increase volatility +- Success depends on physical proximity, not prediction accuracy + +### Infrastructure Requirements +- Co-location in exchange data centers (NYSE Mahwah, CME Aurora, etc.) +- FPGA-based market data parsing (sub-μs) +- Direct market access to all relevant venues +- Microwave networks between NJ and Chicago data centers + +### Regulatory +- SEC Rule 611 (Order Protection Rule) requires best execution +- Latency arbitrage is legal but faces increasing scrutiny as a "tax" on slower participants + +### Backtesting Caveat +Must model venue-specific latencies and network delays; naive backtests overstate profitability by 100-1000x. + +--- + +## 4. Hawkes Process Order Flow Imbalance 🔬 RESEARCH + +### Concept +Model order arrivals as self-exciting point processes. Unlike Poisson (independent arrivals), Hawkes processes recognize trade clustering — a large buy triggers a cascade of further buying. This produces a mathematically rigorous Order Flow Imbalance (OFI) signal. + +### Mathematical Foundation +``` +λ(t) = μ + Σ α·e^(-β(t-t_i)) for t_i < t +``` +Where μ is baseline intensity, α is self-excitation, β is decay rate. + +### Empirical Validation +- Systematic study on NIFTY50 showed consistent predictive power (SSRN) +- Multiple academic papers demonstrate OFI forecasting with Hawkes processes + +### Implementation +- Continuous calibration of Hawkes parameters on incoming trade data +- Compute predicted OFI → directional signal +- Less computationally intensive than deep learning but more rigorous than simple OFI + +### Regime Sensitivity +- Excels during trending/volatile periods (pronounced order clustering) +- Underperforms in quiet, range-bound markets (Hawkes degenerates toward Poisson) + +--- + +## 5. Cross-Chain MEV Arbitrage 🔬 RESEARCH + +### Concept +Capture value from price discrepancies of the same asset across different blockchains. Execute transactions atomically across chains before competitors. + +### Market Size +- One-year study (Sep 2023-Aug 2024) across 9 blockchains: 242,000 cross-chain arbitrages +- Generated $9.4M profit on $466M volume + +### Types +- **SIA (Sequence-Independent Arbitrage):** Trades on separate chains independently +- **SDA (Sequence-Dependent Arbitrage):** Uses bridges to move funds, requires sequencing + +### Key Infrastructure +- Jito Bundles (Solana): auction-based MEV extraction with ordered transaction bundles +- Multiple chain RPCs and node operators +- Smart contracts for atomic execution + +### Challenges +- Cross-chain finality times vary (Solana ~0.4s, Ethereum ~12s) +- Bridge security risks +- Gas optimization critical for profitability + +--- + +## 6. Institutional Capital Flow Arbitrage 🔬 RESEARCH + +### Concept +The January 2024 Bitcoin ETF approval created a causal relationship between ETF flows and BTC price. Research shows ETF net flows explained ~95% of Bitcoin's price variance (R² ≈ 0.95) post-launch. + +### Signal Pipeline +``` +SEC Filings (Form N-CEN) → ETF Flow Data → Anticipate Market Impact → Trade +``` + +### Timing +- More medium-frequency (minutes/hours) than true HFT +- Key source: regulatory filing data parsed before market digests it + +### Market Impact +- Bitcoin correlation with S&P 500 increased significantly post-ETF +- Gold correlation remained weak — BTC adopted as "tech-like" risk asset by institutions + +--- + +## 7. Hybrid: Transformer + Hawkes Process 🔬 RESEARCH + +### Concept +Fuse deep learning pattern recognition with point process rigor. Two independent models produce signals that are combined with dynamic regime-dependent weights. + +### Architecture +``` +LOB Data ─┬─ Transformer → P(UP) ┐ + │ ├─ Weighted Fusion → Final Signal + └─ Hawkes OFI → Direction ───┘ +``` + +### Fusion Logic +- Trending markets: weight Hawkes OFI higher (order clustering signal) +- Sideways/chop: weight Transformer higher (subtle pattern detection) +- Weights determined by regime detection module + +### Advantages +- More robust than single-model approach +- Better interpretability than pure deep learning +- Mirrors real-world practice (teams combine multiple quant tools) + +### Complexity +- Requires expertise in both deep learning (PyTorch/TF) and stochastic processes +- Parallel inference of both models must stay within latency budget + +--- + +## Implementation Roadmap + +| Phase | Timeline | Focus | +|-------|----------|-------| +| **Phase 1** ✅ | Current | Regime-switching A-S, basic strategies, dashboard | +| **Phase 2** | Next | Hawkes OFI implementation, enhanced regime detection | +| **Phase 3** | Future | Deep learning LOB predictor, cross-chain MEV | +| **Phase 4** | Research | Hybrid models, ETF flow integration, latency arb | + +--- + +## References + +- TLOB: Dual Attention Transformer for LOB (arXiv: 2024) +- Hawkes Processes for High-Frequency Trading (SSRN) +- Jito Bundles: MEV on Solana (Jito Labs, 2024) +- Cross-Chain Arbitrage: 9 Blockchains, 242K Trades (arXiv: 2024) +- Post-ETF Bitcoin: Flows, Correlations, and Market Structure (arXiv: 2024) +- Flash Crash 2025: Market Maker vs Taker Performance (arXiv) +- Rule 611 and Fragmentation Economics (QuestDB/DeltaStrategy) diff --git a/live/paper_trader.py b/live/paper_trader.py index c2644bc..3df966f 100644 --- a/live/paper_trader.py +++ b/live/paper_trader.py @@ -88,6 +88,36 @@ btc_prices: deque = deque(maxlen=120) eth_prices: deque = deque(maxlen=120) funding_rates: deque = deque(maxlen=100) +# ═══════════════════════ Regime Detection ═══════════════════════ +# Uses rolling volatility to classify market regime: +# LOW_VOL: quiet markets → tight spreads, aggressive size +# NORMAL: standard conditions → baseline parameters +# HIGH_VOL: turbulence → wide spreads, reduced size, cautious signals + +current_regime = "NORMAL" +regime_confidence = 0.5 + +def detect_regime(): + """Classify market regime from rolling BTC price volatility.""" + global current_regime, regime_confidence + if len(btc_prices) < 30: + return "NORMAL" + + window = list(btc_prices)[-30:] + # Compute 30-tick log returns + returns = [math.log(window[i] / window[i-1]) for i in range(1, len(window))] + realized_vol = math.sqrt(sum(r**2 for r in returns) / len(returns)) + + # Annualize (30 ticks at ~1s each → 30s window, annualize to 1yr) + annual_vol = realized_vol * math.sqrt(365 * 24 * 60 * 60 / 30) + regime_confidence = min(0.95, max(0.2, annual_vol / 2.0)) + + if annual_vol < 0.15: # <15% annualized + return "LOW_VOL" + elif annual_vol > 0.60: # >60% annualized + return "HIGH_VOL" + return "NORMAL" + # ═══════════════════════ Mainnet Data ═══════════════════════ def get_mainnet_prices(): @@ -266,20 +296,35 @@ def simulate_fill(name: str, side: str, coin: str, price: float): # ═══════════════════════ A-S Spread Capture ═══════════════════════ def simulate_avellaneda(btc_bid, btc_ask): - """Avellaneda-Stoikov: simulate spread capture when orders are at best bid/ask.""" + """Avellaneda-Stoikov: regime-adaptive spread capture. + + Regime-dependent behavior: + LOW_VOL → fill_prob=25%, tight margins (capture small spreads frequently) + NORMAL → fill_prob=15%, baseline + HIGH_VOL → fill_prob=8%, skip if spread too wide (adverse selection risk) + """ cfg = STRATEGIES["Avellaneda-Stoikov"] if btc_bid <= 0 or btc_ask <= 0: return - # Each tick, there's a chance our quotes get hit - # On mainnet, this happens frequently. Simulate with probability. - if random.random() < 0.15: # 15% per tick = fill every ~7 seconds on average - # Our bid gets hit (we buy at bid, sell at ask later for profit) + regime = current_regime + spread = btc_ask - btc_bid + + # Regime-dependent fill probability + if regime == "LOW_VOL": + fill_prob = 0.25 + elif regime == "HIGH_VOL": + fill_prob = 0.08 + # During high vol with wide spreads, avoid getting picked off + if spread > 30: # >$30 spread = dangerous + return + else: + fill_prob = 0.15 + + if random.random() < fill_prob: if cfg["position"] <= 0: - # Buy at bid bid_fill_price = btc_bid else: - # Sell at ask (close position) bid_fill_price = btc_ask side = "BUY" if cfg["position"] <= 0 else "SELL" @@ -342,6 +387,8 @@ def write_metrics(): "status": "running", "btc_price": btc_prices[-1] if btc_prices else 0, "eth_price": eth_prices[-1] if eth_prices else 0, + "regime": current_regime, + "regime_confidence": regime_confidence, } try: with open(METRICS_FILE, "w") as f: @@ -354,7 +401,7 @@ async def main(): log.info("="*60) log.info(" FTDT Quant Lab — PAPER TRADING (Mainnet Data)") log.info(f" Capital: ${STARTING_CAPITAL:,} | Reserve: ${RESERVE:,}") - log.info(f" 7 strategies × $1,000 allocation") + log.info(f" 7 strategies × ${STRATEGIES['Order Book Imbalance']['allocation']:,.0f} allocation") log.info(f" Fees: {TAKER_FEE*100:.2f}% taker | Slippage: {SLIPPAGE_BPS} bps") log.info(f" Data: Hyperliquid MAINNET") log.info(f" Dashboard: https://ftdt.io/cv") @@ -390,6 +437,7 @@ async def main(): # Compute signals every 5 ticks if tick % 5 == 0: + current_regime = detect_regime() compute_signals() # Execute signals every 3-5 ticks @@ -443,7 +491,8 @@ async def main(): btc_now = btc_prices[-1] if btc_prices else 0 log.info( f"Tick {tick:4d} | BTC: ${btc_now:,.0f} | " - f"PnL: ${tp:+.2f} | Trades: {tr:3d} | Fees: ${tf:.2f}" + f"PnL: ${tp:+.2f} | Trades: {tr:3d} | Fees: ${tf:.2f} | " + f"Regime: {current_regime}" ) await asyncio.sleep(1)