ExecutionLoop#
tradeexecutor.cli.loop.ExecutionLoop class.
- class ExecutionLoop[source]#
Bases:
object
Live or backtesting trade execution loop.
- __init__(*ignore, name, command_queue, execution_model, sync_method, approval_model, pricing_model_factory, valuation_model_factory, store, client, strategy_factory, cycle_duration, stats_refresh_frequency, max_data_delay=None, reset=False, max_cycles=None, debug_dump_file=None, backtest_start=None, backtest_end=None, backtest_setup=None, backtest_stop_loss_time_bucket=None, tick_offset=datetime.timedelta(0), trade_immediately=False)[source]#
See main.py for details.
- Parameters
name (str) –
command_queue (Queue) –
execution_model (ExecutionModel) –
sync_method (Callable[[Portfolio, datetime, List[AssetIdentifier]], List[ReserveUpdateEvent]]) –
approval_model (ApprovalModel) –
pricing_model_factory (Callable[[ExecutionModel, StrategyExecutionUniverse, RoutingModel], PricingModel]) –
valuation_model_factory (ValuationModelFactory) –
store (StateStore) –
strategy_factory (Callable) –
cycle_duration (CycleDuration) –
stats_refresh_frequency (timedelta) –
backtest_stop_loss_time_bucket (Optional[TimeBucket]) –
tick_offset (timedelta) –
Methods
__init__
(*ignore, name, command_queue, ...)See main.py for details.
init_execution_model
()Initialize the state for this run.
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, state, cycle, live)Run one trade execution tick.
update_position_valuations
(clock, state, ...)Revalue positions and update statistics.
Load backtesting trading universe.
- __init__(*ignore, name, command_queue, execution_model, sync_method, approval_model, pricing_model_factory, valuation_model_factory, store, client, strategy_factory, cycle_duration, stats_refresh_frequency, max_data_delay=None, reset=False, max_cycles=None, debug_dump_file=None, backtest_start=None, backtest_end=None, backtest_setup=None, backtest_stop_loss_time_bucket=None, tick_offset=datetime.timedelta(0), trade_immediately=False)[source]#
See main.py for details.
- Parameters
name (str) –
command_queue (Queue) –
execution_model (ExecutionModel) –
sync_method (Callable[[Portfolio, datetime, List[AssetIdentifier]], List[ReserveUpdateEvent]]) –
approval_model (ApprovalModel) –
pricing_model_factory (Callable[[ExecutionModel, StrategyExecutionUniverse, RoutingModel], PricingModel]) –
valuation_model_factory (ValuationModelFactory) –
store (StateStore) –
strategy_factory (Callable) –
cycle_duration (CycleDuration) –
stats_refresh_frequency (timedelta) –
backtest_stop_loss_time_bucket (Optional[TimeBucket]) –
tick_offset (timedelta) –
- tick(unrounded_timestamp, state, cycle, live, backtesting_universe=None)[source]#
Run one trade execution tick.
- Parameters
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) –
state (State) –
cycle (int) –
live (bool) –
- Return type
- update_position_valuations(clock, state, universe)[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
clock (datetime) – Real-time or historical clock
state (State) –
universe (StrategyExecutionUniverse) –
- 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
start_ts (datetime) – When to start testing (exclusive). We test for the next available timestamp.
end_ts (datetime) – When to stop testing (exclusive).
state (State) –
universe (TradingStrategyUniverse) –
- param universe:
Trading universe containing price data for stoploss checks.