tradingstrategy.priceimpact.HistoricalXYPriceImpactCalculator class.

class HistoricalXYPriceImpactCalculator[source]#

Bases: object

Calculates the Uniswap slippage, old price and new price based on XY liquidity model.

Used for backtesting and historical price impact calculations.

The price impact model here is naive. It assumes that the trade would only use assets in a single pool. However, for any real DEX this is not the case. All DEXes implement the equivalent of Uniswap auto router also known as smart order routing (SOR). Routing finds the optimal path for the swaps between different tokens and can include three hop trades or even four hop trades to find the best price for the swapper. Thus, in real life the price impact might be less than what this model gives to you.

TODO: These fees are not yet confirmed with a live exchange.


Currently we do not have dynamic liquidity provider lp_fees data for all the pairs, as it may vary pair by pair. Thus, in your model you need to manually confirm you are using the correct lp_fees value.

__init__(liquidity_universe, lp_fee=0.003, protocol_fee=0)[source]#


__init__(liquidity_universe[, lp_fee, ...])

param lp_fees

Liquidity provider fees as 0...1 % number

calculate_price_impact(when, pair_id, ...[, ...])

What would have been a price impact if a Uniswap-style trade were executed in the past.

__init__(liquidity_universe, lp_fee=0.003, protocol_fee=0)[source]#
calculate_price_impact(when, pair_id, trade_amount, measurement=LiquiditySampleMeasure.open, max_distance=Timedelta('1 days 00:00:00'))[source]#

What would have been a price impact if a Uniswap-style trade were executed in the past.

  • measurement (LiquiditySampleMeasure) – By default, we check the liquidity based on the liquidity available at the sample openining time.

  • max_distance (Timedelta) – If the sample is too far off, then abort because of gaps in data. Depending on the candle time frame you operate, you might need to adjust max_distance to account the sample timestamp skew. For example, if you are using weekly candles, you need to set this to seven days.

  • when (Timestamp) –

  • pair_id (PrimaryKey) –

  • trade_amount (USDollarAmount) –

Return type