@@ -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
0 commit comments