StrategyRunner#
tradeexecutor.strategy.runner.StrategyRunner Python class in Trading Strategy framework.
- class StrategyRunner[source]#
Bases:
ABC
A base class for a strategy executor.
Each different strategy type needs its own runner. Currently we have
TODO: Make user_supplied_routing_model non-optional after eliminating legacy code.
- __init__(timed_task_context_manager, execution_model, approval_model, valuation_model_factory, sync_model, pricing_model_factory, execution_context, routing_model=None, run_state=None)[source]#
- Parameters:
timed_task_context_manager (AbstractContextManager) –
execution_model (ExecutionModel) –
approval_model (ApprovalModel) –
valuation_model_factory (ValuationModelFactory) –
pricing_model_factory (Callable[[ExecutionModel, StrategyExecutionUniverse, RoutingModel], PricingModel]) –
execution_context (ExecutionContext) –
routing_model (Optional[RoutingModel]) –
Methods
__init__
(timed_task_context_manager, ...[, ...])check_position_triggers
(clock, state, ...)Check stop loss/take profit for positions.
Do we log the strategy steps to logger?
on_clock
(clock, universe, pricing_model, ...)Perform the core strategy decision cycle.
pretick_check
(ts, universe)Called when the trade executor instance is started.
repair_state
(state)Repair unclean state issues.
report_after_execution
(clock, universe, ...)report_after_sync_and_revaluation
(clock, ...)report_before_execution
(clock, universe, ...)Strategy admin helpers to understand a live running strategy.
revalue_portfolio
(ts, state, valuation_method)Revalue portfolio based on the data.
setup_routing
(universe)Setups routing state for this cycle.
sync_portfolio
(strategy_cycle_ts, universe, ...)Adjust portfolio balances based on the external events.
tick
(strategy_cycle_timestamp, universe, ...)Execute the core functions of a strategy.
- __init__(timed_task_context_manager, execution_model, approval_model, valuation_model_factory, sync_model, pricing_model_factory, execution_context, routing_model=None, run_state=None)[source]#
- Parameters:
timed_task_context_manager (AbstractContextManager) –
execution_model (ExecutionModel) –
approval_model (ApprovalModel) –
valuation_model_factory (ValuationModelFactory) –
pricing_model_factory (Callable[[ExecutionModel, StrategyExecutionUniverse, RoutingModel], PricingModel]) –
execution_context (ExecutionContext) –
routing_model (Optional[RoutingModel]) –
- abstract pretick_check(ts, universe)[source]#
Called when the trade executor instance is started.
- Parameters:
client – Trading Strategy client to check server versions etc.
universe (StrategyExecutionUniverse) – THe currently constructed universe
ts (datetime) – Real-time clock signal or past clock timestamp in the case of unit testing
- Raises:
PreflightCheckFailed – In the case we cannot go live
- is_progress_report_needed()[source]#
Do we log the strategy steps to logger?
Disabled for backtesting to speed up
Can be enabled by hacking this function if backtesting needs debugging
- Return type:
- sync_portfolio(strategy_cycle_ts, universe, state, debug_details)[source]#
Adjust portfolio balances based on the external events.
External events include
Deposits
Withdrawals
Interest accrued
Token rebases
- Parameters:
strategy_cycle_ts (datetime) –
universe (StrategyExecutionUniverse) –
state (State) –
debug_details (dict) –
- revalue_portfolio(ts, state, valuation_method)[source]#
Revalue portfolio based on the data.
- Parameters:
ts (datetime) –
state (State) –
valuation_method (ValuationModel) –
- on_clock(clock, universe, pricing_model, state, debug_details)[source]#
Perform the core strategy decision cycle.
- Parameters:
clock (datetime) – The current cycle timestamp
universe (StrategyExecutionUniverse) – Our trading pairs and such. Refreshed before the cycle.
pricing_model (PricingModel) – When constructing trades, uses pricing model to estimate the cost of a trade.
state (State) – The current trade execution and portfolio status
debug_details (dict) –
- Returns:
List of new trades to execute
- Return type:
- report_strategy_thinking(strategy_cycle_timestamp, cycle, universe, state, trades, debug_details)[source]#
Strategy admin helpers to understand a live running strategy.
Post latest variables
Draw the single pair strategy visualisation.
- Parameters:
strategy_cycle_timestamp (datetime) – real time lock
cycle (int) – Cycle number
universe (TradingStrategyUniverse) – Currnet trading universe
trades (List[TradeExecution]) – Trades executed on this cycle
state (State) – Current execution state
debug_details (dict) – Dict of random debug stuff
- setup_routing(universe)[source]#
Setups routing state for this cycle.
- Parameters:
universe (StrategyExecutionUniverse) – The currently tradeable universe
- Returns:
Tuple(routing state, pricing model, valuation model)
- Return type:
- tick(strategy_cycle_timestamp, universe, state, debug_details, cycle_duration=None, cycle=None)[source]#
Execute the core functions of a strategy.
- Parameters:
strategy_cycle_timestamp (datetime) – Current timestamp of the execution cycle.
universe (StrategyExecutionUniverse) – Loaded trading data
state (State) – The current state of the strategy (open position, past trades, visualisation)
debug_details (dict) – Internal bunch of data used in unit testing
cycle_duration (Optional[CycleDuration]) – The currenct cycle duration (time between ticks). This may be specific in a strategy module, but also overridden for testing. This is used only for logging purposes.
execution_context – Live or backtesting
- Returns:
Debug details dictionary where different subsystems can write their diagnostics information what is happening during the dict. Mostly useful for integration testing.
- Return type:
- check_position_triggers(clock, state, universe, stop_loss_pricing_model, routing_state)[source]#
Check stop loss/take profit for positions.
Unlike trade balancing in tick()
Stop loss/take profit can occur only to any existing positions. No new positions are opened.
Trading Universe cannot change for these triggers, but remains stable between main ticks.
check_position_triggers() is much more lightweight and can be called much more frequently, even once per minute
- Returns:
List of generated stop loss trades
- Parameters:
clock (datetime) –
state (State) –
universe (StrategyExecutionUniverse) –
stop_loss_pricing_model (PricingModel) –
routing_state (RoutingState) –
- Return type: