17 using System.Collections;
18 using System.Collections.Generic;
19 using System.Collections.Immutable;
21 using System.Linq.Expressions;
36 public string Name {
get; }
49 public IReadOnlyList<OptionStrategyLegDefinition>
Legs {
get; }
76 var underlying = legs[0].Position.Symbol;
77 if (underlying.HasUnderlying)
83 for (
int i = 0; i < Math.Min(
Legs.Count, legs.Count); i++)
85 var leg =
Legs[i].CreateLegData(legs[i]);
86 leg.Invoke(strategy.UnderlyingLegs.Add, strategy.OptionLegs.Add);
99 match =
Match(options, positions).FirstOrDefault();
100 return match !=
null;
118 public IEnumerable<OptionStrategyDefinitionMatch>
Match(
129 var multiplier =
int.MaxValue;
133 if (underlyingLotsSign != 0)
136 if (underlyingLotsSign != underlyingPositionSign ||
148 return Match(options,
149 ImmutableList<OptionStrategyLegDefinitionMatch>.Empty,
150 ImmutableList<OptionPosition>.Empty,
156 private IEnumerable<OptionStrategyDefinitionMatch>
Match(
158 ImmutableList<OptionStrategyLegDefinitionMatch> legMatches,
159 ImmutableList<OptionPosition> legPositions,
164 var nextLegIndex = legPositions.Count;
165 if (nextLegIndex ==
Legs.Count)
167 if (nextLegIndex > 0)
175 var nextLeg =
Legs[nextLegIndex];
177 foreach (var legMatch
in nextLeg.Match(options, legPositions, positions).Take(maxLegMatch))
182 foreach (var definitionMatch
in Match(options,
183 legMatches.Add(legMatch),
184 legPositions.Add(legMatch.Position),
185 positions - legMatch.Position,
186 Math.Min(multiplier, legMatch.Multiplier)
189 yield
return definitionMatch;
230 return predicates.Aggregate(
new Builder(name),
231 (builder, predicate) => predicate(builder)
239 params Expression<Func<IReadOnlyList<OptionPosition>,
OptionPosition,
bool>>[] predicates
249 params Expression<Func<IReadOnlyList<OptionPosition>,
OptionPosition,
bool>>[] predicates
260 private readonly
string _name;
262 private int _underlyingLots;
263 private List<OptionStrategyLegDefinition> _legs;
271 _legs =
new List<OptionStrategyLegDefinition>();
279 if (_underlyingLots != 0)
281 throw new InvalidOperationException(
"Underlying lots has already been set.");
284 _underlyingLots = lots;
292 params Expression<Func<IReadOnlyList<OptionPosition>,
OptionPosition,
bool>>[] predicates
303 params Expression<Func<IReadOnlyList<OptionPosition>,
OptionPosition,
bool>>[] predicates
323 return Legs.GetEnumerator();
328 IEnumerator IEnumerable.GetEnumerator()