ExecutionLoop#

tradeexecutor.cli.loop.ExecutionLoop class.

class ExecutionLoop[source]#

Bases: object

Live or backtesting trade execution loop.

This is the main loop of any strategy execution.

  • Run scheduled tasks for different areas (trade cycle, position revaluation, stop loss triggers)

  • Call ExecutionModel to perform ticking through the strategy

  • Manage the persistent state of the strategy

__init__(*ignore, name, command_queue, execution_model, execution_context, sync_method, approval_model, pricing_model_factory, valuation_model_factory, store, client, strategy_factory, cycle_duration, stats_refresh_frequency, position_trigger_check_frequency, max_data_delay=None, reset=False, max_cycles=None, debug_dump_file=None, backtest_start=None, backtest_end=None, backtest_setup=None, backtest_candle_time_frame_override=None, backtest_stop_loss_time_frame_override=None, stop_loss_check_frequency=None, tick_offset=datetime.timedelta(0), trade_immediately=False)[source]#

See main.py for details.

Parameters

Methods

__init__(*ignore, name, command_queue, ...)

See main.py for details.

check_position_triggers(ts, state, universe)

Run stop loss price checks.

init_execution_model()

Initialise the execution.

init_simulation(universe_model, runner)

Set up running on a simulated blockchain.

init_state()

Initialize the state for this run.

is_live_trading_unit_test()

Are we attempting to test live trading functionality in unit tests.

run()

The main loop of trade executor.

run_backtest(state)

Backtest loop.

run_backtest_stop_loss_checks(start_ts, ...)

Generate stop loss price checks.

run_live(state)

Run live trading cycle.

tick(unrounded_timestamp, cycle_duration, ...)

Run one trade execution tick.

update_position_valuations(clock, state, ...)

Revalue positions and update statistics.

warm_up_backtest()

Load backtesting trading universe.

__init__(*ignore, name, command_queue, execution_model, execution_context, sync_method, approval_model, pricing_model_factory, valuation_model_factory, store, client, strategy_factory, cycle_duration, stats_refresh_frequency, position_trigger_check_frequency, max_data_delay=None, reset=False, max_cycles=None, debug_dump_file=None, backtest_start=None, backtest_end=None, backtest_setup=None, backtest_candle_time_frame_override=None, backtest_stop_loss_time_frame_override=None, stop_loss_check_frequency=None, tick_offset=datetime.timedelta(0), trade_immediately=False)[source]#

See main.py for details.

Parameters
is_live_trading_unit_test()[source]#

Are we attempting to test live trading functionality in unit tests.

See test_cli_commands.py

Return type

bool

init_state()[source]#

Initialize the state for this run.

  • If we are doing live trading, load the last saved state

  • In backtesting the state is always reset. We do not support resumes for crashed backetsting.

Return type

State

init_execution_model()[source]#

Initialise the execution.

Perform preflight checks e.g. to see if our trading accounts look sane.

init_simulation(universe_model, runner)[source]#

Set up running on a simulated blockchain.

Used with tradeexecutor.testing.simulated_execution_loop to allow fine granularity manipulation of in-memory blockchain to simulate trigger conditions in testing.

Parameters
tick(unrounded_timestamp, cycle_duration, state, cycle, live, backtesting_universe=None)[source]#

Run one trade execution tick.

Parma unrounded_timestamp

The approximately time when this ticket was triggered. Alawys after the tick timestamp. Will be rounded to the nearest cycle duration timestamps.

Parameters
  • state (State) – The current state of the strategy

  • cycle (int) – The number ofthis cycle

  • cycle_duration (CycleDuration) – Cycle duration for this cycle. Either from the strategy module, or a backtest override.

  • backtesting_universe (Optional[StrategyExecutionUniverse]) – If passed, use this universe instead of trying to download and filter new one. This is shortcut for backtesting where the universe does not change between cycles (as opposite to live trading new pairs pop in to the existince).

  • unrounded_timestamp (datetime) –

  • live (bool) –

Return type

StrategyExecutionUniverse

update_position_valuations(clock, state, universe, execution_mode)[source]#

Revalue positions and update statistics.

A new statistics entry is calculated for portfolio and all of its positions and added to the state.

Parameters
check_position_triggers(ts, state, universe)[source]#

Run stop loss price checks.

Used for live stop loss check; backtesting uses optimised run_backtest_stop_loss_checks().

Parameters
Return type

List[TradeExecution]

param universe:

Trading universe containing price data for stoploss checks.

Returns

List of generated trigger trades

Parameters
Return type

List[TradeExecution]

warm_up_backtest()[source]#

Load backtesting trading universe.

Display progress bars for data downloads.

run_backtest_stop_loss_checks(start_ts, end_ts, state, universe)[source]#

Generate stop loss price checks.

Backtests may use finer grade data for stop loss signals, to be more realistic with actual trading.

Here we use the finer grade data to check the stop losses on a given time period.

Parameters
param universe:

Trading universe containing price data for stoploss checks.

run_backtest(state)[source]#

Backtest loop.

Parameters

state (State) –

Return type

dict

run_live(state)[source]#

Run live trading cycle.

Parameters

state (State) –

run()[source]#

The main loop of trade executor.

Main entry point to the loop.

  • Chooses between live and backtesting execution mode

  • Loads or creates the initial state

  • Sets up strategy runner

Returns

Debug state where each key is the cycle number

Return type

dict