StrategyRunner#

tradeexecutor.strategy.runner.StrategyRunner class.

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_method, pricing_model_factory, routing_model=None)[source]#
Parameters

Methods

__init__(timed_task_context_manager, ...[, ...])

check_position_triggers(clock, state, ...)

Check stop loss/take profit for positions.

on_clock(clock, universe, pricing_model, ...)

Perform the core strategy decision cycle.

pretick_check(ts, universe)

Called when the trade executor instance is started.

report_after_execution(clock, universe, ...)

report_after_sync_and_revaluation(clock, ...)

report_before_execution(clock, universe, ...)

report_strategy_thinking(clock, universe, ...)

Strategy runner subclass can fill in.

revalue_portfolio(ts, state, valuation_method)

Revalue portfolio based on the data.

setup_routing(universe)

Setups routing state for this cycle.

sync_portfolio(ts, universe, state, ...)

Adjust portfolio balances based on the external events.

tick(clock, universe, state, debug_details)

Perform the strategy main tick.

__init__(timed_task_context_manager, execution_model, approval_model, valuation_model_factory, sync_method, pricing_model_factory, routing_model=None)[source]#
Parameters
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

sync_portfolio(ts, universe, state, debug_details)[source]#

Adjust portfolio balances based on the external events.

External events include - Deposits - Withdrawals - Interest accrued - Token rebases

Parameters
revalue_portfolio(ts, state, valuation_method)[source]#

Revalue portfolio based on the data.

Parameters
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

List[TradeExecution]

report_strategy_thinking(clock, universe, state, trades, debug_details)[source]#

Strategy runner subclass can fill in.

By default, no-op. Override in the subclass.

Parameters
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

Tuple[RoutingState, PricingModel, ValuationModel]

tick(clock, universe, state, debug_details)[source]#

Perform the strategy main tick.

Returns

Debug details dictionary where different subsystems can write their diagnostics information what is happening during the dict. Mostly useful for integration testing.

Parameters
Return type

dict

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

Parameters