Skip to content

Commit

Permalink
Add OptionStrategyMatcher (#4924)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mchandschuh authored Dec 2, 2020
1 parent 474c5cd commit b9974e6
Show file tree
Hide file tree
Showing 47 changed files with 5,509 additions and 116 deletions.
49 changes: 28 additions & 21 deletions Algorithm/QCAlgorithm.Trading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -559,39 +559,43 @@ private IEnumerable<OrderTicket> GenerateOrders(OptionStrategy strategy, int str
var orders = new List<OrderTicket>();

// setting up the tag text for all orders of one strategy
var strategyTag = $"{strategy.Name} ({strategyQuantity.ToStringInvariant()})";
var tag = $"{strategy.Name} ({strategyQuantity.ToStringInvariant()})";

// walking through all option legs and issuing orders
if (strategy.OptionLegs != null)
{
var underlying = strategy.Underlying;
foreach (var optionLeg in strategy.OptionLegs)
{
var optionSeq = Securities.Where(kv => kv.Key.Underlying == strategy.Underlying &&
kv.Key.ID.OptionRight == optionLeg.Right &&
kv.Key.ID.Date == optionLeg.Expiration &&
kv.Key.ID.StrikePrice == optionLeg.Strike);
// search for both american/european style -- much better than looping through all securities
var american = QuantConnect.Symbol.CreateOption(underlying, underlying.ID.Market,
OptionStyle.American, optionLeg.Right, optionLeg.Strike, optionLeg.Expiration);

if (optionSeq.Count() != 1)
var european = QuantConnect.Symbol.CreateOption(underlying, underlying.ID.Market,
OptionStyle.European, optionLeg.Right, optionLeg.Strike, optionLeg.Expiration);

Security contract;
if (!Securities.TryGetValue(american, out contract) && !Securities.TryGetValue(european, out contract))
{
throw new InvalidOperationException("Couldn't find the option contract in algorithm securities list. " +
Invariant($"Underlying: {strategy.Underlying}, option {optionLeg.Right}, strike {optionLeg.Strike}, ") +
Invariant($"expiration: {optionLeg.Expiration}"));
Invariant($"expiration: {optionLeg.Expiration}")
);
}

var option = optionSeq.First().Key;

var orderQuantity = optionLeg.Quantity * strategyQuantity;
switch (optionLeg.OrderType)
{
case OrderType.Market:
var marketOrder = MarketOrder(option, optionLeg.Quantity * strategyQuantity, tag: strategyTag);
orders.Add(marketOrder);
orders.Add(MarketOrder(contract.Symbol, orderQuantity, tag: tag));
break;

case OrderType.Limit:
var limitOrder = LimitOrder(option, optionLeg.Quantity * strategyQuantity, optionLeg.OrderPrice, tag: strategyTag);
orders.Add(limitOrder);
orders.Add(LimitOrder(contract.Symbol, orderQuantity, optionLeg.OrderPrice, tag));
break;

default:
throw new InvalidOperationException("Order type is not supported in option strategy: " + optionLeg.OrderType.ToString());
throw new InvalidOperationException(Invariant($"Order type is not supported in option strategy: {optionLeg.OrderType}"));
}
}
}
Expand All @@ -603,25 +607,28 @@ private IEnumerable<OrderTicket> GenerateOrders(OptionStrategy strategy, int str
{
if (!Securities.ContainsKey(strategy.Underlying))
{
var error = $"Couldn't find the option contract underlying in algorithm securities list. Underlying: {strategy.Underlying}";
throw new InvalidOperationException(error);
throw new InvalidOperationException(
$"Couldn't find the option contract underlying in algorithm securities list. Underlying: {strategy.Underlying}"
);
}

var orderQuantity = underlyingLeg.Quantity * strategyQuantity;
switch (underlyingLeg.OrderType)
{
case OrderType.Market:
var marketOrder = MarketOrder(strategy.Underlying, underlyingLeg.Quantity * strategyQuantity, tag: strategyTag);
orders.Add(marketOrder);
orders.Add(MarketOrder(strategy.Underlying, orderQuantity, tag: tag));
break;

case OrderType.Limit:
var limitOrder = LimitOrder(strategy.Underlying, underlyingLeg.Quantity * strategyQuantity, underlyingLeg.OrderPrice, tag: strategyTag);
orders.Add(limitOrder);
orders.Add(LimitOrder(strategy.Underlying, orderQuantity, underlyingLeg.OrderPrice, tag));
break;

default:
throw new InvalidOperationException("Order type is not supported in option strategy: " + underlyingLeg.OrderType.ToString());
throw new InvalidOperationException(Invariant($"Order type is not supported in option strategy: {underlyingLeg.OrderType}"));
}
}
}

return orders;
}

Expand Down
199 changes: 199 additions & 0 deletions Common/BinaryComparison.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
* 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.Linq.Expressions;
using static QuantConnect.Util.ExpressionBuilder;

namespace QuantConnect
{
/// <summary>
/// Enumeration class defining binary comparisons and providing access to expressions and functions
/// capable of evaluating a particular comparison for any type. If a particular type does not implement
/// a binary comparison than an exception will be thrown.
/// </summary>
public class BinaryComparison
{
/// <summary>
/// Gets the <see cref="BinaryComparison"/> equivalent of <see cref="ExpressionType.Equal"/>
/// </summary>
public static readonly BinaryComparison Equal = new BinaryComparison(ExpressionType.Equal);

/// <summary>
/// Gets the <see cref="BinaryComparison"/> equivalent of <see cref="ExpressionType.NotEqual"/>
/// </summary>
public static readonly BinaryComparison NotEqual = new BinaryComparison(ExpressionType.NotEqual);

/// <summary>
/// Gets the <see cref="BinaryComparison"/> equivalent of <see cref="ExpressionType.LessThan"/>
/// </summary>
public static readonly BinaryComparison LessThan = new BinaryComparison(ExpressionType.LessThan);

/// <summary>
/// Gets the <see cref="BinaryComparison"/> equivalent of <see cref="ExpressionType.GreaterThan"/>
/// </summary>
public static readonly BinaryComparison GreaterThan = new BinaryComparison(ExpressionType.GreaterThan);

/// <summary>
/// Gets the <see cref="BinaryComparison"/> equivalent of <see cref="ExpressionType.LessThanOrEqual"/>
/// </summary>
public static readonly BinaryComparison LessThanOrEqual = new BinaryComparison(ExpressionType.LessThanOrEqual);

/// <summary>
/// Gets the <see cref="BinaryComparison"/> equivalent of <see cref="ExpressionType.GreaterThanOrEqual"/>
/// </summary>
public static readonly BinaryComparison GreaterThanOrEqual = new BinaryComparison(ExpressionType.GreaterThanOrEqual);

/// <summary>
/// Gets the <see cref="BinaryComparison"/> matching the provided <paramref name="type"/>
/// </summary>
public static BinaryComparison FromExpressionType(ExpressionType type)
{
switch (type)
{
case ExpressionType.Equal: return Equal;
case ExpressionType.NotEqual: return NotEqual;
case ExpressionType.LessThan: return LessThan;
case ExpressionType.LessThanOrEqual: return LessThanOrEqual;
case ExpressionType.GreaterThan: return GreaterThan;
case ExpressionType.GreaterThanOrEqual: return GreaterThanOrEqual;
default:
throw new InvalidOperationException($"The specified ExpressionType '{type}' is not a binary comparison.");
}
}

/// <summary>
/// Gets the expression type defining the binary comparison.
/// </summary>
public ExpressionType Type { get; }

private BinaryComparison(ExpressionType type)
{
Type = type;
}

/// <summary>
/// Evaluates the specified <paramref name="left"/> and <paramref name="right"/> according to this <see cref="BinaryComparison"/>
/// </summary>
public bool Evaluate<T>(T left, T right)
=> OfType<T>.GetFunc(Type)(left, right);

/// <summary>
/// Gets a function capable of performing this <see cref="BinaryComparison"/>
/// </summary>
public Func<T, T, bool> GetEvaluator<T>()
=> OfType<T>.GetFunc(Type);

/// <summary>
/// Gets an expression representing this <see cref="BinaryComparison"/>
/// </summary>
public Expression<Func<T, T, bool>> GetExpression<T>()
=> OfType<T>.GetExpression(Type);

/// <summary>
/// Flips the logic ordering of the comparison's operands. For example, <see cref="LessThan"/>
/// is converted into <see cref="GreaterThan"/>
/// </summary>
public BinaryComparison FlipOperands()
{
switch (Type)
{
case ExpressionType.Equal: return this;
case ExpressionType.NotEqual: return this;
case ExpressionType.LessThan: return GreaterThan;
case ExpressionType.LessThanOrEqual: return GreaterThanOrEqual;
case ExpressionType.GreaterThan: return LessThan;
case ExpressionType.GreaterThanOrEqual: return LessThanOrEqual;
default:
throw new Exception(
"The skies are falling and the oceans are rising! " +
"If you've made it here then this exception is the least of your worries! " +
$"ExpressionType: {Type}"
);
}
}

/// <summary>Returns a string that represents the current object.</summary>
/// <returns>A string that represents the current object.</returns>
public override string ToString()
{
return Type.ToString();
}

/// <summary>
/// Provides thread-safe lookups of expressions and functions for binary comparisons by type.
/// MUCH faster than using a concurrency dictionary, for example, as it's expanded at runtime
/// and hard-linked, no look-up is actually performed!
/// </summary>
private static class OfType<T>
{
private static readonly Expression<Func<T, T, bool>> EqualExpr = MakeBinaryComparisonLambdaOrNull(ExpressionType.Equal);
private static readonly Expression<Func<T, T, bool>> NotEqualExpr = MakeBinaryComparisonLambdaOrNull(ExpressionType.NotEqual);
private static readonly Expression<Func<T, T, bool>> LessThanExpr = MakeBinaryComparisonLambdaOrNull(ExpressionType.LessThan);
private static readonly Expression<Func<T, T, bool>> LessThanOrEqualExpr = MakeBinaryComparisonLambdaOrNull(ExpressionType.LessThanOrEqual);
private static readonly Expression<Func<T, T, bool>> GreaterThanExpr = MakeBinaryComparisonLambdaOrNull(ExpressionType.GreaterThan);
private static readonly Expression<Func<T, T, bool>> GreaterThanOrEqualExpr = MakeBinaryComparisonLambdaOrNull(ExpressionType.GreaterThanOrEqual);

public static Expression<Func<T, T, bool>> GetExpression(ExpressionType type)
{
switch (type)
{
case ExpressionType.Equal: return EqualExpr;
case ExpressionType.NotEqual: return NotEqualExpr;
case ExpressionType.LessThan: return LessThanExpr;
case ExpressionType.LessThanOrEqual: return LessThanOrEqualExpr;
case ExpressionType.GreaterThan: return GreaterThanExpr;
case ExpressionType.GreaterThanOrEqual: return GreaterThanOrEqualExpr;
default:
throw new InvalidOperationException($"The specified ExpressionType '{type}' is not a binary comparison.");
}
}

private static readonly Func<T, T, bool> EqualFunc = EqualExpr?.Compile();
private static readonly Func<T, T, bool> NotEqualFunc = NotEqualExpr?.Compile();
private static readonly Func<T, T, bool> LessThanFunc = LessThanExpr?.Compile();
private static readonly Func<T, T, bool> LessThanOrEqualFunc = LessThanOrEqualExpr?.Compile();
private static readonly Func<T, T, bool> GreaterThanFunc = GreaterThanExpr?.Compile();
private static readonly Func<T, T, bool> GreaterThanOrEqualFunc = GreaterThanOrEqualExpr?.Compile();

public static Func<T, T, bool> GetFunc(ExpressionType type)
{
switch (type)
{
case ExpressionType.Equal: return EqualFunc;
case ExpressionType.NotEqual: return NotEqualFunc;
case ExpressionType.LessThan: return LessThanFunc;
case ExpressionType.LessThanOrEqual: return LessThanOrEqualFunc;
case ExpressionType.GreaterThan: return GreaterThanFunc;
case ExpressionType.GreaterThanOrEqual: return GreaterThanOrEqualFunc;
default:
throw new InvalidOperationException($"The specified ExpressionType '{type}' is not a binary comparison.");
}
}

private static Expression<Func<T, T, bool>> MakeBinaryComparisonLambdaOrNull(ExpressionType type)
{
try
{
return MakeBinaryComparisonLambda<T>(type);
}
catch
{
return null;
}
}
}
}
}
Loading

0 comments on commit b9974e6

Please sign in to comment.