# AMM Mean Reversion Backtest

### Overview

The AMM Mean Reversion Backtest component simulates trading strategy performance using historical DEX pool data while accounting for realistic trading conditions, including:

* Price impact from pool reserves
* Gas costs and fee deductions
* Slippage simulation
* Portfolio balance tracking

### How It Works

1. **Data Retrieval:**\
   The backtest first fetches historical candle and liquidity data for the specified pair and timeframe using `TradingStrategyDataManager`. Gas data is fetched using. `GasDataManager`.
2. **Average Price Calculation:**\
   For each day (or selected interval), it computes the moving average of the asset’s price over the chosen time window.
3. **Trade Signal Generation:**
   * If current price < average price \* (1 - threshold), it’s a BUY signal.
   * If current price > average price \* (1 + threshold), it’s a SELL signal.
   * Otherwise, no trade is executed.
4. **Trade Simulation:**\
   On a trade signal, the strategy uses `AMMPriceSimulator` To:
   * Calculate the expected amount out based on current pool reserves.
   * Apply trading fees, slippage, and gas costs.
   * Update the pool’s reserves and the strategy’s balances accordingly.
5. **Performance Tracking:**\
   With each simulated trade, the system records:
   * P\&L: How balances evolve over time.
   * Slippage costs: Difference between theoretical and realized price.
   * Gas fees: Deducted from quote (or base) balances, representing real trading friction.
6. **Result Compilation:**\
   At the end of the backtest, the component produces:
   * Final portfolio value in base/quote assets and USD.
   * Total number of trades, average slippage, and cumulative gas fees.
   * Performance metrics (percentage returns, drawdowns, etc.)

### Backtest Execution Flow

<figure><img src="https://3852631627-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKuJdhzEr7LzRCKfWpwGp%2Fuploads%2FaL9Bb7zkvKqFMtmkE3Y8%2FStartBacktestLoop.jpg?alt=media&#x26;token=c94f208e-1d4e-4214-a750-27f34f29ba33" alt="" width="563"><figcaption></figcaption></figure>

### Output Metrics

After the backtest is complete, here are some key metrics available:

* **Backtest Period:**
  * Start Time, End Time
  * Total Duration
* **Configuration:**
  * Exchange Name, Chain, Trading Pair
  * Threshold, Trade Amount
* **Initial and Final Pool States:**
  * TVL, prices, and reserve amounts in USD
* **Gas Balance Summary:**
  * Total Gas Used
  * Remaining Gas balance
* **Trades Analysis:**
  * Number of buy and sell trades
  * Total trade volume in base and quote assets
  * Average prices for executed trades
* **Performance Analysis**:
  * Total portfolio value at start and end
  * Total gas fees (native and USD)
  * Slippage cost (native and USD)
  * Total P\&L and percentage return.

\




\
