BacktestPricing#
API documentation for tradeexecutor.backtest.backtest_pricing.BacktestPricing Python class in Trading Strategy framework.
- class BacktestPricing[source]#
Bases:
PricingModel
Look up the historical prices.
By default, assume we can get buy/sell at open price of the timestamp
Different pricing model can be used for rebalances (more coarse) and stop losses (more granular)
This is a simple model and does not use liquidity data for the price impact estimation
We provide data_delay_tolerance to deal with potential gaps in price data
- __init__(candle_universe, routing_model, data_delay_tolerance=Timedelta('2 days 00:00:00'), candle_timepoint_kind='open', very_small_amount=Decimal('0.10'), time_bucket=None, allow_missing_fees=False, trading_fee_override=None, liquidity_universe=None)[source]#
- Parameters:
candle_universe (GroupedCandleUniverse) – Candles where our backtesing date comes from
routing_model (RoutingModel) – How do we route trades between different pairs TODO: Now ignored
data_delay_tolerance –
How long time gaps we allow in the backtesting data before aborting the backtesting with an exception. This is an safety check for bad data.
Sometimes there cannot be trades for days if the blockchain has been halted, and thus no price data available.
candle_timepoint_kind – Do we use opening or closing price in backtesting
very_small_amount – What kind o a test amount we do use for a trade when we do not know the actual size of the trade.
time_bucket (Optional[TimeBucket]) –
The granularity of the price data.
Currently used for diagnostics and debug only.
allow_missing_fees –
Allow trading pairs with missing fee information.
All trading pairs should have good fee information by default, unless dealing with legacy tests.
trading_fee_override (float | None) –
Override the trading fee with a custom fee.
liquidity_universe (tradingstrategy.liquidity.GroupedLiquidityUniverse | None) –
Used in TVL based position size limit.
Methods
__init__
(candle_universe, routing_model[, ...])- param candle_universe:
check_supported_quote_token
(pair)get_buy_price
(ts, pair, reserve)Get the price for a buy transaction.
get_mid_price
(ts, pair)Get the mid price by the candle.
get_pair_fee
(ts, pair)Figure out the fee from a pair or a routing.
get_pair_for_id
(internal_id)Look up 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 available liquidity at the opening of the day.
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.
- __init__(candle_universe, routing_model, data_delay_tolerance=Timedelta('2 days 00:00:00'), candle_timepoint_kind='open', very_small_amount=Decimal('0.10'), time_bucket=None, allow_missing_fees=False, trading_fee_override=None, liquidity_universe=None)[source]#
- Parameters:
candle_universe (GroupedCandleUniverse) – Candles where our backtesing date comes from
routing_model (RoutingModel) – How do we route trades between different pairs TODO: Now ignored
data_delay_tolerance –
How long time gaps we allow in the backtesting data before aborting the backtesting with an exception. This is an safety check for bad data.
Sometimes there cannot be trades for days if the blockchain has been halted, and thus no price data available.
candle_timepoint_kind – Do we use opening or closing price in backtesting
very_small_amount – What kind o a test amount we do use for a trade when we do not know the actual size of the trade.
time_bucket (Optional[TimeBucket]) –
The granularity of the price data.
Currently used for diagnostics and debug only.
allow_missing_fees –
Allow trading pairs with missing fee information.
All trading pairs should have good fee information by default, unless dealing with legacy tests.
trading_fee_override (float | None) –
Override the trading fee with a custom fee.
liquidity_universe (tradingstrategy.liquidity.GroupedLiquidityUniverse | None) –
Used in TVL based position size limit.
- get_pair_for_id(internal_id)[source]#
Look up a trading pair.
Useful if a strategy is only dealing with pair integer ids.
- Parameters:
internal_id (int) –
- Return type:
- 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:
- get_buy_price(ts, pair, reserve)[source]#
Get the price for a buy transaction.
- Parameters:
ts (datetime) –
pair (TradingPairIdentifier) –
- Return type:
- get_mid_price(ts, pair)[source]#
Get the mid price by the candle.
- Parameters:
ts (datetime) –
pair (TradingPairIdentifier) –
- Return type:
- 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:
pair (TradingPairIdentifier) –
quantity (Decimal) –
- Return type:
- get_pair_fee(ts, pair)[source]#
Figure out the fee from a pair or a routing.
- Parameters:
ts (datetime) –
pair (TradingPairIdentifier) –
- Return type:
- 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.