Files
quantconnect--lean/Common/Securities/Option/StrategyMatcher/OptionStrategyDefinition.cs
Michael Handschuh b9974e6f54 Add OptionStrategyMatcher (#4924)
* Reformat/cleanup OptionStrategies

This file was breaking pretty much every style convention in LEAN.
There are other things that should be addressed in here that weren't,
such as passing non-argument names as argument names for ArgumentException,
as well as preferring constructors over property initializer syntax, but
such changes aren't being made to keep this commit strictly reformatting
instead of refactoring.

Added braces and reformatted long lines to make code more legible.

* Add abstract base class for OptionStrategy Option/UnderlyingLegData

This allows us to create either or and later use the Invoke method to push it
into the appropriate list on OptionStrategy.

* Replace O(n) option contract search with 2 O(1) TryGetValue calls

A better improvement would be resolving the correct symbol in the strategy, but
this immediate change is instead just focused on removing the O(n) search inside
a loop.

* Add BinaryComparison and supporting methods in ExpressionBuilder

We're going to use these binary comparisons to make it possible to create
ad-hoc queries against a collection of symbols. Using these expressions,
along with type supporting composition of these expression, we'll be able
to define predicates that can declaratively define how to match an option
strategy with an algorithms current holdings.

* Make GetValueOrDefault defaultValue optional

Was receiving ambiguous invocations leading to neading to invoke this
method explicitly (LinqExtensions.GetValueOrDefault) instead of being
able to use it as an extension method. Making the default value optional
seems to have resolved this ambiguity, leading to cleaner code in the
OptionPositionCollection (forthcoming)

* Add OptionPosition and OptionPositionCollection

OptionPositionCollection aims to provide a single coherent interface
for querying an algorithm's option contract positions and the underlying
equity's position in a performant, immutable way. The immutability of
the type is necessary for how the options matcher will operate. We need
to recursively evaluate potential matches, each step down the stack removing
positions from the collection consumed by each leg matched. This will enable
parallelism of the solution as well as simplifying the mental model for
understanding due to not needing to track mutations to the collection
instance.

* Add Option test class for easily creating option symbol objects

* Add OptionStrategyLegPredicate and OptionStrategyLegDefinition

The definition is a composition of predicates, and each predicate supports
matching against a set of pre-existing legs and a current position being
checked for the next leg (this leg). In addition to the matching functionality,
it also supports filtering the OptionPositionCollection, which is where much
of the work for resolving potential option strategies is done. By successively
filtering the OptionPositionCollection through successive application of predicates,
we wil end up with a small set of remaining positions that can be individually
evaluated for best margin impacts.

All of this effectively unrolls into a giant evaluation tree. Because of this
inherent structure, common in combinatorial optimization, the OptionPositionCollection
is an immutable type to support concurrent evaluations of different branches of
the tree. For large position collections this will dramatically improve strategy
resolution times. Finally, the interface between the predicate and the positions
collection is purposefully thin and provides a target for future optimizations.

* Add OptionStrategyDefinition and OptionStrategyDefinitions pre-defined definitions

The OptionStrategyDefinition is a definitional object provided a template and functions
used to match algorithm holdings (via OptionPositionCollection) to this definition. The
definition defines a particular way in which option positions can be combined in order to
achieve a more favorable margin requirement, thereby allowing the algorithm to hold more
positions than otherwise possible. This ties into the existing OptionStrategy classes and
the end result of the matching process will be OptionStrategy instances definiing all
strategies matched according to the provided definitions.

* Add OptionStrategyMatcher and Options class, w/ supporting types

OptionStrategyMatcherOptions aims to provide some knobs and dials to control how
the matcher behaves, and more importantly, which positions get prioritized when
matching. Prioritization is controlled via two different enumerators, one controller
which definitions are matched first and the other controller which positions are
matched first. Still unimplemented, is computing multiple solutions and running the
provided objective function to determine the best match. When this gets implemented,
we'll also want to implement the timer. For anyone looking to implement these features,
please talk with Michael Handschuh as there's a particular way of representing these
types of combinatorial solutions (a 3D tree) that can be used as a variation of the
linear simplex method for optimizing combinatorial problems.

* OptionStrategyMatcher: Address PR review comments

* Ensure created OptionStrategy legs all have the same multiplier

Each leg definition match gets it's own multiplier which indicates the
maximum number of times we matched that particular leg. When we finish
matching all legs, we pick the smallest multiplier from all the legs in
the definition and use that as the definition's multiplier. When we go
to create the OptionStrategy object we MUST make sure we're using the
multiplier from the definition and not from the individual legs.

This change fixes this issue and also provides a guard clause to ensure
that we're not trying to use a multiplier larger than what was matched.

* Add XML docs for OptionStrategyDefinitions from OptionStrategies
2020-12-02 18:42:24 -03:00

390 lines
16 KiB
C#

/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Linq.Expressions;
namespace QuantConnect.Securities.Option.StrategyMatcher
{
/// <summary>
/// Provides a definitional object for an <see cref="OptionStrategy"/>. This definition is used to 'match' option
/// positions via <see cref="OptionPositionCollection"/>. The <see cref="OptionStrategyMatcher"/> utilizes a full
/// collection of these definitional objects in order to match an algorithm's option position holdings to the
/// set of strategies in an effort to reduce the total margin required for holding the positions.
/// </summary>
public class OptionStrategyDefinition : IEnumerable<OptionStrategyLegDefinition>
{
/// <summary>
/// Gets the definition's name
/// </summary>
public string Name { get; }
/// <summary>
/// Gets the number of underlying lots required to match this definition. A lot size
/// is equal to the contract's multiplier and is usually equal to 100.
/// </summary>
public int UnderlyingLots { get; }
/// <summary>
/// Gets the option leg definitions. This list does NOT contain a definition for the
/// required underlying lots, due to its simplicity. Instead the required underlying
/// lots are defined via the <see cref="UnderlyingLots"/> property of the definition.
/// </summary>
public IReadOnlyList<OptionStrategyLegDefinition> Legs { get; }
/// <summary>
/// Gets the total number of legs, INCLUDING the underlying leg if applicable. This
/// is used to perform a coarse filter as the minimum number of unique positions in
/// the positions collection.
/// </summary>
public int LegCount => Legs.Count + (UnderlyingLots == 0 ? 0 : 1);
/// <summary>
/// Initializes a new instance of the <see cref="OptionStrategyDefinition"/> class
/// </summary>
/// <param name="name">The definition's name</param>
/// <param name="underlyingLots">The required number of underlying lots</param>
/// <param name="legs">Definitions for each option leg</param>
public OptionStrategyDefinition(string name, int underlyingLots, IEnumerable<OptionStrategyLegDefinition> legs)
{
Name = name;
Legs = legs.ToList();
UnderlyingLots = underlyingLots;
}
/// <summary>
/// Creates the <see cref="OptionStrategy"/> instance using this definition and the provided leg matches
/// </summary>
public OptionStrategy CreateStrategy(IReadOnlyList<OptionStrategyLegDefinitionMatch> legs)
{
var underlying = legs[0].Position.Symbol;
if (underlying.HasUnderlying)
{
underlying = underlying.Underlying;
}
var strategy = new OptionStrategy {Name = Name, Underlying = underlying};
for (int i = 0; i < Math.Min(Legs.Count, legs.Count); i++)
{
var leg = Legs[i].CreateLegData(legs[i]);
leg.Invoke(strategy.UnderlyingLegs.Add, strategy.OptionLegs.Add);
}
return strategy;
}
/// <summary>
/// Attempts to match the positions to this definition exactly once, by evaluating the enumerable and
/// taking the first entry matched. If not match is found, then false is returned and <paramref name="match"/>
/// will be null.
/// </summary>
public bool TryMatchOnce(OptionStrategyMatcherOptions options, OptionPositionCollection positions, out OptionStrategyDefinitionMatch match)
{
match = Match(options, positions).FirstOrDefault();
return match != null;
}
/// <summary>
/// Determines all possible matches for this definition using the provided <paramref name="positions"/>.
/// This includes OVERLAPPING matches. It's up to the actual matcher to make decisions based on which
/// matches to accept. This allows the matcher to prioritize matching certain positions over others.
/// </summary>
public IEnumerable<OptionStrategyDefinitionMatch> Match(OptionPositionCollection positions)
{
return Match(OptionStrategyMatcherOptions.ForDefinitions(this), positions);
}
/// <summary>
/// Determines all possible matches for this definition using the provided <paramref name="positions"/>.
/// This includes OVERLAPPING matches. It's up to the actual matcher to make decisions based on which
/// matches to accept. This allows the matcher to prioritize matching certain positions over others.
/// </summary>
public IEnumerable<OptionStrategyDefinitionMatch> Match(
OptionStrategyMatcherOptions options,
OptionPositionCollection positions
)
{
// TODO : Pass OptionStrategyMatcherOptions in and respect applicable options
if (positions.Count < LegCount)
{
return Enumerable.Empty<OptionStrategyDefinitionMatch>();
}
var multiplier = int.MaxValue;
// first check underlying lots has correct sign and sufficient magnitude
var underlyingLotsSign = Math.Sign(UnderlyingLots);
if (underlyingLotsSign != 0)
{
var underlyingPositionSign = Math.Sign(positions.UnderlyingQuantity);
if (underlyingLotsSign != underlyingPositionSign ||
Math.Abs(positions.UnderlyingQuantity) < Math.Abs(UnderlyingLots))
{
return Enumerable.Empty<OptionStrategyDefinitionMatch>();
}
// set multiplier for underlying
multiplier = positions.UnderlyingQuantity / UnderlyingLots;
}
// TODO : Consider add OptionStrategyLegDefinition for underlying for consistency purposes.
// Might want to enforce that it's always the first leg definition as well for easier slicing.
return Match(options,
ImmutableList<OptionStrategyLegDefinitionMatch>.Empty,
ImmutableList<OptionPosition>.Empty,
positions,
multiplier
).Distinct();
}
private IEnumerable<OptionStrategyDefinitionMatch> Match(
OptionStrategyMatcherOptions options,
ImmutableList<OptionStrategyLegDefinitionMatch> legMatches,
ImmutableList<OptionPosition> legPositions,
OptionPositionCollection positions,
int multiplier
)
{
var nextLegIndex = legPositions.Count;
if (nextLegIndex == Legs.Count)
{
if (nextLegIndex > 0)
{
yield return new OptionStrategyDefinitionMatch(this, legMatches, multiplier);
}
}
else if (positions.Count >= LegCount - nextLegIndex)
{
// grab the next leg definition and perform the match, restricting total to configured maximum per leg
var nextLeg = Legs[nextLegIndex];
var maxLegMatch = options.GetMaximumLegMatches(nextLegIndex);
foreach (var legMatch in nextLeg.Match(options, legPositions, positions).Take(maxLegMatch))
{
// add match to the match we're constructing and deduct matched position from positions collection
// we track the min multiplier in line so when we're done, we have the total number of matches for
// the matched set of positions in this 'thread' (OptionStrategy.Quantity)
foreach (var definitionMatch in Match(options,
legMatches.Add(legMatch),
legPositions.Add(legMatch.Position),
positions - legMatch.Position,
Math.Min(multiplier, legMatch.Multiplier)
))
{
yield return definitionMatch;
}
}
}
else
{
// positions.Count < LegsCount indicates a failed match
// could include partial matches, would allow an algorithm to determine if adding a
// new position could help reduce overall margin exposure by completing a strategy
}
}
/// <summary>
/// Attempts to exactly match the specified positions to this strategy definition with as much quantity as possible.
/// </summary>
public bool TryMatch(IReadOnlyList<OptionPosition> positions, out OptionStrategy strategy)
{
if (positions.Count == 0 || Legs.Count != positions.Count)
{
strategy = null;
return false;
}
var underlying = positions[0].Symbol;
if (underlying.SecurityType == SecurityType.Option)
{
underlying = underlying.Underlying;
}
var quantityMultiplier = int.MaxValue;
var matches = new List<OptionStrategy.LegData>();
for (int i = 0; i < Legs.Count; i++)
{
var leg = Legs[i];
var position = positions[i];
OptionStrategy.LegData match;
if (!leg.TryMatch(position, out match))
{
strategy = null;
return false;
}
matches.Add(match);
var multiple = match.Quantity / leg.Quantity;
quantityMultiplier = Math.Min(multiple, quantityMultiplier);
}
// separate matches into option/underlying legs and resize according to smallest quantity multipler
var optionLegs = new List<OptionStrategy.OptionLegData>();
var underlyingLegs = new List<OptionStrategy.UnderlyingLegData>();
for (var i = 0; i < matches.Count; i++)
{
var match = matches[i];
match.Invoke(underlyingLegs.Add, optionLegs.Add);
match.Quantity = Legs[i].Quantity * quantityMultiplier;
}
strategy = new OptionStrategy
{
Name = Name,
OptionLegs = optionLegs,
Underlying = underlying,
UnderlyingLegs = underlyingLegs
};
return true;
}
/// <summary>Returns a string that represents the current object.</summary>
/// <returns>A string that represents the current object.</returns>
public override string ToString()
{
return Name;
}
/// <summary>
/// Factory function for creating definitions
/// </summary>
public static OptionStrategyDefinition Create(string name, int underlyingLots, params OptionStrategyLegDefinition[] legs)
{
return new OptionStrategyDefinition(name, underlyingLots, legs);
}
/// <summary>
/// Factory function for creating definitions
/// </summary>
public static OptionStrategyDefinition Create(string name, params OptionStrategyLegDefinition[] legs)
{
return new OptionStrategyDefinition(name, 0, legs);
}
/// <summary>
/// Factory function for creating definitions
/// </summary>
public static OptionStrategyDefinition Create(string name, params Func<Builder, Builder>[] predicates)
{
return predicates.Aggregate(new Builder(name),
(builder, predicate) => predicate(builder)
).Build();
}
/// <summary>
/// Factory function for creating a call leg definition
/// </summary>
public static OptionStrategyLegDefinition CallLeg(int quantity,
params Expression<Func<IReadOnlyList<OptionPosition>, OptionPosition, bool>>[] predicates
)
{
return OptionStrategyLegDefinition.Create(OptionRight.Call, quantity, predicates);
}
/// <summary>
/// Factory function for creating a put leg definition
/// </summary>
public static OptionStrategyLegDefinition PutLeg(int quantity,
params Expression<Func<IReadOnlyList<OptionPosition>, OptionPosition, bool>>[] predicates
)
{
return OptionStrategyLegDefinition.Create(OptionRight.Put, quantity, predicates);
}
/// <summary>
/// Builder class supporting fluent syntax in constructing <see cref="OptionStrategyDefinition"/>.
/// </summary>
public class Builder
{
private readonly string _name;
private int _underlyingLots;
private List<OptionStrategyLegDefinition> _legs;
/// <summary>
/// Initializes a new instance of the <see cref="Builder"/> class
/// </summary>
public Builder(string name)
{
_name = name;
_legs = new List<OptionStrategyLegDefinition>();
}
/// <summary>
/// Sets the required number of underlying lots
/// </summary>
public Builder WithUnderlyingLots(int lots)
{
if (_underlyingLots != 0)
{
throw new InvalidOperationException("Underlying lots has already been set.");
}
_underlyingLots = lots;
return this;
}
/// <summary>
/// Adds a call leg
/// </summary>
public Builder WithCall(int quantity,
params Expression<Func<IReadOnlyList<OptionPosition>, OptionPosition, bool>>[] predicates
)
{
_legs.Add(OptionStrategyLegDefinition.Create(OptionRight.Call, quantity, predicates));
return this;
}
/// <summary>
/// Adds a put leg
/// </summary>
public Builder WithPut(int quantity,
params Expression<Func<IReadOnlyList<OptionPosition>, OptionPosition, bool>>[] predicates
)
{
_legs.Add(OptionStrategyLegDefinition.Create(OptionRight.Put, quantity, predicates));
return this;
}
/// <summary>
/// Builds the <see cref="OptionStrategyDefinition"/>
/// </summary>
public OptionStrategyDefinition Build()
{
return new OptionStrategyDefinition(_name, _underlyingLots, _legs);
}
}
/// <summary>Returns an enumerator that iterates through the collection.</summary>
/// <returns>An enumerator that can be used to iterate through the collection.</returns>
public IEnumerator<OptionStrategyLegDefinition> GetEnumerator()
{
return Legs.GetEnumerator();
}
/// <summary>Returns an enumerator that iterates through a collection.</summary>
/// <returns>An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the collection.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}