book
Checkout our new book! Hands on AI Trading with Python, QuantConnect, and AWS Learn More arrow

Options Models

Exercise

Introduction

If you exercise a long Option position or are assigned on your short Option position, LEAN processes an Option exercise order. The Option exercise model converts the Option exercise order into an OrderEvent.

Set Models

To set the exercise model of an Option, call the SetOptionExerciseModelset_option_exercise_model method of the Option object inside a security initializer.

public class BrokerageModelExampleAlgorithm : QCAlgorithm
{
    public override void Initialize()
    {
        // In the Initialize method, set the security initializer to set models of assets.
        // To seed the security with the last known prices, set the security seeder to FuncSecuritySeeder with the GetLastKnownPrices method.
        var securitySeeder = new FuncSecuritySeeder(GetLastKnownPrices);
        
        // In some case, for example, adding Options and Futures using Universes, we don't need to seed prices.
        // To avoid the overhead of and potential timeouts for calling the GetLastKnownPrices method, set the security seeder to SecuritySeeder.Null.
        securitySeeder = SecuritySeeder.Null;

        SetSecurityInitializer(new MySecurityInitializer(BrokerageModel, securitySeeder));
    }
}

public class MySecurityInitializer : BrokerageModelSecurityInitializer
{
    public MySecurityInitializer(IBrokerageModel brokerageModel, ISecuritySeeder securitySeeder)
        : base(brokerageModel, securitySeeder) {}    
    public override void Initialize(Security security)
    {
        // First, call the superclass definition.
        // This method sets the reality models of each security using the default reality models of the brokerage model.
        base.Initialize(security);

        // Next, overwrite the Option exercise model
        if (security.Type == SecurityType.Option) // Option type
        {
            (security as Option).SetOptionExerciseModel(new DefaultExerciseModel());
        }    }
}
class BrokerageModelExampleAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        # In the Initialize method, set the security initializer to set models of assets.
        # To seed the security with the last known prices, set the security seeder to FuncSecuritySeeder with the get_last_known_prices method.
        security_seeder = FuncSecuritySeeder(self.get_last_known_prices)
        
        # In some case, for example, adding Options and Futures using Universes, we don't need to seed prices.
        # To avoid the overhead of and potential timeouts for calling the get_last_known_prices method, set the security seeder to SecuritySeeder.NULL.
        security_seeder = SecuritySeeder.NULL

        self.set_security_initializer(MySecurityInitializer(self.brokerage_model, security_seeder))

# Outside of the algorithm class
class MySecurityInitializer(BrokerageModelSecurityInitializer):

    def __init__(self, brokerage_model: IBrokerageModel, security_seeder: ISecuritySeeder) -> None:
        super().__init__(brokerage_model, security_seeder)    
    def initialize(self, security: Security) -> None:
        # First, call the superclass definition.
        # This method sets the reality models of each security using the default reality models of the brokerage model.
        super().initialize(security)

        # Next, overwrite the Option exercise model
        if security.Type == SecurityType.OPTION: # Option type
            security.set_option_exercise_model(DefaultExerciseModel())

Default Behavior

The default Option exercise model is the DefaultExerciseModel. The DefaultExerciseModel fills exercise orders to the full quantity with zero fees and applies an order tag to represent if the order is an exercise or assignment. To view the implementation of this model, see the LEAN GitHub repository.

Model Structure

Option exercise models should implement the IOptionExerciseModel interface. The IOptionExerciseModel interface must implement the OptionExerciseoption_exercise method, which receives Option and OptionExerciseOrder objects and then returns a list of OrderEvent objects that contain the order fill information.

Option exercise models should extend the DefaultExerciseModel class. Extensions of the DefaultExerciseModel must implement the OptionExerciseoption_exercise method, which receives Option and OptionExerciseOrder objects and then returns a list of OrderEvent objects that contain the order fill information.

using QuantConnect.Orders.OptionExercise;

public class CustomOptionExerciseModelExampleAlgorithm : QCAlgorithm
{
    public override void Initialize()
    {
        var security = AddOption("SPY");
        // Set custom option exercise model for mimicking specific Brokerage most realistic actions
        (security as Option).SetOptionExerciseModel(new MyOptionExerciseModel());
    }
}

// Define the custom Option exercise model outside of the algorithm
public class MyOptionExerciseModel : IOptionExerciseModel
{
    public override IEnumerable<OrderEvent> OptionExercise(Option option, OptionExerciseOrder order)
    {
        var inTheMoney = option.IsAutoExercised(option.Underlying.Close);
        var isAssignment = inTheMoney && option.Holdings.IsShort;

        yield return new OrderEvent(
            order.Id,
            option.Symbol,
            option.LocalTime.ConvertToUtc(option.Exchange.TimeZone),
            OrderStatus.Filled,
            Extensions.GetOrderDirection(order.Quantity),
            0.0m,
            order.Quantity,
            OrderFee.Zero,
            "Tag"
        ) { IsAssignment = isAssignment };
    }
}
class CustomOptionExerciseModelExampleAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        security = self.add_option("SPY")
        # Set custom option exercise model for mimicking specific Brokerage most realistic actions
        security.set_option_exercise_model(MyOptionExerciseModel())

# Define the custom Option exercise model outside of the algorithm
class MyOptionExerciseModel(DefaultExerciseModel):
    def option_exercise(self, option: Option, order: OptionExerciseOrder) -> list[OrderEvent]:
        in_the_money = option.is_auto_exercised(option.underlying.close)
        is_assignment = in_the_money and option.holdings.is_short

        order_event = OrderEvent(
            order.id,
            option.symbol,
            Extensions.convert_to_utc(option.local_time, option.exchange.time_zone),
            OrderStatus.FILLED,
            Extensions.get_order_direction(order.quantity),
            0.0,
            order.quantity,
            OrderFee.zero,
            "Tag"
        )
        order_event.is_assignment = is_assignment
        return [ order_event ]

For a full example algorithm, see this backtestthis backtest.

OptionExerciseOrder objects have the following properties:

The following table describes the arguments of the OrderEvent constructor:

Argument Details

Argument: orderIdorder_id

Id of the parent order

Data Type: int | Default Value: -

Argument: symbol

Asset Symbol

Data Type: Symbol | Default Value: -

Argument: utcTimeutc_time

Date/time of this event

Data Type: DateTimedatetime | Default Value: -

Argument: direction

The direction of the order. The OrderDirection enumeration has the following members:

Data Type: OrderDirection | Default Value: Hold

Argument: fillPricefill_price

Fill price information if applicable

Data Type: decimalfloat | Default Value: 0

Argument: fillQuantityfill_quantity

Fill quantity

Data Type: decimalfloat | Default Value: 0

Argument: orderFeeorder_fee

The order fee. You can use OrderFee.Zero or create an OrderFee object with a custom fee.

new OrderFee(new CashAmount(0.5m, "USD"));OrderFee(CashAmount(0.5, 'USD'))

Data Type: OrderFee | Default Value: -

Argument: message

Message from the exchange

Data Type: stringstr | Default Value: ""

OrderEvent objects have the following attributes:

Examples

The following examples demonstrate some common practices for implementing a custom option exercise model.

Example 1: Cash Settlement

The following algorithm trades GOOG 30-day expiring straddle. Yet, instead of settling with the underlying stock, some brokerages will settle with cash for ITM options. To simulate this behavior, we can create a custom option exercise model.

using static QuantConnect.Extensions;
public class OptionExerciseModelAlgorithm : QCAlgorithm
{
    private Symbol _goog;

    public override void Initialize()
    {
        SetStartDate(2017, 4, 1);
        SetEndDate(2017, 6, 30);

        // Request GOOG option data for trading.
        var security = AddOption("GOOG");
        _goog = security.Symbol;
        // Filter for the 2 ATM contracts expiring in 30 days to form a straddle strategy.
        security.SetFilter((universe) => universe.IncludeWeeklys().Straddle(30));
        // Set custom option exercise model for disabling exercise through security initializer.
        SetSecurityInitializer(new MySecurityInitializer(BrokerageModel, new FuncSecuritySeeder(GetLastKnownPrices)));
    }

    public override void OnData(Slice slice)
    {
        // Open position on updated option chain data.
        if (!Portfolio.Invested && slice.OptionChains.TryGetValue(_goog, out var chain))
        {
            // Only one strike and expiry for the straddle universe.
            var strike = chain.Min(x => x.Strike);
            var expiry = chain.Min(x => x.Expiry);
            // Open the straddle position.
            var optionStrategy = OptionStrategies.Straddle(_goog, strike, expiry);
            Buy(optionStrategy, 5);
        }
    }

    private class MySecurityInitializer : BrokerageModelSecurityInitializer
    {
        public MySecurityInitializer(IBrokerageModel brokerageModel, ISecuritySeeder securitySeeder)
            : base(brokerageModel, securitySeeder) {}

        public override void Initialize(Security security)
        {
            base.Initialize(security);

            // Set the custom Option exercise model for Option securities
            if (security.Type == SecurityType.Option)
            {
                (security as Option).SetOptionExerciseModel(new MyOptionExerciseModel());
            }
        }
    }

    // Define the custom Option exercise model outside of the algorithm
    private class MyOptionExerciseModel : IOptionExerciseModel
    {
        public IEnumerable<OrderEvent> OptionExercise(Option option, OptionExerciseOrder order)
        {
            var underlying = option.Underlying;
            var utcTime = option.LocalTime.ConvertToUtc(option.Exchange.TimeZone);
            var inTheMoney = option.IsAutoExercised(underlying.Close);
            // Cash settle: using payoff.
            var payoff = option.GetIntrinsicValue(underlying.Close);

            // Only liquidate option positions, but do not add equity positions.
            yield return new OrderEvent(
                order.Id,
                option.Symbol,
                utcTime,
                OrderStatus.Filled,
                GetOrderDirection(order.Quantity),
                payoff,
                order.Quantity,
                OrderFee.Zero,
                "Option Settlement"
            )
            {
                IsInTheMoney = inTheMoney
            };
        }
    }
}
class OptionExerciseModelAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self.set_start_date(2017, 4, 1)
        self.set_end_date(2017, 6, 30)

        # Request GOOG option data for trading.
        security = self.add_option("GOOG")
        self.goog = security.symbol
        # Filter for the 2 ATM contracts expiring in 30 days to form a straddle strategy.
        security.set_filter(lambda universe: universe.include_weeklys().straddle(30))
        # Set custom option exercise model for disabling exercise through security initializer.
        self.set_security_initializer(MySecurityInitializer(self.brokerage_model, FuncSecuritySeeder(self.get_last_known_prices)))

    def on_data(self, slice: Slice) -> None:
        # Open position on updated option chain data.
        chain = slice.option_chains.get(self.goog)
        if chain and not self.portfolio.invested:
            # Only one strike and expiry for the straddle universe.
            strike = min(x.strike for x in chain)
            expiry = min(x.expiry for x in chain)
            # Open the straddle position.
            option_straddle = OptionStrategies.straddle(self.goog, strike, expiry)
            self.buy(option_straddle, 5)

class MySecurityInitializer(BrokerageModelSecurityInitializer):
    def __init__(self, brokerageModel, securitySeeder):
        super().__init__(brokerageModel, securitySeeder)

    def initialize(self, security: Security) -> None:
        super().initialize(security)

        # Set the custom Option exercise model for Option securities
        if security.type == SecurityType.OPTION:
            security.set_option_exercise_model(MyOptionExerciseModel())

# Define the custom Option exercise model outside of the algorithm
class MyOptionExerciseModel(DefaultExerciseModel):
    def option_exercise(self, option: Option, order: OptionExerciseOrder) -> list[OrderEvent]:
        underlying = option.underlying
        utc_time = Extensions.convert_to_utc(option.local_time, option.exchange.time_zone)
        in_the_money = option.is_auto_exercised(underlying.close)
        # Cash settle: using payoff.
        payoff = option.get_intrinsic_value(underlying.close)

        # Only liquidate option positions, but do not add equity positions.
        order_event = OrderEvent(
            order.id,
            option.symbol,
            utc_time,
            OrderStatus.FILLED,
            Extensions.get_order_direction(order.quantity),
            payoff,
            order.quantity,
            OrderFee.ZERO,
            "Option Settlement"
        )
        order_event.is_in_the_money = in_the_money
        return [ order_event ]

Other Examples

For more examples, see the following algorithms:

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: