@@ -29,7 +29,7 @@ public class ImpliedVolatility : BarIndicator, IIndicatorWarmUpPeriodProvider
29
29
private BaseDataConsolidator _consolidator ;
30
30
private RateOfChange _roc ;
31
31
private decimal _impliedVolatility ;
32
- private bool _binomial ;
32
+ private OptionPricingModelType _optionModel ;
33
33
34
34
/// <summary>
35
35
/// Gets the expiration time of the option
@@ -77,9 +77,10 @@ public class ImpliedVolatility : BarIndicator, IIndicatorWarmUpPeriodProvider
77
77
/// <param name="option">The option to be tracked</param>am>
78
78
/// <param name="riskFreeRate">The risk free rate</param>
79
79
/// <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 )
83
84
{
84
85
}
85
86
@@ -90,8 +91,9 @@ public ImpliedVolatility(Symbol option, decimal riskFreeRate = 0.05m, int period
90
91
/// <param name="option">The option to be tracked</param>
91
92
/// <param name="riskFreeRate">The risk free rate</param>
92
93
/// <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 )
95
97
: base ( name )
96
98
{
97
99
var sid = option . ID ;
@@ -103,7 +105,7 @@ public ImpliedVolatility(string name, Symbol option, decimal riskFreeRate = 0.05
103
105
_optionSymbol = option ;
104
106
_underlyingSymbol = option . Underlying ;
105
107
_roc = new ( 1 ) ;
106
- _binomial = binomial ;
108
+ _optionModel = optionModel ;
107
109
108
110
Strike = sid . StrikePrice ;
109
111
Expiry = sid . Date ;
@@ -172,14 +174,16 @@ protected override decimal ComputeNextValue(IBaseDataBar input)
172
174
173
175
// Calculate the theoretical option price
174
176
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 )
176
178
{
177
- if ( binomial )
179
+ switch ( optionModel )
178
180
{
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 ) ;
180
186
}
181
- // IV is calculated under BSM framework in default
182
- return OptionGreekIndicatorsHelper . BlackTheoreticalPrice ( volatility , spotPrice , strikePrice , timeToExpiration , riskFreeRate , optionType ) ;
183
187
}
184
188
185
189
// Calculate the IV of the option
@@ -189,7 +193,7 @@ private decimal CalculateIV(DateTime time)
189
193
var spotPrice = UnderlyingPrice . Current . Value ;
190
194
var timeToExpiration = Convert . ToDecimal ( ( Expiry - time ) . TotalDays ) / 365m ;
191
195
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 ) ;
193
197
return OptionGreekIndicatorsHelper . BrentApproximation ( f , price , 0.01m , 1.0m ) ;
194
198
}
195
199
0 commit comments