PricingModel#

API documentation for tradeexecutor.strategy.pricing_model.PricingModel Python class in Trading Strategy framework.

class PricingModel[source]#

Bases: ABC

Get a price for the asset.

Needed for various aspects

  • Revaluate portfolio positiosn

  • Estimate buy/sell price for the live trading so we can calculate slippage

  • Get the historical price in backtesting

Timestamp is passed to the pricing method. However we expect it only be honoured during the backtesting - live execution may always use the latest price.

Note

For example, in futures markets there could be different fees on buy/sell transctions.

__init__()#

Methods

__init__()

get_buy_price(ts, pair, reserve)

Get the buy price for an asset.

get_mid_price(ts, pair)

Get the mid-price for an asset.

get_pair_fee(ts, pair)

Estimate the trading/LP fees for a trading pair.

get_quote_token_tvl(timestamp, pair)

Get the raw TVL of a trading pair.

get_sell_price(ts, pair, quantity)

Get the sell price for an asset.

get_usd_tvl(timestamp, pair)

Get the TVL of a trading pair.

quantize_base_quantity(pair, quantity[, ...])

Convert any base token quantity to the native token units by its ERC-20 decimals.

set_trading_fee_override(trading_fee_override)

Set the trading fee override.

abstract get_sell_price(ts, pair, quantity)[source]#

Get the sell price for an asset.

Parameters:
  • ts (datetime) – When to get the price. Used in backtesting. Live models may ignore.

  • pair (TradingPairIdentifier) – Trading pair we are intereted in

  • quantity (Optional[Decimal]) – If the sel quantity is known, get the price with price impact.

Returns:

Price structure for the trade.

Return type:

TradePricing

abstract get_buy_price(ts, pair, reserve)[source]#

Get the buy price for an asset.

Parameters:
  • ts (datetime) – When to get the price. Used in backtesting. Live models may ignore.

  • pair (TradingPairIdentifier) – Trading pair we are intereted in

  • reserve (Optional[Decimal]) – If the buy token quantity is known, get the buy price with price impact.

Returns:

Price structure for the trade.

Return type:

TradePricing

abstract get_mid_price(ts, pair)[source]#

Get the mid-price for an asset.

Mid price is an non-trddeable price between the best ask and the best pid.

Parameters:
Returns:

The mid price for the pair at a timestamp.

Return type:

float

abstract get_pair_fee(ts, pair)[source]#

Estimate the trading/LP fees for a trading pair.

This information can come either from the exchange itself (Uni v2 compatibles), or from the trading pair (Uni v3).

The return value is used to fill the fee values for any newly opened trades.

Parameters:
  • ts (datetime) – Timestamp of the trade. Note that currently fees do not vary over time, but might do so in the future.

  • pair (TradingPairIdentifier) –

    Trading pair for which we want to have the fee.

    Can be left empty if the underlying exchange is always offering the same fee.

Returns:

The estimated trading fee, expressed as %.

Returns None if the fee information is not available. This can be different from zero fees.

Return type:

Optional[float]

get_usd_tvl(timestamp, pair)[source]#

Get the TVL of a trading pair.

Returns:

TVL in US dollar

Parameters:
Return type:

float

get_quote_token_tvl(timestamp, pair)[source]#

Get the raw TVL of a trading pair.

  • Used by tradeexecutor.strategy.tvl_size_risk.

  • Assumes to use raw data

  • Uses only half of the liquidity - namely locked quote token like USDC or WETH

  • This gives much more realistic estimation of a market depth than get_usd_tvl()

  • Optional: May not be available in all pricing model implementations

Returns:

Quote token locked in a pool

Parameters:
Return type:

Decimal

quantize_base_quantity(pair, quantity, rounding='ROUND_DOWN')[source]#

Convert any base token quantity to the native token units by its ERC-20 decimals.

Parameters:
Return type:

Decimal

set_trading_fee_override(trading_fee_override)[source]#

Set the trading fee override.

  • Override the trading fee for all tradingpairs

  • Allows to simulate different price levels and price impacts, based on the historical data with a different fee tier

  • Only useful for backtesting - in the live execution you pay whatever fees you are given by the venue

Parameters:

trading_fee_override (float | None) –

The new fee tier.

Example: 0.0030 for 30 BPS.

Set None to disable and use the trading fee from the source data.