Trading
The trading subpackage groups all trading domain specific components and tooling.
This is a top-level package where the majority of users will interface with the framework. Custom trading strategies can be implemented by inheriting from the Strategy base class.
class Controller
Bases: Actor
The base class for all trader controllers.
- Parameters:
- trader (Trader) – The reference to the trader instance to control.
- config (ActorConfig , optional) – The configuration for the controller
- Raises: TypeError – If config is not of type ActorConfig.
create_actor(actor: Actor, start: bool = True) → None
Add the given actor to the controlled trader.
- Parameters:
- actor (Actor) – The actor to add.
- start (bool , default True) – If the actor should be started immediately.
- Raises:
- ValueError – If actor.state is
RUNNING
orDISPOSED
. - RuntimeError – If actor is already registered with the trader.
- ValueError – If actor.state is
create_actor_from_config(actor_config: ImportableActorConfig, start: bool = True) → None
Create the actor corresponding to actor_config.
- Parameters:
- actor_config (ImportableActorConfig) – The actor config of the actor to add.
- start (bool , default True) – If the actor should be started immediately.
- Raises:
- ValueError – If actor.state is
RUNNING
orDISPOSED
. - RuntimeError – If actor is already registered with the trader.
- ValueError – If actor.state is
create_strategy(strategy: Strategy, start: bool = True) → None
Add the given strategy to the controlled trader.
- Parameters:
- strategy (Strategy) – The strategy to add.
- start (bool , default True) – If the strategy should be started immediately.
- Raises:
- ValueError – If strategy.state is
RUNNING
orDISPOSED
. - RuntimeError – If strategy is already registered with the trader.
- ValueError – If strategy.state is
create_strategy_from_config(strategy_config: ImportableStrategyConfig, start: bool = True) → None
Create the strategy corresponding to strategy_config.
- Parameters:
- strategy_config (ImportableStrategyConfig) – The strategy config of the strategy to add.
- start (bool , default True) – If the strategy should be started immediately.
- Raises:
- ValueError – If strategy.state is
RUNNING
orDISPOSED
. - RuntimeError – If strategy is already registered with the trader.
- ValueError – If strategy.state is
execute(command: Command) → None
register_base(self, PortfolioFacade portfolio, MessageBus msgbus, CacheFacade cache, Clock clock) → void
Register with a trader.
- Parameters:
- portfolio (PortfolioFacade) – The read-only portfolio for the actor.
- msgbus (MessageBus) – The message bus for the actor.
- cache (CacheFacade) – The read-only cache for the actor.
- clock (Clock) – The clock for the actor.
WARNING
System method (not intended to be called by user code).
remove_actor(actor: Actor) → None
Remove the given actor.
Will stop the actor first if state is RUNNING
.
- Parameters: actor (Actor) – The actor to remove.
- Raises: ValueError – If actor is not already registered with the trader.
remove_actor_from_id(actor_id: ComponentId) → None
Remove the actor corresponding to actor_id.
Will stop the actor first if state is RUNNING
.
- Parameters: actor_id (ComponentId) – The ID of the actor to remove.
- Raises: ValueError – If actor is not already registered with the trader.
remove_strategy(strategy: Strategy) → None
Remove the given strategy.
Will stop the strategy first if state is RUNNING
.
- Parameters: strategy (Strategy) – The strategy to remove.
- Raises: ValueError – If strategy is not already registered with the trader.
remove_strategy_from_id(strategy_id: StrategyId) → None
Remove the strategy corresponding to strategy_id.
Will stop the strategy first if state is RUNNING
.
- Parameters: strategy_id (StrategyId) – The ID of the strategy to remove.
- Raises: ValueError – If strategy is not already registered with the trader.
start_actor(actor: Actor) → None
Start the given actor.
Will log a warning if the actor is already RUNNING
.
- Raises: ValueError – If actor is not already registered with the trader.
start_actor_from_id(actor_id: ComponentId) → None
Start the actor corresponding to actor_id.
Will log a warning if the actor is already RUNNING
.
- Parameters: actor_id (ComponentId) – The ID of the actor to start.
- Raises: ValueError – If actor is not already registered with the trader.
start_strategy(strategy: Strategy) → None
Start the given strategy.
Will log a warning if the strategy is already RUNNING
.
- Raises: ValueError – If strategy is not already registered with the trader.
start_strategy_from_id(strategy_id: StrategyId) → None
Start the strategy corresponding to strategy_id.
Will log a warning if the strategy is already RUNNING
.
- Parameters: strategy_id (StrategyId) – The ID of the strategy to start.
- Raises: ValueError – If strategy is not already registered with the trader.
stop_actor(actor: Actor) → None
Stop the given actor.
Will log a warning if the actor is not RUNNING
.
- Parameters: actor (Actor) – The actor to stop.
- Raises: ValueError – If actor is not already registered with the trader.
stop_actor_from_id(actor_id: ComponentId) → None
Stop the actor corresponding to actor_id.
Will log a warning if the actor is not RUNNING
.
- Parameters: actor_id (ComponentId) – The ID of the actor to stop.
- Raises: ValueError – If actor is not already registered with the trader.
stop_strategy(strategy: Strategy) → None
Stop the given strategy.
Will log a warning if the strategy is not RUNNING
.
- Parameters: strategy (Strategy) – The strategy to stop.
- Raises: ValueError – If strategy is not already registered with the trader.
stop_strategy_from_id(strategy_id: StrategyId) → None
Stop the strategy corresponding to strategy_id.
Will log a warning if the strategy is not RUNNING
.
- Parameters: strategy_id (StrategyId) – The ID of the strategy to stop.
- Raises: ValueError – If strategy is not already registered with the trader.
class Strategy
Bases: Actor
Strategy(config: StrategyConfig | None = None)
The base class for all trading strategies.
This class allows traders to implement their own customized trading strategies. A trading strategy can configure its own order management system type, which determines how positions are handled by the ExecutionEngine.
Strategy OMS (Order Management System) types:
: - UNSPECIFIED
: No specific type has been configured, will therefore
default to the native OMS type for each venue.
HEDGING
: A position ID will be assigned for each new position which is opened per instrument.NETTING
: There will only be a single position for the strategy per instrument. The position ID naming convention is {instrument_id}-{strategy_id}.
- Parameters: config (StrategyConfig , optional) – The trading strategy configuration.
- Raises: TypeError – If config is not of type StrategyConfig.
WARNING
- This class should not be used directly, but through a concrete subclass.
- Do not call components such as clock and logger in the __init__ prior to registration.
cancel_all_orders(self, InstrumentId instrument_id, OrderSide order_side=OrderSide.NO_ORDER_SIDE, ClientId client_id=None, dict params=None) → void
Cancel all orders for this strategy for the given instrument ID.
A CancelAllOrders command will be created and then sent to both the OrderEmulator and the ExecutionEngine.
- Parameters:
- instrument_id (InstrumentId) – The instrument for the orders to cancel.
- order_side (OrderSide, default
NO_ORDER_SIDE
(both sides)) – The side of the orders to cancel. - client_id (ClientId , optional) – The specific client ID for the command.
If
None
then will be inferred from the venue in the instrument ID. - params (dict *[*str , Any ] , optional) – Additional parameters potentially used by a specific client.
cancel_gtd_expiry(self, Order order) → void
Cancel the managed GTD expiry for the given order.
If there is no current GTD expiry timer, then an error will be logged.
- Parameters: order (Order) – The order to cancel the GTD expiry for.
cancel_order(self, Order order, ClientId client_id=None, dict params=None) → void
Cancel the given order with optional routing instructions.
A CancelOrder command will be created and then sent to either the OrderEmulator or the ExecutionEngine (depending on whether the order is emulated).
- Parameters:
cancel_orders(self, list orders, ClientId client_id=None, dict params=None) → void
Batch cancel the given list of orders with optional routing instructions.
For each order in the list, a CancelOrder command will be created and added to a BatchCancelOrders command. This command is then sent to the ExecutionEngine.
Logs an error if the orders list contains local/emulated orders.
- Parameters:
- Raises:
- ValueError – If orders is empty.
- TypeError – If orders contains a type other than Order.