BacktestRoutingModel#

API documentation for tradeexecutor.backtest.backtest_routing.BacktestRoutingModel Python class in Trading Strategy framework.

class BacktestRoutingModel[source]#

Bases: RoutingModel

A simple router that does not optimise the trade execution cost.

  • Able to trade on multiple exchanges

  • Able to three-way trades through predefined intermediary hops, either on the exchange itself or some outside exchange

__init__(factory_router_map, allowed_intermediary_pairs, reserve_token_address, trading_fee=None)[source]#
Parameters:
  • factory_router_map (Dict[str, Tuple[str, Optional[str]]]) – Defines router smart contracts to be used with each DEX. Each Uniswap v2 is uniquely identified by its factory contract. Addresses always lowercase.

  • allowed_intermediary_pairs (Dict[str, str]) –

    Quote token address -> pair smart contract address mapping.

    Because we hold our reserves only in one currecy e.g. BUSD and we want to trade e.g. Cake/BNB pairs, we need to whitelist BNB as an allowed intermediary token. This makes it possible to do BUSD -> BNB -> Cake trade. This set is the list of pair smart contract addresses that are allowed to be used as a hop.

  • reserve_token_address (str) – Token address of our reserve currency. Relevent for buy/sell routing. Lowercase.

  • trading_fee (Optional[float]) – The trading fee applied to all trades by default, unless a pair overrides.

Methods

__init__(factory_router_map, ...[, trading_fee])

param factory_router_map:

convert_address_dict_to_lower(address_dict)

Convert all key addresses to lowercase to avoid mix up with Ethereum address checksums

create_routing_state(universe, execution_details)

Create a new routing state for this cycle.

get_default_trading_fee()

Get the trading/LP fee applied to all trading pairs.

get_reserve_asset(pair_universe)

Translate our reserve token address tok an asset description.

perform_preflight_checks_and_logging(...)

"Checks the integrity of the routing.

pre_trade_assertions(reserve_asset_amount, ...)

Some basic assertions made at the beginning of the trade() method on child class.

route_pair(pair_universe, trading_pair)

Return Uniswap routing information (path components) for a trading pair.

settle_trade(web3, state, trade, receipts[, ...])

Post-trade

setup_internal(routing_state, trade)

Simulate trade braodcast and mark it as success.

setup_trades(routing_state, trades[, ...])

Strategy and live execution connection.

__init__(factory_router_map, allowed_intermediary_pairs, reserve_token_address, trading_fee=None)[source]#
Parameters:
  • factory_router_map (Dict[str, Tuple[str, Optional[str]]]) – Defines router smart contracts to be used with each DEX. Each Uniswap v2 is uniquely identified by its factory contract. Addresses always lowercase.

  • allowed_intermediary_pairs (Dict[str, str]) –

    Quote token address -> pair smart contract address mapping.

    Because we hold our reserves only in one currecy e.g. BUSD and we want to trade e.g. Cake/BNB pairs, we need to whitelist BNB as an allowed intermediary token. This makes it possible to do BUSD -> BNB -> Cake trade. This set is the list of pair smart contract addresses that are allowed to be used as a hop.

  • reserve_token_address (str) – Token address of our reserve currency. Relevent for buy/sell routing. Lowercase.

  • trading_fee (Optional[float]) – The trading fee applied to all trades by default, unless a pair overrides.

get_default_trading_fee()[source]#

Get the trading/LP fee applied to all trading pairs.

This is Uni v2 style fee.

Returns:

Trading fee, BPS * 10000 as a float.

If information not available return None.

Return type:

Optional[float]

setup_internal(routing_state, trade)[source]#

Simulate trade braodcast and mark it as success.

Parameters:
setup_trades(routing_state, trades, check_balances=False)[source]#

Strategy and live execution connection.

Turns abstract strategy trades to real blockchain transactions.

  • Modifies TradeExecution objects in place and associates a blockchain transaction for each

  • Signs tranactions from the hot wallet and broadcasts them to the network

Parameters:
  • check_balances – Check that the wallet has enough reserves to perform the trades before executing them. Because we are selling before buying. sometimes we do no know this until the sell tx has been completed.

  • max_slippage – Max slippaeg tolerated per trade. 0.01 is 1%.

  • routing_state (BacktestRoutingState) –

  • trades (List[TradeExecution]) –

create_routing_state(universe, execution_details)[source]#

Create a new routing state for this cycle.

Parameters:
Return type:

BacktestRoutingState