ExecutionModel#

API documentation for tradeexecutor.strategy.execution_model.ExecutionModel Python class in Trading Strategy framework.

class ExecutionModel[source]#

Bases: ABC

Define how trades are executed.

See also tradeexecutor.strategy.mode.ExecutionMode.

Used directly by BacktestExecutionModel, and indirectly (through EthereumExecutionModel) by UniswapV2ExecutionModel and UniswapV3ExecutionModel

__init__()#

Methods

__init__()

create_default_routing_model(strategy_universe)

Get the default routing model for this executor.

execute_trades(ts, state, trades, ...[, ...])

Execute the trades determined by the algo on a designed Uniswap v2 instance.

get_balance_address()

Get the address where the strat holds tokens.

get_routing_state_details()

Get needed details to establish a routing state.

get_safe_latest_block()

Fix the block number for all checks and actions.

initialize()

Set up the execution model ready to make trades.

is_stop_loss_supported()

Do we support stop-loss/take profit functionality with this execution model?

preflight_check()

Check that we can start the trade executor

repair_unconfirmed_trades(state)

Repair unconfirmed trades.

abstract get_balance_address()[source]#

Get the address where the strat holds tokens.

Returns:

None if this executor does not use on-chain addresses.

Return type:

str | None

abstract preflight_check()[source]#

Check that we can start the trade executor

Raise:

AssertionError if something is a miss

abstract initialize()[source]#

Set up the execution model ready to make trades.

Read any on-chain, etc., data to get synced.

  • Read EVM nonce for the hot wallet from the chain

abstract get_safe_latest_block()[source]#

Fix the block number for all checks and actions.

  • At the start of each action cycle (strategy decision, position triggers) we fix ourselves to a certain block number we know is “safe” and the data in at this block number is unlike to change

  • We then perform all deposit and redemptions and accounting checks using this block number as end block, to get a

Returns:

A good safe latest block number.

Return None if the block number is irrelevant for the execution, like backtesting and such.

Return type:

int | None

abstract get_routing_state_details()[source]#

Get needed details to establish a routing state.

Return type:

RoutingStateDetails

abstract is_stop_loss_supported()[source]#

Do we support stop-loss/take profit functionality with this execution model?

  • For backtesting we need to have data stream for candles used to calculate stop loss

  • For production execution, we need to have special oracle data streams for checking real-time stop loss

Return type:

bool

abstract execute_trades(ts, state, trades, routing_model, routing_state, max_slippage=0.005, check_balances=False, rebroadcast=False)[source]#

Execute the trades determined by the algo on a designed Uniswap v2 instance.

Parameters:
  • ts (datetime) – Timestamp of the trade cycle.

  • universe – Current trading universe for this cycle.

  • state (State) – State of the trade executor.

  • trades (List[TradeExecution]) – List of trades decided by the strategy. Will be executed and modified in place.

  • routing_model (RoutingModel) – Routing model how to execute the trades

  • routing_state (RoutingState) – State of already made on-chain transactions and such on this cycle

  • max_slippage – Max slippage % allowed on trades before trade execution fails.

  • check_balances – Check that on-chain accounts have enough balance before creating transaction objects. Useful during unit tests to spot issues in trade routing.

  • rebroadcast

    This is a rebroadcast and reconfirmation of existing transactions.

    Transactions had been marked for a broadcast before, but their status is unknown.

    See tradeexecutor.ethereum.rebroadcast.

abstract repair_unconfirmed_trades(state)[source]#

Repair unconfirmed trades.

Repair trades that failed to properly broadcast or confirm due to blockchain node issues.

Returns:

List of fixed trades

Parameters:

state (State) –

Return type:

List[TradeExecution]

create_default_routing_model(strategy_universe)[source]#

Get the default routing model for this executor.

Returns:

Parameters:

strategy_universe (TradingStrategyUniverse) –

Return type:

RoutingModel