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 SetOptionExerciseModel
set_option_exercise_model
method of the Option
object inside a security initializer.
// In the Initialize method, set the security initializer to seed initial the prices and models of assets. SetSecurityInitializer(new MySecurityInitializer(BrokerageModel, new FuncSecuritySeeder(GetLastKnownPrices))); 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()); } } }
# In the Initialize method, set the security initializer to seed initial the prices and models of assets. self.set_security_initializer(MySecurityInitializer(self.brokerage_model, FuncSecuritySeeder(self.get_last_known_prices))) # 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 OptionExercise
option_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 OptionExercise
option_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; // In the security initializer, set the custom Option exercise model (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 }; } }
# In the security initializer, set the custom Option exercise model 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: |
Argument: |
Argument: |
Argument: |
Argument: |
Argument: |
Argument: |
Argument: |
OrderEvent
objects have the following attributes: