Skip to content

trading.execution.backtester.orders.stoploss_order_manager

trading.execution.backtester.orders.stoploss_order_manager

StoplossOrderManager

StoplossOrderManager(event_bus: EventBus)

Holds pending stoploss order events & publishes order events if the stoploss is met. If the stoploss event is exited manually (all shares are sold), the stoploss has no effect. Assumes only one ticker to be traded & no pyramiding.

Initializes StoplossOrderManager.

Parameters:

Name Type Description Default
event_bus EventBus

The event bus to publish order events to upon the stoploss price being met.

required
Source code in src\contango\trading\execution\backtester\orders\stoploss_order_manager.py
30
31
32
33
34
35
36
37
38
39
40
41
def __init__(self, event_bus: EventBus) -> None:
    """
    Initializes `StoplossOrderManager`.

    Args:
        event_bus: The event bus to publish order events to upon the stoploss price being met.
    """
    self._event_bus = event_bus

    self._pending_stoploss_order: StoplossOrderEvent | None = None
    self._current_portfolio_snapshot: PortfolioSnapshotEvent | None = None
    self._previous_portfolio_snapshot: PortfolioSnapshotEvent | None = None

check_if_stoploss_met

check_if_stoploss_met(market_data: MarketDataEvent) -> None

Executes the pending stoploss order when the market bar breaches the stop price. Publishes an OrderEvent upon the stoploss being reached.

Parameters:

Name Type Description Default
market_data MarketDataEvent

The current market data event (bar) for the OHLCV data.

required
Source code in src\contango\trading\execution\backtester\orders\stoploss_order_manager.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def check_if_stoploss_met(self, market_data: MarketDataEvent) -> None:
    """
    Executes the pending stoploss order when the market bar breaches the stop price.
    Publishes an `OrderEvent` upon the stoploss being reached.

    Args:
        market_data: The current market data event (bar) for the OHLCV data.
    """
    stoploss_order = self._pending_stoploss_order
    previous_portfolio = self._previous_portfolio_snapshot
    current_portfolio = self._current_portfolio_snapshot

    if previous_portfolio is None or current_portfolio is None:
        return

    # If the position is sold off in the strategy elsewhere
    if previous_portfolio.position != 0 and current_portfolio.position == 0:
        self._pending_stoploss_order = None
        return

    if stoploss_order is None:
        return

    if market_data.close < stoploss_order.stop_price:
        quantity_to_sell = -stoploss_order.quantity
        order = OrderEvent(
            timestamp=market_data.timestamp,
            quantity=quantity_to_sell,
            reason="Stoploss was met."
        )

        self._event_bus.publish(order)
        self._pending_stoploss_order = None

collect_portfolio_snapshot

collect_portfolio_snapshot(snapshot: PortfolioSnapshotEvent) -> None

Sets the latest & previous portfolio snapshots so the manager can tell whether the stop is still relevant.

Source code in src\contango\trading\execution\backtester\orders\stoploss_order_manager.py
49
50
51
52
53
54
def collect_portfolio_snapshot(self, snapshot: PortfolioSnapshotEvent) -> None:
    """
    Sets the latest & previous portfolio snapshots so the manager can tell whether the stop is still relevant.
    """
    self._previous_portfolio_snapshot = self._current_portfolio_snapshot
    self._current_portfolio_snapshot = snapshot

collect_stoploss_order_event

collect_stoploss_order_event(stoploss_order: StoplossOrderEvent) -> None

Stores the latest pending stoploss order.

Source code in src\contango\trading\execution\backtester\orders\stoploss_order_manager.py
43
44
45
46
47
def collect_stoploss_order_event(self, stoploss_order: StoplossOrderEvent) -> None:
    """
    Stores the latest pending stoploss order.
    """
    self._pending_stoploss_order = stoploss_order