Hide navigation sidebar
Hide table of contents sidebar
Back to top
Toggle table of contents sidebar

# def on_filled_order

The lifecycle method is called when an order has been successfully filled by the broker. Use this lifecycle event to execute code when an order has been filled by the broker

Parameters:

position (Position): The updated position object related to the order symbol. If the strategy already holds 200 shares of SPY and 300 has just been filled, then position.quantity will be 500 shares otherwise if it is a new position, a new position object will be created and passed to this method. order (Order): The corresponding order object that has been filled price (float): The filled price quantity (int): The filled quantity multiplier (int): Options multiplier

    class MyStrategy(Strategy):
        def on_filled_order(self, position, order, price, quantity, multiplier):
            if order.side == "sell":
                self.log_message(f"{quantity} shares of {order.symbol} has been sold at {price}$")
            elif order.side == "buy":
                self.log_message(f"{quantity} shares of {order.symbol} has been bought at {price}$")

            self.log_message(f"Currently holding {position.quantity} of {position.symbol}")

## Reference

strategies.strategy.Strategy.on_filled_order(self, position, order, price, quantity, multiplier)

  Use this lifecycle event to execute code when an order has been filled by the broker.

  Parameters:

    - position (Position object) – The position that is being filled.

    - order (Order object) – The order that is being filled.

    - price (float) – The price of the fill.

    - quantity (int) – The quantity of the fill.

    - multiplier (float) – The multiplier of the fill.

  Return type:

    None

  Example

      >>> def on_filled_order(self, position, order, price, quantity, multiplier):
      >>>     if order.asset == "AAPL":
      >>>         self.log_message("Order for AAPL filled")
      >>>         self.log_message(f"Price: {price}")

      >>> # Update dictionary with new position
      >>> def on_filled_order(self, position, order, price, quantity, multiplier):
      >>>     if order.asset == "AAPL":
      >>>         self.log_message("Order for AAPL filled")
      >>>         self.log_message(f"Price: {price}")
      >>>         self.positions["AAPL"] = position