RoutingModel#

API documentation for tradeexecutor.strategy.routing.RoutingModel Python class in Trading Strategy framework.

class RoutingModel[source]#

Bases: ABC

Trade roouting model base class.

Nothing done here - check the subclasses.

Used directly by BacktestRoutingModel and indirectly (through EthereumRoutingModel) by UniswapV2SimpleRoutingModel and UniswapV3SimpleRoutingModel

__init__(allowed_intermediary_pairs, reserve_token_address)[source]#
Parameters:
  • addresses

    Defines router smart contracts to be used with each DEX.

    Each Uniswap v2 is uniquely identified by its factory contract. Addresses always lowercase. Factory Router map

    For Uniswap V3, addresses is a dict of factory, router, position_manager, and quoter addresses

  • 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.

  • chain_id – Store the chain id for which these routes were generated for.

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

Methods

__init__(allowed_intermediary_pairs, ...)

param addresses:

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_trades(state, trades[, ...])

Setup the trades decided by a strategy.

__init__(allowed_intermediary_pairs, reserve_token_address)[source]#
Parameters:
  • addresses

    Defines router smart contracts to be used with each DEX.

    Each Uniswap v2 is uniquely identified by its factory contract. Addresses always lowercase. Factory Router map

    For Uniswap V3, addresses is a dict of factory, router, position_manager, and quoter addresses

  • 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.

  • chain_id – Store the chain id for which these routes were generated for.

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

static convert_address_dict_to_lower(address_dict)[source]#

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

Return type:

dict

static pre_trade_assertions(reserve_asset_amount, max_slippage, target_pair, reserve_asset)[source]#

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

returns: None.

An error will be raised during method call if assertions aren’t met.

Parameters:
Return type:

None

get_reserve_asset(pair_universe)[source]#

Translate our reserve token address tok an asset description.

Parameters:

pair_universe (PandasPairUniverse) –

Return type:

AssetIdentifier

route_pair(pair_universe, trading_pair)[source]#

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

For three-way pairs, figure out the intermedia step.

Returns:

(router address, target pair, intermediate pair) tuple

Parameters:
Return type:

tuple[tradeexecutor.state.identifier.TradingPairIdentifier, Optional[tradeexecutor.state.identifier.TradingPairIdentifier]]

perform_preflight_checks_and_logging(pair_universe)[source]#

“Checks the integrity of the routing.

  • Called from check-wallet to see our routing and balances are good

Parameters:

pair_universe (PandasPairUniverse) –

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]

abstract create_routing_state(universe, execution_details)[source]#

Create a new routing state for this cycle.

Parameters:
Return type:

RoutingState

abstract setup_trades(state, trades, check_balances=False, rebroadcast=False)[source]#

Setup the trades decided by a strategy.

  • Decides the best way, or a way, to execute a trade

  • Sets up blockchain transactions needed for trades, like approves

  • Trade instances are mutated in-place

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.

  • rebroadcast – Allow rebroadcast of already broadcasted trades, but for which we did not get a receipt yet.

  • state (RoutingState) –

  • trades (List[TradeExecution]) –

Raises:

CannotExecuteTrade – If a trade cannot be executed, e.g. due to an unsupported pair or an exchange,

settle_trade(web3, state, trade, receipts, stop_on_execution_failure=False)[source]#

Post-trade

  • Read on-chain data about the execution success and performance

  • Mark trade succeed or failed

Parameters:
  • state (State) – Strategy state

  • web3

    Web3 connection.

    TODO: Breaks abstraction. Figure better way to pass this around later. Maybe create an Ethereum-specific routing parent class?

  • trade (TradeExecution) – Trade executed in this execution batch

  • receipts (Dict[HexBytes, dict]) –

    Blockchain receipts we received in this execution batch.

    Hash -> receipt mapping.

  • stop_on_execution_failure

    Raise an error if the trade failed.

    Used in unit testing.