|
| 1 | +/* |
| 2 | + * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. |
| 3 | + * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System; |
| 17 | +using MathNet.Numerics.Distributions; |
| 18 | +using Python.Runtime; |
| 19 | +using QuantConnect.Data; |
| 20 | + |
| 21 | +namespace QuantConnect.Indicators |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// Option Delta indicator that calculate the delta of an option |
| 25 | + /// </summary> |
| 26 | + /// <remarks>sensitivity of option price relative to $1 of underlying change</remarks> |
| 27 | + public class Delta : OptionGreeksIndicatorBase |
| 28 | + { |
| 29 | + /// <summary> |
| 30 | + /// Initializes a new instance of the Delta class |
| 31 | + /// </summary> |
| 32 | + /// <param name="name">The name of this indicator</param> |
| 33 | + /// <param name="option">The option to be tracked</param> |
| 34 | + /// <param name="riskFreeRateModel">Risk-free rate model</param> |
| 35 | + /// <param name="optionModel">The option pricing model used to estimate Delta</param> |
| 36 | + /// <param name="ivModel">The option pricing model used to estimate IV</param> |
| 37 | + public Delta(string name, Symbol option, IRiskFreeInterestRateModel riskFreeRateModel, |
| 38 | + OptionPricingModelType optionModel = OptionPricingModelType.BlackScholes, OptionPricingModelType? ivModel = null) |
| 39 | + : base(name, option, riskFreeRateModel, optionModel: optionModel, ivModel: ivModel) |
| 40 | + { |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Initializes a new instance of the Delta class |
| 45 | + /// </summary> |
| 46 | + /// <param name="option">The option to be tracked</param> |
| 47 | + /// <param name="riskFreeRateModel">Risk-free rate model</param> |
| 48 | + /// <param name="optionModel">The option pricing model used to estimate Delta</param> |
| 49 | + /// <param name="ivModel">The option pricing model used to estimate IV</param> |
| 50 | + public Delta(Symbol option, IRiskFreeInterestRateModel riskFreeRateModel, |
| 51 | + OptionPricingModelType optionModel = OptionPricingModelType.BlackScholes, OptionPricingModelType? ivModel = null) |
| 52 | + : this($"Delta({optionModel})", option, riskFreeRateModel, optionModel, ivModel) |
| 53 | + { |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Initializes a new instance of the Delta class |
| 58 | + /// </summary> |
| 59 | + /// <param name="name">The name of this indicator</param> |
| 60 | + /// <param name="option">The option to be tracked</param> |
| 61 | + /// <param name="riskFreeRateModel">Risk-free rate model</param> |
| 62 | + /// <param name="optionModel">The option pricing model used to estimate Delta</param> |
| 63 | + /// <param name="ivModel">The option pricing model used to estimate IV</param> |
| 64 | + public Delta(string name, Symbol option, PyObject riskFreeRateModel, |
| 65 | + OptionPricingModelType optionModel = OptionPricingModelType.BlackScholes, OptionPricingModelType? ivModel = null) |
| 66 | + : base(name, option, riskFreeRateModel, optionModel: optionModel, ivModel: ivModel) |
| 67 | + { |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Initializes a new instance of the Delta class |
| 72 | + /// </summary> |
| 73 | + /// <param name="option">The option to be tracked</param> |
| 74 | + /// <param name="riskFreeRateModel">Risk-free rate model</param> |
| 75 | + /// <param name="optionModel">The option pricing model used to estimate Delta</param> |
| 76 | + /// <param name="ivModel">The option pricing model used to estimate IV</param> |
| 77 | + public Delta(Symbol option, PyObject riskFreeRateModel, OptionPricingModelType optionModel = OptionPricingModelType.BlackScholes, |
| 78 | + OptionPricingModelType? ivModel = null) |
| 79 | + : this($"Delta({optionModel})", option, riskFreeRateModel, optionModel, ivModel) |
| 80 | + { |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Initializes a new instance of the Delta class |
| 85 | + /// </summary> |
| 86 | + /// <param name="option">The option to be tracked</param>am> |
| 87 | + /// <param name="riskFreeRate">Risk-free rate, as a constant</param> |
| 88 | + /// <param name="optionModel">The option pricing model used to estimate Delta</param> |
| 89 | + /// <param name="ivModel">The option pricing model used to estimate IV</param> |
| 90 | + public Delta(string name, Symbol option, decimal riskFreeRate = 0.05m, |
| 91 | + OptionPricingModelType optionModel = OptionPricingModelType.BlackScholes, OptionPricingModelType? ivModel = null) |
| 92 | + : base(name, option, riskFreeRate, optionModel: optionModel, ivModel: ivModel) |
| 93 | + { |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Initializes a new instance of the Delta class |
| 98 | + /// </summary> |
| 99 | + /// <param name="option">The option to be tracked</param> |
| 100 | + /// <param name="riskFreeRate">Risk-free rate, as a constant</param> |
| 101 | + /// <param name="optionModel">The option pricing model used to estimate Delta</param> |
| 102 | + /// <param name="ivModel">The option pricing model used to estimate IV</param> |
| 103 | + public Delta(Symbol option, decimal riskFreeRate = 0.05m, OptionPricingModelType optionModel = OptionPricingModelType.BlackScholes, |
| 104 | + OptionPricingModelType? ivModel = null) |
| 105 | + : this($"Delta({optionModel})", option, riskFreeRate, optionModel, ivModel) |
| 106 | + { |
| 107 | + } |
| 108 | + |
| 109 | + // Calculate the theoretical option price |
| 110 | + private decimal TheoreticalDelta(decimal spotPrice, decimal timeToExpiration, decimal volatility, |
| 111 | + OptionPricingModelType optionModel = OptionPricingModelType.BlackScholes) |
| 112 | + { |
| 113 | + var math = OptionGreekIndicatorsHelper.DecimalMath; |
| 114 | + |
| 115 | + switch (optionModel) |
| 116 | + { |
| 117 | + case OptionPricingModelType.BinomialCoxRossRubinstein: |
| 118 | + var upFactor = math(Math.Exp, volatility * math(Math.Sqrt, timeToExpiration / OptionGreekIndicatorsHelper.Steps)); |
| 119 | + if (upFactor == 1) |
| 120 | + { |
| 121 | + // provide a small step to estimate delta |
| 122 | + upFactor = 1.00001m; |
| 123 | + } |
| 124 | + |
| 125 | + var sU = spotPrice * upFactor; |
| 126 | + var sD = spotPrice * 1m / upFactor; |
| 127 | + |
| 128 | + var fU = OptionGreekIndicatorsHelper.CRRTheoreticalPrice(volatility, sU, Strike, timeToExpiration, RiskFreeRate, Right); |
| 129 | + var fD = OptionGreekIndicatorsHelper.CRRTheoreticalPrice(volatility, sD, Strike, timeToExpiration, RiskFreeRate, Right); |
| 130 | + |
| 131 | + return (fU - fD) / (sU - sD); |
| 132 | + |
| 133 | + case OptionPricingModelType.BlackScholes: |
| 134 | + default: |
| 135 | + var norm = new Normal(); |
| 136 | + var d1 = OptionGreekIndicatorsHelper.CalculateD1(spotPrice, Strike, timeToExpiration, RiskFreeRate, volatility); |
| 137 | + |
| 138 | + if (Right == OptionRight.Call) |
| 139 | + { |
| 140 | + return math(norm.CumulativeDistribution, d1); |
| 141 | + } |
| 142 | + |
| 143 | + return -math(norm.CumulativeDistribution, -d1); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + // Calculate the Delta of the option |
| 148 | + protected override decimal CalculateGreek(DateTime time) |
| 149 | + { |
| 150 | + var spotPrice = UnderlyingPrice.Current.Value; |
| 151 | + var timeToExpiration = Convert.ToDecimal((Expiry - time).TotalDays) / 365m; |
| 152 | + var volatility = ImpliedVolatility.Current.Value; |
| 153 | + |
| 154 | + return TheoreticalDelta(spotPrice, timeToExpiration, volatility, _optionModel); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments