Lean  $LEAN_TAG$
DefaultExerciseModel.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15 */
16 
17 using System.Collections.Generic;
20 using static QuantConnect.Extensions;
21 
23 {
24  /// <summary>
25  /// Represents the default option exercise model (physical, cash settlement)
26  /// </summary>
28  {
29  /// <summary>
30  /// Default option exercise model for the basic equity/index option security class.
31  /// </summary>
32  /// <param name="option">Option we're trading this order</param>
33  /// <param name="order">Order to update</param>
34  public virtual IEnumerable<OrderEvent> OptionExercise(Option option, OptionExerciseOrder order)
35  {
36  var underlying = option.Underlying;
37  var utcTime = option.LocalTime.ConvertToUtc(option.Exchange.TimeZone);
38 
39  var inTheMoney = option.IsAutoExercised(underlying.Close);
40  var isAssignment = inTheMoney && option.Holdings.IsShort;
41 
42  yield return new OrderEvent(
43  order.Id,
44  option.Symbol,
45  utcTime,
46  OrderStatus.Filled,
47  GetOrderDirection(order.Quantity),
48  0.0m,
49  order.Quantity,
50  OrderFee.Zero,
51  Messages.DefaultExerciseModel.ContractHoldingsAdjustmentFillTag(inTheMoney, isAssignment, option)
52  )
53  {
54  IsAssignment = isAssignment,
55  IsInTheMoney = inTheMoney
56  };
57 
58  // TODO : Support Manual Exercise of OTM contracts [ inTheMoney = false ]
59  if (inTheMoney && option.ExerciseSettlement == SettlementType.PhysicalDelivery)
60  {
61  var exerciseQuantity = option.GetExerciseQuantity(order.Quantity);
62 
63  yield return new OrderEvent(
64  order.Id,
65  underlying.Symbol,
66  utcTime,
67  OrderStatus.Filled,
68  GetOrderDirection(exerciseQuantity),
69  option.StrikePrice,
70  exerciseQuantity,
71  OrderFee.Zero,
73  ) { IsInTheMoney = true };
74  }
75  }
76  }
77 }