core.test_hedge

def test_hedge_calculator_full_flow():

Test Complete Hedge Strategy Workflow

This integration test simulates the entire lifecycle of a hedged liquidity position. It verifies that all components (Uniswap math, Option pricing, Scenario analysis) work together to produce a coherent risk analysis.

Workflow

  1. Inputs: Define LP position ($100k in 2200-3650 range) and market params.
  2. Liquidity: Calculate $L$, initial tokens, and derived values.
  3. Scenarios: Simulate price moves from -30% to +20%.
  4. Hedge Construction: Calculate required Put Options to flatten the IL curve.
  5. Analysis: Combine LP Value, Fees, and Hedge Payoff to project final P&L.

Test Data

Inputs:

  • Current Price: 3050
  • Range: [2200, 3650]
  • Investment: 100,000 USD
  • Volatility: 70%
  • Expiry: 106 days
  • Fee Yield: 63% APR

Expected Metrics:

  1. Fees Earned: $$Fees = I \times Y \times \frac{T_{days}}{365}$$ $$Fees = 100,000 \times 0.63 \times \frac{106}{365} \approx 18,295.89$$

  2. Hedge Cost (Premium):

    • The test expects ~4,044 USD.
    • Note: This differs from the Spec Example (~8,889) because the algorithm strictly defines quantity at reference strike as 0, whereas the example manually added an ATM position. We validate against the strict algorithm.
  3. Capital at 0% Move:

    • LP Value: 100,000 (No IL at P0)
    • Fees: +18,295.89
    • Hedge Payoff: 0 (OTM puts expire worthless)
    • Total: ~118,295.89

EXCEL REPRODUCTION

This is an integration test. To verify it in Excel:

  1. Solve for Hedge Quantities:

    • Follow the "EXCEL REPRODUCTION GUIDE" in test_solver.py.
    • Use the specific inputs from this test (Spot=3050, Pmin=2200, Pmax=3650).
    • Obtain the optimal quantities ($q_1, q_2, ...$).
  2. Calculate Hedge Cost (Premium):

    • For each strike $K_i$ with quantity $q_i > 0$:
    • Calculate Put Price $P_i$ using Black-Scholes (see test_black_scholes.py).
    • Cost = $\sum (q_i \times P_i)$.
    • Verify this matches result.total_cost (~4044).
  3. Verify Scenario Capital:

    • For the 0% variation scenario (Price=3050):
    • LP Value = 100,000.
    • Fees = 18,295.89 (calculated above).
    • Hedge Payoff = 0 (all OTM puts expire worthless).
    • Total = 118,295.89.
  4. Verify Other Scenarios:

    • Pick another scenario (e.g., -20% or Price=2440).
    • LP Value: Calculate from Uniswap math (or use simulator values).
    • Fees: Constant (18,295.89).
    • Hedge Payoff: $\sum \max(K_i - 2440, 0) \times q_i$.
    • Total = LP + Fees + Payoff.