b9974e6f54
* 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
245 lines
12 KiB
C#
245 lines
12 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.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using QuantConnect.Util;
|
|
|
|
namespace QuantConnect.Securities.Option.StrategyMatcher
|
|
{
|
|
/// <summary>
|
|
/// Defines a condition under which a particular <see cref="OptionPosition"/> can be combined with
|
|
/// a preceding list of leg (also of type <see cref="OptionPosition"/>) to achieve a particular
|
|
/// option strategy.
|
|
/// </summary>
|
|
public class OptionStrategyLegPredicate
|
|
{
|
|
private readonly BinaryComparison _comparison;
|
|
private readonly IOptionStrategyLegPredicateReferenceValue _reference;
|
|
private readonly Func<IReadOnlyList<OptionPosition>, OptionPosition, bool> _predicate;
|
|
private readonly Expression<Func<IReadOnlyList<OptionPosition>, OptionPosition, bool>> _expression;
|
|
|
|
/// <summary>
|
|
/// Determines whether or not this predicate is able to utilize <see cref="OptionPositionCollection"/> indexes.
|
|
/// </summary>
|
|
public bool IsIndexed => _comparison != null && _reference != null;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="OptionStrategyLegPredicate"/> class
|
|
/// </summary>
|
|
/// <param name="comparison">The <see cref="BinaryComparison"/> invoked</param>
|
|
/// <param name="reference">The reference value, such as a strike price, encapsulated within the
|
|
/// <see cref="IOptionStrategyLegPredicateReferenceValue"/> to enable resolving the value from different potential sets.</param>
|
|
/// <param name="predicate">The compiled predicate expression</param>
|
|
/// <param name="expression">The predicate expression, from which, all other values were derived.</param>
|
|
public OptionStrategyLegPredicate(
|
|
BinaryComparison comparison,
|
|
IOptionStrategyLegPredicateReferenceValue reference,
|
|
Func<IReadOnlyList<OptionPosition>, OptionPosition, bool> predicate,
|
|
Expression<Func<IReadOnlyList<OptionPosition>, OptionPosition, bool>> expression
|
|
)
|
|
{
|
|
_reference = reference;
|
|
_predicate = predicate;
|
|
_comparison = comparison;
|
|
_expression = expression;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines whether or not the provided combination of preceding <paramref name="legs"/>
|
|
/// and current <paramref name="position"/> adhere to this predicate's requirements.
|
|
/// </summary>
|
|
public bool Matches(IReadOnlyList<OptionPosition> legs, OptionPosition position)
|
|
{
|
|
try
|
|
{
|
|
return _predicate(legs, position);
|
|
}
|
|
catch (InvalidOperationException)
|
|
{
|
|
// attempt to access option SecurityIdentifier values, such as strike, on the underlying
|
|
// this simply means we don't match and can safely ignore this exception. now, this does
|
|
// somewhat indicate a potential design flaw, but I content that this is better than having
|
|
// to manage the underlying position separately throughout the entire matching process.
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Filters the specified <paramref name="positions"/> by applying this predicate based on the referenced legs.
|
|
/// </summary>
|
|
public OptionPositionCollection Filter(IReadOnlyList<OptionPosition> legs, OptionPositionCollection positions, bool includeUnderlying)
|
|
{
|
|
if (!IsIndexed)
|
|
{
|
|
// if the predicate references non-indexed properties or contains complex/multiple conditions then
|
|
// we'll need to do a full table scan. this is not always avoidable, but we should try to avoid it
|
|
return OptionPositionCollection.Empty.AddRange(
|
|
positions.Where(position => _predicate(legs, position))
|
|
);
|
|
}
|
|
|
|
var referenceValue = _reference.Resolve(legs);
|
|
switch (_reference.Target)
|
|
{
|
|
case PredicateTargetValue.Right: return positions.Slice((OptionRight) referenceValue, includeUnderlying);
|
|
case PredicateTargetValue.Strike: return positions.Slice(_comparison, (decimal) referenceValue, includeUnderlying);
|
|
case PredicateTargetValue.Expiration: return positions.Slice(_comparison, (DateTime) referenceValue, includeUnderlying);
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the underlying <see cref="IOptionStrategyLegPredicateReferenceValue"/> value used by this predicate.
|
|
/// </summary>
|
|
public IOptionStrategyLegPredicateReferenceValue GetReferenceValue()
|
|
{
|
|
return _reference;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new <see cref="OptionStrategyLegPredicate"/> from the specified predicate <paramref name="expression"/>
|
|
/// </summary>
|
|
public static OptionStrategyLegPredicate Create(
|
|
Expression<Func<IReadOnlyList<OptionPosition>, OptionPosition, bool>> expression
|
|
)
|
|
{
|
|
// expr must NOT include compound comparisons
|
|
// expr is a lambda of one of the following forms:
|
|
// (legs, position) => position.{target} {comparison} legs[i].{reference-target}
|
|
// (legs, position) => legs[i].{reference-target} {comparison} position.{target}
|
|
// (legs, position) => position.{target} {comparison} {literal-reference-target}
|
|
// (legs, position) => {literal-reference-target} {comparison} position.{target}
|
|
|
|
// we want to make the comparison of a common form, specifically:
|
|
// position.{target} {comparison} {reference-target}
|
|
// this is so when we invoke OptionPositionCollection we have the correct comparison type
|
|
// for example, legs[0].Strike > position.Strike
|
|
// needs to be inverted into position.Strike < legs[0].Strike
|
|
// so we can call OptionPositionCollection.Slice(BinaryComparison.LessThan, legs[0].Strike);
|
|
|
|
try
|
|
{
|
|
var legsParameter = expression.Parameters[0];
|
|
var positionParameter = expression.Parameters[1];
|
|
var binary = expression.OfType<BinaryExpression>().Single(e => e.NodeType.IsBinaryComparison());
|
|
var comparison = BinaryComparison.FromExpressionType(binary.NodeType);
|
|
var leftReference = CreateReferenceValue(legsParameter, positionParameter, binary.Left);
|
|
var rightReference = CreateReferenceValue(legsParameter, positionParameter, binary.Right);
|
|
if (leftReference != null && rightReference != null)
|
|
{
|
|
throw new ArgumentException($"The provided expression is not of the required form: {expression}");
|
|
}
|
|
|
|
// we want the left side to be null, indicating position.{target}
|
|
// if not, then we need to flip the comparison operand
|
|
var reference = rightReference;
|
|
if (rightReference == null)
|
|
{
|
|
reference = leftReference;
|
|
comparison = comparison.FlipOperands();
|
|
}
|
|
|
|
return new OptionStrategyLegPredicate(comparison, reference, expression.Compile(), expression);
|
|
}
|
|
catch
|
|
{
|
|
// we can still handle arbitrary predicates, they just require a full search of the positions
|
|
// as we're unable to leverage any of the pre-build indexes via Slice methods.
|
|
return new OptionStrategyLegPredicate(null, null, expression.Compile(), expression);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new <see cref="IOptionStrategyLegPredicateReferenceValue"/> from the specified lambda parameters
|
|
/// and expression to be evaluated.
|
|
/// </summary>
|
|
private static IOptionStrategyLegPredicateReferenceValue CreateReferenceValue(
|
|
Expression legsParameter,
|
|
Expression positionParameter,
|
|
Expression expression
|
|
)
|
|
{
|
|
// if we're referencing the position parameter then this isn't a reference value
|
|
// this 'value' is the positions being matched in OptionPositionCollection
|
|
// verify the legs parameter doesn't appear in here either
|
|
var expressions = expression.AsEnumerable().ToList();
|
|
var containsLegParameter = expressions.Any(e => ReferenceEquals(e, legsParameter));
|
|
var containsPositionParameter = expressions.Any(e => ReferenceEquals(e, positionParameter));
|
|
if (containsPositionParameter)
|
|
{
|
|
if (containsLegParameter)
|
|
{
|
|
throw new NotSupportedException("Expressions containing references to both parameters " +
|
|
"(legs and positions) on the same side of an equality operator are not supported."
|
|
);
|
|
}
|
|
|
|
// this expression is of the form position.Strike/position.Expiration/position.Right
|
|
// and as such, is not a reference value, simply return null
|
|
return null;
|
|
}
|
|
|
|
if (!containsLegParameter)
|
|
{
|
|
// this is a literal and we'll attempt to evaluate it.
|
|
var value = Expression.Lambda(expression).Compile().DynamicInvoke();
|
|
if (value == null)
|
|
{
|
|
throw new ArgumentNullException($"Failed to evaluate expression literal: {expressions}");
|
|
}
|
|
|
|
return ConstantOptionStrategyLegReferenceValue.Create(value);
|
|
}
|
|
|
|
// we're looking for an array indexer into the legs list
|
|
var methodCall = expressions.Single<MethodCallExpression>();
|
|
Debug.Assert(methodCall.Method.Name == "get_Item");
|
|
// compile and dynamically invoke the argument to get_Item(x) {legs[x]}
|
|
var arrayIndex = (int) Expression.Lambda(methodCall.Arguments[0]).Compile().DynamicInvoke();
|
|
|
|
// and then a member expression denoting the property (target)
|
|
var member = expressions.Single<MemberExpression>().Member;
|
|
var target = GetPredicateTargetValue(member.Name);
|
|
|
|
return new OptionStrategyLegPredicateReferenceValue(arrayIndex, target);
|
|
}
|
|
|
|
private static PredicateTargetValue GetPredicateTargetValue(string memberName)
|
|
{
|
|
switch (memberName)
|
|
{
|
|
case nameof(OptionPosition.Right): return PredicateTargetValue.Right;
|
|
case nameof(OptionPosition.Strike): return PredicateTargetValue.Strike;
|
|
case nameof(OptionPosition.Expiration): return PredicateTargetValue.Expiration;
|
|
default:
|
|
throw new NotImplementedException(
|
|
$"Failed to resolve member name '{memberName}' to {nameof(PredicateTargetValue)}"
|
|
);
|
|
}
|
|
}
|
|
|
|
/// <summary>Returns a string that represents the current object.</summary>
|
|
/// <returns>A string that represents the current object.</returns>
|
|
public override string ToString()
|
|
{
|
|
return _expression.ToString();
|
|
}
|
|
}
|
|
} |