Lean
$LEAN_TAG$
OptionStrategyMatcher.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.Collections.Generic;
17
18
namespace
QuantConnect.Securities.Option.StrategyMatcher
19
{
20
/// <summary>
21
/// Matches <see cref="OptionPositionCollection"/> against a collection of <see cref="OptionStrategyDefinition"/>
22
/// according to the <see cref="OptionStrategyMatcherOptions"/> provided.
23
/// </summary>
24
public
class
OptionStrategyMatcher
25
{
26
/// <summary>
27
/// Specifies options controlling how the matcher operates
28
/// </summary>
29
public
OptionStrategyMatcherOptions
Options
{
get
; }
30
31
/// <summary>
32
/// Initializes a new instance of the <see cref="OptionStrategyMatcher"/> class
33
/// </summary>
34
/// <param name="options">Specifies definitions and other options controlling the matcher</param>
35
public
OptionStrategyMatcher
(
OptionStrategyMatcherOptions
options)
36
{
37
Options
= options;
38
}
39
40
// TODO : Implement matching multiple permutations and using the objective function to select the best solution
41
42
/// <summary>
43
/// Using the definitions provided in <see cref="Options"/>, attempts to match all <paramref name="positions"/>.
44
/// The resulting <see cref="OptionStrategyMatch"/> presents a single, valid solution for matching as many positions
45
/// as possible.
46
/// </summary>
47
public
OptionStrategyMatch
MatchOnce
(
OptionPositionCollection
positions)
48
{
49
// these definitions are enumerated according to the configured IOptionStrategyDefinitionEnumerator
50
51
var strategies =
new
List<OptionStrategy>();
52
foreach
(var definition
in
Options
.
Definitions
)
53
{
54
// simplest implementation here is to match one at a time, updating positions in between
55
// a better implementation would be to evaluate all possible matches and make decisions
56
// prioritizing positions that would require more margin if not matched
57
58
OptionStrategyDefinitionMatch
match;
59
while
(definition.TryMatchOnce(
Options
, positions, out match))
60
{
61
positions = match.
RemoveFrom
(positions);
62
strategies.
Add
(match.CreateStrategy());
63
}
64
65
if
(positions.IsEmpty)
66
{
67
break
;
68
}
69
}
70
71
return
new
OptionStrategyMatch
(strategies);
72
}
73
}
74
}
Common
Securities
Option
StrategyMatcher
OptionStrategyMatcher.cs
Generated by
1.8.17