RoutingModel#
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 the trading/LP fee applied to all trading pairs.
get_reserve_asset
(pair_universe)Translate our reserve token address tok an asset description.
"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.
setup_trades
(state, trades[, check_balances])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:
- 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:
reserve_asset_amount (int) –
max_slippage (float) –
target_pair (TradingPairIdentifier) –
reserve_asset (AssetIdentifier) –
- Return type:
None
- get_reserve_asset(pair_universe)[source]#
Translate our reserve token address tok an asset description.
- Parameters:
pair_universe (PandasPairUniverse) –
- Return type:
- 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:
pair_universe (PandasPairUniverse) –
trading_pair (TradingPairIdentifier) –
- 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.
- abstract create_routing_state(universe, execution_details)[source]#
Create a new routing state for this cycle.
- Parameters:
execution_details (object) – A dict of whatever connects live execution to routing.
universe (StrategyExecutionUniverse) –
- Return type:
- abstract setup_trades(state, trades, check_balances=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.
state (RoutingState) –
trades (List[TradeExecution]) –
- Raises:
CannotExecuteTrade – If a trade cannot be executed, e.g. due to an unsupported pair or an exchange,