Skip to content

Commit e479c36

Browse files
committed
Add OptionPricingModelType, as option for greeks & IV estimation
1 parent 89a598a commit e479c36

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed

Indicators/ImpliedVolatility.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class ImpliedVolatility : BarIndicator, IIndicatorWarmUpPeriodProvider
2929
private BaseDataConsolidator _consolidator;
3030
private RateOfChange _roc;
3131
private decimal _impliedVolatility;
32-
private bool _binomial;
32+
private OptionPricingModelType _optionModel;
3333

3434
/// <summary>
3535
/// Gets the expiration time of the option
@@ -77,9 +77,10 @@ public class ImpliedVolatility : BarIndicator, IIndicatorWarmUpPeriodProvider
7777
/// <param name="option">The option to be tracked</param>am>
7878
/// <param name="riskFreeRate">The risk free rate</param>
7979
/// <param name="period">The lookback period of historical volatility</param>
80-
/// <param name="binomial">Should option priced under binomial model?</param>
81-
public ImpliedVolatility(Symbol option, decimal riskFreeRate = 0.05m, int period = 252, bool binomial = false)
82-
: this($"IV({option.Value},{riskFreeRate},{period},{binomial})", option, riskFreeRate, period, binomial)
80+
/// <param name="optionModel">The option pricing model used to estimate IV</param>
81+
public ImpliedVolatility(Symbol option, decimal riskFreeRate = 0.05m, int period = 252,
82+
OptionPricingModelType optionModel = OptionPricingModelType.BlackScholes)
83+
: this($"IV({option.Value},{riskFreeRate},{period},{optionModel})", option, riskFreeRate, period, optionModel)
8384
{
8485
}
8586

@@ -90,8 +91,9 @@ public ImpliedVolatility(Symbol option, decimal riskFreeRate = 0.05m, int period
9091
/// <param name="option">The option to be tracked</param>
9192
/// <param name="riskFreeRate">The risk free rate</param>
9293
/// <param name="period">The lookback period of historical volatility</param>
93-
/// <param name="binomial">Should option priced under binomial model?</param>
94-
public ImpliedVolatility(string name, Symbol option, decimal riskFreeRate = 0.05m, int period = 252, bool binomial = false)
94+
/// <param name="optionModel">The option pricing model used to estimate IV</param>
95+
public ImpliedVolatility(string name, Symbol option, decimal riskFreeRate = 0.05m, int period = 252,
96+
OptionPricingModelType optionModel = OptionPricingModelType.BlackScholes)
9597
: base(name)
9698
{
9799
var sid = option.ID;
@@ -103,7 +105,7 @@ public ImpliedVolatility(string name, Symbol option, decimal riskFreeRate = 0.05
103105
_optionSymbol = option;
104106
_underlyingSymbol = option.Underlying;
105107
_roc = new(1);
106-
_binomial = binomial;
108+
_optionModel = optionModel;
107109

108110
Strike = sid.StrikePrice;
109111
Expiry = sid.Date;
@@ -172,14 +174,16 @@ protected override decimal ComputeNextValue(IBaseDataBar input)
172174

173175
// Calculate the theoretical option price
174176
private decimal TheoreticalPrice(decimal volatility, decimal spotPrice, decimal strikePrice, decimal timeToExpiration, decimal riskFreeRate,
175-
OptionRight optionType, bool binomial = false)
177+
OptionRight optionType, OptionPricingModelType optionModel = OptionPricingModelType.BlackScholes)
176178
{
177-
if (binomial)
179+
switch (optionModel)
178180
{
179-
return OptionGreekIndicatorsHelper.CRRTheoreticalPrice(volatility, spotPrice, strikePrice, timeToExpiration, riskFreeRate, optionType);
181+
case OptionPricingModelType.BinomialCoxRossRubinstein:
182+
return OptionGreekIndicatorsHelper.CRRTheoreticalPrice(volatility, spotPrice, strikePrice, timeToExpiration, riskFreeRate, optionType);
183+
case OptionPricingModelType.BlackScholes:
184+
default:
185+
return OptionGreekIndicatorsHelper.BlackTheoreticalPrice(volatility, spotPrice, strikePrice, timeToExpiration, riskFreeRate, optionType);
180186
}
181-
// IV is calculated under BSM framework in default
182-
return OptionGreekIndicatorsHelper.BlackTheoreticalPrice(volatility, spotPrice, strikePrice, timeToExpiration, riskFreeRate, optionType);
183187
}
184188

185189
// Calculate the IV of the option
@@ -189,7 +193,7 @@ private decimal CalculateIV(DateTime time)
189193
var spotPrice = UnderlyingPrice.Current.Value;
190194
var timeToExpiration = Convert.ToDecimal((Expiry - time).TotalDays) / 365m;
191195

192-
Func<decimal, decimal> f = (vol) => TheoreticalPrice(vol, spotPrice, Strike, timeToExpiration, RiskFreeRate, Right, _binomial);
196+
Func<decimal, decimal> f = (vol) => TheoreticalPrice(vol, spotPrice, Strike, timeToExpiration, RiskFreeRate, Right, _optionModel);
193197
return OptionGreekIndicatorsHelper.BrentApproximation(f, price, 0.01m, 1.0m);
194198
}
195199

Indicators/OptionPricingModelType.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
namespace QuantConnect.Indicators
17+
{
18+
/// <summary>
19+
/// Defines different types of option pricing model
20+
/// </summary>
21+
public enum OptionPricingModelType
22+
{
23+
/// <summary>
24+
/// Vanilla Black Scholes Model
25+
/// </summary>
26+
BlackScholes,
27+
/// <summary>
28+
/// The Cox-Ross-Rubinstein binomial tree model (CRR model)
29+
/// </summary>
30+
BinomialCoxRossRubinstein
31+
}
32+
}

Tests/Indicators/ImpliedVolatilityTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public void ComparesIVOnCRRModel(decimal price, decimal spotPrice, OptionRight r
145145
{
146146
// Under CRR framework
147147
var symbol = Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, right, 450m, _reference.AddDays(expiry));
148-
var indicator = new ImpliedVolatility(_symbol, 0.04m, binomial: true);
148+
var indicator = new ImpliedVolatility(_symbol, 0.04m, optionModel: OptionPricingModelType.BinomialCoxRossRubinstein);
149149

150150
var optionTradeBar = new TradeBar(_reference, _symbol, price, price, price, price, 0m);
151151
var spotTradeBar = new TradeBar(_reference, _underlying, spotPrice, spotPrice, spotPrice, spotPrice, 0m);

0 commit comments

Comments
 (0)