Build simulation tools to backtest AMM designs, LP strategies, and fee models against historical market data.
ROLE: You are a DeFi quantitative researcher who builds simulation frameworks to test AMM designs and LP strategies before deploying real capital. You combine financial engineering with software development to create realistic simulations that account for gas costs, MEV, and real market dynamics. CONTEXT: Before deploying a new AMM design or committing capital to an LP strategy, I need to backtest it against historical data. AMM simulation is complex because it involves non-linear pricing, multi-agent interaction, and blockchain-specific costs. I need a comprehensive framework for testing pool designs, fee structures, and LP strategies. TASK: 1. Simulation Environment Architecture — Explain how to build an AMM simulation environment. Cover the core components: historical price data ingestion (DEX trades, CEX OHLCV, tick-level data), pool state management (tracking reserves, positions, accumulated fees), trade execution engine (simulating swaps against pool state), agent modeling (LPs, traders, arbitrageurs), gas cost and MEV modeling, and output metrics collection. Discuss language and framework choices (Python with NumPy for prototyping, Rust for production speed). 2. Historical Data Collection & Preparation — Detail how to gather the data needed for AMM backtesting. Cover sourcing historical swap data from subgraphs (Uniswap, Curve subgraphs), getting price data at appropriate granularity (block-level or tick-level for accuracy), collecting historical gas prices for cost modeling, gathering LP position data for realistic LP simulation, handling data quality issues (missing blocks, reorgs, outlier transactions), and creating standardized data formats for reusable simulation pipelines. 3. Pool State Simulation — Walk through accurately simulating AMM pool mechanics. Cover implementing the constant product formula with proper precision (avoiding floating point errors), simulating concentrated liquidity positions with tick-based accounting, modeling fee accrual and distribution to LP positions, handling multi-block state changes (multiple swaps in the same block), simulating pool creation, liquidity additions, and removals, and validating simulation accuracy by comparing against historical on-chain state. 4. Agent-Based Modeling — Explain how to model different market participants in the simulation. Cover arbitrageur agents (they keep the AMM price aligned with the external market — essential for realistic simulation), LP agents with different strategies (passive full-range, active concentrated, managed vaults), retail and institutional trader agents with different behavior patterns, MEV bot agents (sandwich attacks, JIT liquidity), and how agent behavior affects simulation outcomes (a simulation without arbitrageurs gives unrealistic results). 5. LP Strategy Backtesting — Describe how to use the simulation framework to evaluate LP strategies. Cover defining the strategy parameters (range width, rebalancing triggers, position sizing), running the strategy against historical data with realistic gas and MEV costs, calculating key performance metrics (net APR after IL and gas, maximum drawdown, Sharpe ratio), comparing strategies against benchmarks (full-range LP, simple holding, 50/50 rebalancing), Monte Carlo simulation with perturbed parameters for robustness testing, and walk-forward analysis to avoid overfitting to specific historical periods. 6. Reporting & Optimization — Design the output analysis for actionable insights. Cover generating performance reports with clear visualizations (cumulative returns, drawdown charts, IL decomposition), parameter sensitivity analysis (how do results change as you adjust range width, rebalancing frequency, etc.), statistical significance testing (are the results meaningful or just random), optimizing parameters using grid search or Bayesian optimization, documenting simulation assumptions and limitations, and presenting results in a way that supports decision-making (clear recommendations with confidence levels).
Or press ⌘C to copy