BacktestExecution#

API documentation for tradeexecutor.backtest.backtest_execution.BacktestExecution Python class in Trading Strategy framework.

class BacktestExecution[source]#

Bases: ExecutionModel

Simulate trades against historical data.

__init__(wallet, max_slippage=0.01, lp_fees=0.003, stop_loss_data_available=False)[source]#
Parameters:

Methods

__init__(wallet[, max_slippage, lp_fees, ...])

create_default_routing_model(strategy_universe)

Get the default routing model for this executor.

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

Execute the trades on a simulated environment.

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 wallet

is_live_trading()

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.

simulate_leverage(state, trade)

Leverage simulation with a simulated wallet.

simulate_spot(state, trade)

Spot market translation simulation with a simulated wallet.

simulate_trade(ts, state, idx, trade)

Set backtesting trade state from planned to executed.

__init__(wallet, max_slippage=0.01, lp_fees=0.003, stop_loss_data_available=False)[source]#
Parameters:
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.

get_balance_address()[source]#

Get the address where the strat holds tokens.

Returns:

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

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

preflight_check()[source]#

Check that we can start the trade executor

Raise:

AssertionError if something is a miss

initialize()[source]#

Set up the wallet

simulate_spot(state, trade)[source]#

Spot market translation simulation with a simulated wallet.

Check that the trade “executes” against the simulated wallet

Parameters:
Returns:

(ecuted_quantity, executed_reserve, sell_amount_epsilon_fix) tuple

Raises:

OutOfSimulatedBalance – Wallet does not have enough tokens to do the trade

Return type:

Tuple[Decimal, Decimal, bool]

simulate_leverage(state, trade)[source]#

Leverage simulation with a simulated wallet.

Check that the trade “executes” against the simulated wallet

TODO: currently doesn’t support leverage long yet

Parameters:
Returns:

(ecuted_quantity, executed_reserve, sell_amount_epsilon_fix) tuple

Raises:

OutOfSimulatedBalance – Wallet does not have enough tokens to do the trade

simulate_trade(ts, state, idx, trade)[source]#

Set backtesting trade state from planned to executed.

Currently, always executes trades “perfectly” i.e. no different slipppage that was planned, etc.

Parameters:
  • ts (datetime) – Strategy cycle timestamp

  • state (State) – Current backtesting state

  • idx (int) – Index of the trade to be executed on this cycle

  • trade (TradeExecution) – The actual trade

Returns:

Executed quantity and executed reserve amounts

Return type:

Tuple[Decimal, Decimal, Decimal, Decimal]

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

Execute the trades on a simulated environment.

Calculates price impact based on historical data and fills the expected historical trade output.

Parameters:
get_routing_state_details()[source]#

Get needed details to establish a routing state.

Return type:

dict

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]