/*
* 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 Python.Runtime;
using QuantConnect.Data;
using QuantConnect.Data.Market;
using QuantConnect.Data.UniverseSelection;
using QuantConnect.Interfaces;
using QuantConnect.Orders;
using QuantConnect.Orders.Fees;
using QuantConnect.Orders.Fills;
using QuantConnect.Orders.OptionExercise;
using QuantConnect.Orders.Slippage;
using QuantConnect.Python;
using QuantConnect.Securities.Interfaces;
using QuantConnect.Util;
using System;
using System.Collections.Generic;
namespace QuantConnect.Securities.Option
{
///
/// Option Security Object Implementation for Option Assets
///
///
public class Option : Security, IDerivativeSecurity, IOptionPrice
{
///
/// The default number of days required to settle an equity sale
///
public static int DefaultSettlementDays { get; set; } = 1;
///
/// The default time of day for settlement
///
public static readonly TimeSpan DefaultSettlementTime = new (6, 0, 0);
///
/// Constructor for the option security
///
/// Defines the hours this exchange is open
/// The cash object that represent the quote currency
/// The subscription configuration for this security
/// The symbol properties for this security
/// Currency converter used to convert
/// instances into units of the account currency
/// Provides all data types registered in the algorithm
/// Used in testing
public Option(SecurityExchangeHours exchangeHours,
SubscriptionDataConfig config,
Cash quoteCurrency,
OptionSymbolProperties symbolProperties,
ICurrencyConverter currencyConverter,
IRegisteredSecurityDataTypesProvider registeredTypes)
: this(config.Symbol,
quoteCurrency,
symbolProperties,
new OptionExchange(exchangeHours),
new OptionCache(),
new OptionPortfolioModel(),
new ImmediateFillModel(),
new InteractiveBrokersFeeModel(),
NullSlippageModel.Instance,
new ImmediateSettlementModel(),
Securities.VolatilityModel.Null,
new OptionMarginModel(),
new OptionDataFilter(),
new SecurityPriceVariationModel(),
currencyConverter,
registeredTypes,
null)
{
AddData(config);
SetDataNormalizationMode(DataNormalizationMode.Raw);
}
///
/// Constructor for the option security
///
/// The symbol of the security
/// Defines the hours this exchange is open
/// The cash object that represent the quote currency
/// The symbol properties for this security
/// Currency converter used to convert
/// instances into units of the account currency
/// Provides all data types registered in the algorithm
/// Cache to store security information
/// Future underlying security
public Option(Symbol symbol,
SecurityExchangeHours exchangeHours,
Cash quoteCurrency,
OptionSymbolProperties symbolProperties,
ICurrencyConverter currencyConverter,
IRegisteredSecurityDataTypesProvider registeredTypes,
SecurityCache securityCache,
Security underlying)
: this(symbol,
quoteCurrency,
symbolProperties,
new OptionExchange(exchangeHours),
securityCache,
new OptionPortfolioModel(),
new ImmediateFillModel(),
new InteractiveBrokersFeeModel(),
NullSlippageModel.Instance,
new ImmediateSettlementModel(),
Securities.VolatilityModel.Null,
new OptionMarginModel(),
new OptionDataFilter(),
new SecurityPriceVariationModel(),
currencyConverter,
registeredTypes,
underlying)
{
}
///
/// Creates instance of the Option class.
///
///
/// Allows for the forwarding of the security configuration to the
/// base Security constructor
///
protected Option(Symbol symbol,
Cash quoteCurrency,
SymbolProperties symbolProperties,
SecurityExchange exchange,
SecurityCache cache,
ISecurityPortfolioModel portfolioModel,
IFillModel fillModel,
IFeeModel feeModel,
ISlippageModel slippageModel,
ISettlementModel settlementModel,
IVolatilityModel volatilityModel,
IBuyingPowerModel buyingPowerModel,
ISecurityDataFilter dataFilter,
IPriceVariationModel priceVariationModel,
ICurrencyConverter currencyConverter,
IRegisteredSecurityDataTypesProvider registeredTypesProvider,
Security underlying
) : base(
symbol,
quoteCurrency,
symbolProperties,
exchange,
cache,
portfolioModel,
fillModel,
feeModel,
slippageModel,
settlementModel,
volatilityModel,
buyingPowerModel,
dataFilter,
priceVariationModel,
currencyConverter,
registeredTypesProvider,
Securities.MarginInterestRateModel.Null
)
{
ExerciseSettlement = SettlementType.PhysicalDelivery;
SetDataNormalizationMode(DataNormalizationMode.Raw);
OptionExerciseModel = new DefaultExerciseModel();
PriceModel = symbol.ID.OptionStyle switch
{
// CRR model has the best accuracy and speed suggested by
// Branka, Zdravka & Tea (2014). Numerical Methods versus Bjerksund and Stensland Approximations for American Options Pricing.
// International Journal of Economics and Management Engineering. 8:4.
// Available via: https://downloads.dxfeed.com/specifications/dxLibOptions/Numerical-Methods-versus-Bjerksund-and-Stensland-Approximations-for-American-Options-Pricing-.pdf
// Also refer to OptionPriceModelTests.MatchesIBGreeksBulk() test,
// we select the most accurate and computational efficient model
OptionStyle.American => OptionPriceModels.BinomialCoxRossRubinstein(),
OptionStyle.European => OptionPriceModels.BlackScholes(),
_ => throw new ArgumentException("Invalid OptionStyle")
};
Holdings = new OptionHolding(this, currencyConverter);
_symbolProperties = (OptionSymbolProperties)symbolProperties;
SetFilter(-1, 1, TimeSpan.Zero, TimeSpan.FromDays(35));
Underlying = underlying;
OptionAssignmentModel = new DefaultOptionAssignmentModel();
ScaledStrikePrice = StrikePrice * SymbolProperties.StrikeMultiplier;
}
// save off a strongly typed version of symbol properties
private readonly OptionSymbolProperties _symbolProperties;
///
/// Returns true if this is the option chain security, false if it is a specific option contract
///
public bool IsOptionChain => Symbol.IsCanonical();
///
/// Returns true if this is a specific option contract security, false if it is the option chain security
///
public bool IsOptionContract => !Symbol.IsCanonical();
///
/// Gets the strike price
///
public decimal StrikePrice => Symbol.ID.StrikePrice;
///
/// Gets the strike price multiplied by the strike multiplier
///
public decimal ScaledStrikePrice
{
get;
private set;
}
///
/// Gets the expiration date
///
public DateTime Expiry => Symbol.ID.Date;
///
/// Gets the right being purchased (call [right to buy] or put [right to sell])
///
public OptionRight Right => Symbol.ID.OptionRight;
///
/// Gets the option style
///
public OptionStyle Style => Symbol.ID.OptionStyle;
///
/// Gets the most recent bid price if available
///
public override decimal BidPrice => Cache.BidPrice;
///
/// Gets the most recent ask price if available
///
public override decimal AskPrice => Cache.AskPrice;
///
/// When the holder of an equity option exercises one contract, or when the writer of an equity option is assigned
/// an exercise notice on one contract, this unit of trade, usually 100 shares of the underlying security, changes hands.
///
public int ContractUnitOfTrade
{
get
{
return _symbolProperties.ContractUnitOfTrade;
}
set
{
_symbolProperties.SetContractUnitOfTrade(value);
}
}
///
/// The contract multiplier for the option security
///
public int ContractMultiplier
{
get
{
return (int)_symbolProperties.ContractMultiplier;
}
set
{
_symbolProperties.SetContractMultiplier(value);
}
}
///
/// Aggregate exercise amount or aggregate contract value. It is the total amount of cash one will pay (or receive) for the shares of the
/// underlying stock if he/she decides to exercise (or is assigned an exercise notice). This amount is not the premium paid or received for an equity option.
///
public decimal GetAggregateExerciseAmount()
{
return StrikePrice * ContractMultiplier;
}
///
/// Returns the directional quantity of underlying shares that are going to change hands on exercise/assignment of all
/// contracts held by this account, taking into account the contract's as well as the contract's current
/// , which may have recently changed due to a split/reverse split in the underlying security.
///
///
/// Long option positions result in exercise while short option positions result in assignment. This function uses the term
/// exercise loosely to refer to both situations.
///
public decimal GetExerciseQuantity()
{
// negate Holdings.Quantity to match an equivalent order
return GetExerciseQuantity(-Holdings.Quantity);
}
///
/// Returns the directional quantity of underlying shares that are going to change hands on exercise/assignment of the
/// specified , taking into account the contract's as well
/// as the contract's current , which may have recently changed due to a split/reverse
/// split in the underlying security.
///
///
/// Long option positions result in exercise while short option positions result in assignment. This function uses the term
/// exercise loosely to refer to both situations.
///
/// The quantity of contracts being exercised as provided by the .
/// A negative value indicates exercise (we are long and the order quantity is negative to bring us (closer) to zero.
/// A positive value indicates assignment (we are short and the order quantity is positive to bring us (closer) to zero.
public decimal GetExerciseQuantity(decimal exerciseOrderQuantity)
{
// when exerciseOrderQuantity > 0 [ we are short ]
// && right == call => we sell to contract holder => negative
// && right == put => we buy from contract holder => positive
// when exerciseOrderQuantity < 0 [ we are long ]
// && right == call => we buy from contract holder => positive
// && right == put => we sell to contract holder => negative
var sign = Right == OptionRight.Call ? -1 : 1;
return sign * exerciseOrderQuantity * ContractUnitOfTrade;
}
///
/// Checks if option is eligible for automatic exercise on expiration
///
public bool IsAutoExercised(decimal underlyingPrice)
{
return GetIntrinsicValue(underlyingPrice) >= 0.01m;
}
///
/// Intrinsic value function of the option
///
public decimal GetIntrinsicValue(decimal underlyingPrice)
{
return OptionPayoff.GetIntrinsicValue(underlyingPrice, ScaledStrikePrice, Right);
}
///
/// Option payoff function at expiration time
///
/// The price of the underlying
///
public decimal GetPayOff(decimal underlyingPrice)
{
return OptionPayoff.GetPayOff(underlyingPrice, ScaledStrikePrice, Right);
}
///
/// Option out of the money function
///
/// The price of the underlying
///
public decimal OutOfTheMoneyAmount(decimal underlyingPrice)
{
return Math.Max(0, Right == OptionRight.Call ? ScaledStrikePrice - underlyingPrice : underlyingPrice - ScaledStrikePrice);
}
///
/// Specifies if option contract has physical or cash settlement on exercise
///
public SettlementType ExerciseSettlement
{
get; set;
}
///
/// Gets or sets the underlying security object.
///
public Security Underlying
{
get; set;
}
///
/// Gets a reduced interface of the underlying security object.
///
ISecurityPrice IOptionPrice.Underlying => Underlying;
///
/// For this option security object, evaluates the specified option
/// contract to compute a theoretical price, IV and greeks
///
/// The current data slice. This can be used to access other information
/// available to the algorithm
/// The option contract to evaluate
/// An instance of containing the theoretical
/// price of the specified option contract
public OptionPriceModelResult EvaluatePriceModel(Slice slice, OptionContract contract)
{
return PriceModel.Evaluate(this, slice, contract);
}
///
/// Gets or sets the price model for this option security
///
public IOptionPriceModel PriceModel
{
get; set;
}
///
/// Fill model used to produce fill events for this security
///
public IOptionExerciseModel OptionExerciseModel
{
get; set;
}
///
/// The automatic option assignment model
///
public IOptionAssignmentModel OptionAssignmentModel
{
get; set;
}
///
/// When enabled, approximates Greeks if corresponding pricing model didn't calculate exact numbers
///
[Obsolete("This property has been deprecated. Please use QLOptionPriceModel.EnableGreekApproximation instead.")]
public bool EnableGreekApproximation
{
get
{
var model = PriceModel as QLOptionPriceModel;
if (model != null)
{
return model.EnableGreekApproximation;
}
return false;
}
set
{
var model = PriceModel as QLOptionPriceModel;
if (model != null)
{
model.EnableGreekApproximation = value;
}
}
}
///
/// Gets or sets the contract filter
///
public IDerivativeSecurityFilter ContractFilter
{
get; set;
}
///
/// Sets the automatic option assignment model
///
/// The option assignment model to use
public void SetOptionAssignmentModel(PyObject pyObject)
{
if (pyObject.TryConvert(out var optionAssignmentModel))
{
// pure C# implementation
SetOptionAssignmentModel(optionAssignmentModel);
}
else if (Extensions.TryConvert(pyObject, out _, allowPythonDerivative: true))
{
SetOptionAssignmentModel(new OptionAssignmentModelPythonWrapper(pyObject));
}
else
{
using(Py.GIL())
{
throw new ArgumentException($"SetOptionAssignmentModel: {pyObject.Repr()} is not a valid argument.");
}
}
}
///
/// Sets the automatic option assignment model
///
/// The option assignment model to use
public void SetOptionAssignmentModel(IOptionAssignmentModel optionAssignmentModel)
{
OptionAssignmentModel = optionAssignmentModel;
}
///
/// Sets the option exercise model
///
/// The option exercise model to use
public void SetOptionExerciseModel(PyObject pyObject)
{
if (pyObject.TryConvert(out var optionExerciseModel))
{
// pure C# implementation
SetOptionExerciseModel(optionExerciseModel);
}
else if (Extensions.TryConvert(pyObject, out _, allowPythonDerivative: true))
{
SetOptionExerciseModel(new OptionExerciseModelPythonWrapper(pyObject));
}
else
{
using (Py.GIL())
{
throw new ArgumentException($"SetOptionExerciseModel: {pyObject.Repr()} is not a valid argument.");
}
}
}
///
/// Sets the option exercise model
///
/// The option exercise model to use
public void SetOptionExerciseModel(IOptionExerciseModel optionExerciseModel)
{
OptionExerciseModel = optionExerciseModel;
}
///
/// Sets the to a new instance of the filter
/// using the specified min and max strike values. Contracts with expirations further than 35
/// days out will also be filtered.
///
/// The min strike rank relative to market price, for example, -1 would put
/// a lower bound of one strike under market price, where a +1 would put a lower bound of one strike
/// over market price
/// The max strike rank relative to market place, for example, -1 would put
/// an upper bound of on strike under market price, where a +1 would be an upper bound of one strike
/// over market price
public void SetFilter(int minStrike, int maxStrike)
{
SetFilterImp(universe => universe.Strikes(minStrike, maxStrike));
}
///
/// Sets the to a new instance of the filter
/// using the specified min and max strike and expiration range values
///
/// The minimum time until expiry to include, for example, TimeSpan.FromDays(10)
/// would exclude contracts expiring in less than 10 days
/// The maximum time until expiry to include, for example, TimeSpan.FromDays(10)
/// would exclude contracts expiring in more than 10 days
public void SetFilter(TimeSpan minExpiry, TimeSpan maxExpiry)
{
SetFilterImp(universe => universe.Expiration(minExpiry, maxExpiry));
}
///
/// Sets the to a new instance of the filter
/// using the specified min and max strike and expiration range values
///
/// The min strike rank relative to market price, for example, -1 would put
/// a lower bound of one strike under market price, where a +1 would put a lower bound of one strike
/// over market price
/// The max strike rank relative to market place, for example, -1 would put
/// an upper bound of on strike under market price, where a +1 would be an upper bound of one strike
/// over market price
/// The minimum time until expiry to include, for example, TimeSpan.FromDays(10)
/// would exclude contracts expiring in less than 10 days
/// The maximum time until expiry to include, for example, TimeSpan.FromDays(10)
/// would exclude contracts expiring in more than 10 days
public void SetFilter(int minStrike, int maxStrike, TimeSpan minExpiry, TimeSpan maxExpiry)
{
SetFilterImp(universe => universe
.Strikes(minStrike, maxStrike)
.Expiration(minExpiry, maxExpiry));
}
///
/// Sets the to a new instance of the filter
/// using the specified min and max strike and expiration range values
///
/// The min strike rank relative to market price, for example, -1 would put
/// a lower bound of one strike under market price, where a +1 would put a lower bound of one strike
/// over market price
/// The max strike rank relative to market place, for example, -1 would put
/// an upper bound of on strike under market price, where a +1 would be an upper bound of one strike
/// over market price
/// The minimum time, expressed in days, until expiry to include, for example, 10
/// would exclude contracts expiring in less than 10 days
/// The maximum time, expressed in days, until expiry to include, for example, 10
/// would exclude contracts expiring in more than 10 days
public void SetFilter(int minStrike, int maxStrike, int minExpiryDays, int maxExpiryDays)
{
SetFilterImp(universe => universe
.Strikes(minStrike, maxStrike)
.Expiration(minExpiryDays, maxExpiryDays));
}
///
/// Sets the to a new universe selection function
///
/// new universe selection function
public void SetFilter(Func universeFunc)
{
ContractFilter = new FuncSecurityDerivativeFilter(universe =>
{
var optionUniverse = universe as OptionFilterUniverse;
var result = universeFunc(optionUniverse);
return result.ApplyTypesFilter();
});
ContractFilter.Asynchronous = false;
}
///
/// Sets the to a new universe selection function
///
/// new universe selection function
public void SetFilter(PyObject universeFunc)
{
ContractFilter = new FuncSecurityDerivativeFilter(universe =>
{
var optionUniverse = universe as OptionFilterUniverse;
using (Py.GIL())
{
PyObject result = (universeFunc as dynamic)(optionUniverse);
//Try to convert it to the possible outcomes and process it
//Must try filter first, if it is a filter and you try and convert it to
//list, TryConvert() with catch an exception. Later Python algo will break on
//this exception because we are using Py.GIL() and it will see the error set
OptionFilterUniverse filter;
List list;
if ((result).TryConvert(out filter))
{
optionUniverse = filter;
}
else if ((result).TryConvert(out list))
{
optionUniverse = optionUniverse.WhereContains(list);
}
else
{
throw new ArgumentException($"QCAlgorithm.SetFilter: result type {result.GetPythonType()} from " +
$"filter function is not a valid argument, please return either a OptionFilterUniverse or a list of symbols");
}
}
return optionUniverse.ApplyTypesFilter();
});
ContractFilter.Asynchronous = false;
}
///
/// Sets the data normalization mode to be used by this security
///
public override void SetDataNormalizationMode(DataNormalizationMode mode)
{
if (mode != DataNormalizationMode.Raw)
{
throw new ArgumentException("DataNormalizationMode.Raw must be used with options");
}
base.SetDataNormalizationMode(mode);
}
private void SetFilterImp(Func universeFunc)
{
ContractFilter = new FuncSecurityDerivativeFilter(universe =>
{
var optionUniverse = universe as OptionFilterUniverse;
var result = universeFunc(optionUniverse);
return result.ApplyTypesFilter();
});
}
///
/// Updates the symbol properties of this security
///
internal override void UpdateSymbolProperties(SymbolProperties symbolProperties)
{
if (symbolProperties != null)
{
SymbolProperties = new OptionSymbolProperties(symbolProperties);
}
}
}
}