Lean  $LEAN_TAG$
OptionStrategyLegPredicateReferenceValue.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 System.Collections.Generic;
18 
20 {
21  /// <summary>
22  /// Provides an implementation of <see cref="IOptionStrategyLegPredicateReferenceValue"/> that references an option
23  /// leg from the list of already matched legs by index. The property referenced is defined by <see cref="PredicateTargetValue"/>
24  /// </summary>
26  {
27  private readonly int _index;
28 
29  /// <summary>
30  /// Gets the target of this value
31  /// </summary>
32  public PredicateTargetValue Target { get; }
33 
34  /// <summary>
35  /// Initializes a new instance of the <see cref="IOptionStrategyLegPredicateReferenceValue"/> class
36  /// </summary>
37  /// <param name="index">The legs list index</param>
38  /// <param name="target">The property value being referenced</param>
40  {
41  _index = index;
42  Target = target;
43  }
44 
45  /// <summary>
46  /// Resolves the value of the comparand specified in an <see cref="OptionStrategyLegPredicate"/>.
47  /// For example, the predicate may include ... > legs[0].Strike, and upon evaluation, we need to
48  /// be able to extract leg[0].Strike for the currently contemplated set of legs adhering to a
49  /// strategy's definition.
50  /// </summary>
51  public object Resolve(IReadOnlyList<OptionPosition> legs)
52  {
53  if (_index >= legs.Count)
54  {
55  throw new InvalidOperationException(
56  $"OptionStrategyLegPredicateReferenceValue[{_index}] is unable to be resolved. Only {legs.Count} legs were provided."
57  );
58  }
59 
60  var leg = legs[_index];
61  switch (Target)
62  {
63  case PredicateTargetValue.Right: return leg.Right;
64  case PredicateTargetValue.Strike: return leg.Strike;
65  case PredicateTargetValue.Expiration: return leg.Expiration;
66  default:
67  throw new ArgumentOutOfRangeException();
68  }
69  }
70  }
71 }