private void DoCalculation() { IsCalculateEnabled = false; RaisePropertyChanged("IsCalculateEnabled"); ErrorCode errcode = FirstValidation(); if (errcode != ErrorCode.OK) { Result = Utility.getErrorMsg(errcode); } else { var prob = new ImpliedVolatilityProb( _spotPrice, _interestRate, _maturity, _strikePrice, _optionPremium, _optionType ); errcode = prob.validate(); if (errcode != ErrorCode.OK) { Result = Utility.getErrorMsg(errcode); } else { Result = Math.Round(prob.calculate(), 4).ToString(); } } RaisePropertyChanged("Result"); IsCalculateEnabled = true; RaisePropertyChanged("IsCalculateEnabled"); }
public static double ImpliedVolatilityCalculate(ImpliedVolatilityProb prob) { double s = prob.SpotPrice; double k = prob.StrikePrice; double t = prob.Maturity; double r = prob.InterestRate; double premium = prob.OptionPremium; OptionTypeEnum optionType = prob.OptionType; double sigmaHat = Math.Sqrt(2 * Math.Abs((Math.Log(s / k) + r * t) / t)); double tol = Double.MinValue; double sigma = sigmaHat; double sigmaDiff = 1; int n = 1, nmax = 100; while (sigmaDiff >= tol && n < nmax) { double increment = GetIncrement(s, k, t, r, sigma, premium, optionType); sigma = sigma - increment; sigmaDiff = Math.Abs(increment); n++; } return(sigma); }