Lean  $LEAN_TAG$
OptionStrategy.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 using System;
17 using QuantConnect.Orders;
18 using System.Collections.Generic;
19 
21 {
22  /// <summary>
23  /// Option strategy specification class. Describes option strategy and its parameters for trading.
24  /// </summary>
25  public class OptionStrategy
26  {
27  /// <summary>
28  /// Option strategy name
29  /// </summary>
30  public string Name { get; set; }
31 
32  /// <summary>
33  /// The canonical Option symbol of the strategy
34  /// </summary>
35  public Symbol CanonicalOption { get; set; }
36 
37  /// <summary>
38  /// Underlying symbol of the strategy
39  /// </summary>
40  public Symbol Underlying { get; set; }
41 
42  /// <summary>
43  /// Option strategy legs
44  /// </summary>
45  public List<OptionLegData> OptionLegs { get; set; } = new List<OptionLegData>();
46 
47  /// <summary>
48  /// Option strategy underlying legs (usually 0 or 1 legs)
49  /// </summary>
50  public List<UnderlyingLegData> UnderlyingLegs { get; set; } = new List<UnderlyingLegData>();
51 
52  /// <summary>
53  /// Defines common properties between <see cref="OptionLegData"/> and <see cref="UnderlyingLegData"/>
54  /// </summary>
55  public abstract class LegData : Leg
56  {
57  /// <summary>
58  /// Invokes the correct handler based on the runtime type.
59  /// </summary>
60  public abstract void Invoke(Action<UnderlyingLegData> underlyingHandler, Action<OptionLegData> optionHandler);
61  }
62 
63  /// <summary>
64  /// This class is a POCO containing basic data for the option legs of the strategy
65  /// </summary>
66  public class OptionLegData : LegData
67  {
68  /// <summary>
69  /// Option right (type) of the option leg
70  /// </summary>
71  public OptionRight Right { get; set; }
72 
73  /// <summary>
74  /// Expiration date of the leg
75  /// </summary>
76  public DateTime Expiration { get; set; }
77 
78  /// <summary>
79  /// Strike price of the leg
80  /// </summary>
81  public decimal Strike { get; set; }
82 
83  /// <summary>
84  /// Creates a new instance of <see cref="OptionLegData"/> from the specified parameters
85  /// </summary>
86  public static OptionLegData Create(int quantity, Symbol symbol, decimal? orderPrice = null)
87  {
88  return new OptionLegData
89  {
90  Symbol = symbol,
91  Quantity = quantity,
92  Expiration = symbol.ID.Date,
93  OrderPrice = orderPrice,
94  Right = symbol.ID.OptionRight,
95  Strike = symbol.ID.StrikePrice
96  };
97  }
98 
99  /// <summary>
100  /// Invokes the <paramref name="optionHandler"/>
101  /// </summary>
102  public override void Invoke(Action<UnderlyingLegData> underlyingHandler, Action<OptionLegData> optionHandler)
103  {
104  optionHandler(this);
105  }
106  }
107 
108  /// <summary>
109  /// This class is a POCO containing basic data for the underlying leg of the strategy
110  /// </summary>
111  public class UnderlyingLegData : LegData
112  {
113  /// <summary>
114  /// Creates a new instance of <see cref="UnderlyingLegData"/> for the specified <paramref name="quantity"/> of underlying shares.
115  /// </summary>
116  public static UnderlyingLegData Create(int quantity, Symbol symbol, decimal? orderPrice = null)
117  {
118  var data = Create(quantity, orderPrice);
119  data.Symbol = symbol;
120  return data;
121  }
122 
123  /// <summary>
124  /// Creates a new instance of <see cref="UnderlyingLegData"/> for the specified <paramref name="quantity"/> of underlying shares.
125  /// </summary>
126  public static UnderlyingLegData Create(int quantity, decimal? orderPrice = null)
127  {
128  return new UnderlyingLegData
129  {
130  Quantity = quantity,
131  OrderPrice = orderPrice
132  };
133  }
134 
135  /// <summary>
136  /// Invokes the <paramref name="underlyingHandler"/>
137  /// </summary>
138  public override void Invoke(Action<UnderlyingLegData> underlyingHandler, Action<OptionLegData> optionHandler)
139  {
140  underlyingHandler(this);
141  }
142  }
143  }
144 }