Price impact¶
Price impact calculations.
- exception tradingstrategy.priceimpact.LiquidityDataMissing¶
We try to get a price impact for a pair for which we have no data.
- exception tradingstrategy.priceimpact.NoTradingPair¶
Trading pair is missing.
- exception tradingstrategy.priceimpact.SampleTooFarOff¶
There are no samples in this timeset for a specific timepoint.
- class tradingstrategy.priceimpact.LiquiditySampleMeasure¶
For liquidity samples, which measurement we use for a price impact.
- class tradingstrategy.priceimpact.PriceImpact¶
How much price impact a potential trade would have.
Because how Uniswap v2 like DEXes operate, liquidity provider and protocol fees are included within the price impact calculations. Depending on the example these fees are not included in the calculations.
This PriceImpact data also includes separate liquidity provider and protocol fees, as for trading companies these fees might be tax deductible.
TODO: These fees are not yet confirmed with a live exchange.
- available_liquidity: tradingstrategy.types.USDollarAmount¶
Liquidity that was used for the price impact calculation, as expressed US dollars of one sided liquidity, see
XYLiquidity
.
- price_impact: float¶
How much % worse execution you get because due to in availability of the liquidity.
- delivered: tradingstrategy.types.USDollarAmount¶
How much worth of tokens you actually get for trade amount, expressed as US dollar
- lp_fees_paid: tradingstrategy.types.USDollarAmount¶
How much LP fees are paid in this transaction
- protocol_fees_paid: tradingstrategy.types.USDollarAmount¶
How much protocol fees are paid in this transaction
- cost_of_trade: tradingstrategy.types.USDollarAmount¶
How much the trade cost you totally (trade amount - delivered) This includes LP fees paid, protocol fees paid and any loss because of limited liquidity. This does not include gas fees (tx fees) for the network.
- __init__(available_liquidity: tradingstrategy.types.USDollarAmount, price_impact: float, delivered: tradingstrategy.types.USDollarAmount, lp_fees_paid: tradingstrategy.types.USDollarAmount, protocol_fees_paid: tradingstrategy.types.USDollarAmount, cost_of_trade: tradingstrategy.types.USDollarAmount) None ¶
- tradingstrategy.priceimpact.estimate_xyk_price_impact(liquidity: float, trade_amount: float, lp_fee: float, protocol_fee: float) tradingstrategy.priceimpact.PriceImpact ¶
Calculates XY liquidity model slippage.
Works for all Uniswap v2 style DEXes.
For us, price impacts are easier because we operate solely in the US dollar space.
Some price impact calculation examples to study:
https://help.sushidocs.com/products/sushiswap-exchange
https://ethereum.stackexchange.com/a/111334/620
https://dailydefi.org/articles/price-impact-and-how-to-calculate/
https://ethereum.stackexchange.com/a/111334/620
TODO: Check that calculations are consistent with SushiSwap.
- Parameters
liquidity – Liquidity expressed as USD XY liquidity model single side liquidity.
trade_amount – How much buy/sell you are doing
lp_fee – Liquidity provider fee set for the pool as %. E.g. 0.0035 for Sushi.
protocol_fee – Fees paid to the protocol, e.g. xSushi stakers
- class tradingstrategy.priceimpact.HistoricalXYPriceImpactCalculator¶
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.
Note
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: tradingstrategy.liquidity.GroupedLiquidityUniverse, lp_fee=0.003, protocol_fee=0)¶
- Parameters
lp_fees – Liquidity provider fees as 0…1 % number
- calculate_price_impact(when: pandas._libs.tslibs.timestamps.Timestamp, pair_id: tradingstrategy.types.PrimaryKey, trade_amount: tradingstrategy.types.USDollarAmount, measurement: tradingstrategy.priceimpact.LiquiditySampleMeasure = LiquiditySampleMeasure.open, max_distance: pandas._libs.tslibs.timedeltas.Timedelta = Timedelta('1 days 00:00:00')) tradingstrategy.priceimpact.PriceImpact ¶
What would have been a price impact if a Uniswap-style trade were executed in the past.
- Parameters
measurement – By default, we check the liquidity based on the liquidity available at the sample openining time.
max_distance – 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.