public static double BinomialTreePricing(AmericanOptionProb prob) { double s = prob.SpotPrice; double k = prob.StrikePrice; double t = prob.Maturity; double r = prob.InterestRate; double sigma = prob.Volatility; OptionTypeEnum optionType = prob.OptionType; int n = prob.StepsNum; double dt = t / n; double u = Math.Exp(sigma * Math.Sqrt(dt)); double d = Math.Exp(-sigma * Math.Sqrt(dt)); double p = (Math.Exp(r * dt) - d) / (u - d); double[,] stockTree = new double[n + 1, n + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= i; j++) { stockTree[i, j] = s * Math.Pow(u, j) * Math.Pow(d, i - j); } } double[,] valueTree = new double[n + 1, n + 1]; for (int j = 0; j <= n; j++) { valueTree[n, j] = BinomialTreePayoff(stockTree[n, j], k, optionType); } for (int i = n - 1; i >= 0; i--) { for (int j = 0; j <= i; j++) { valueTree[i, j] = Math.Exp(-r * dt) * (p * valueTree[i + 1, j + 1] + (1 - p) * valueTree[i + 1, j]); if (optionType == OptionTypeEnum.Call) { valueTree[i, j] = Math.Max(stockTree[i, j] - k, valueTree[i, j]); } else { valueTree[i, j] = Math.Max(k - stockTree[i, j], valueTree[i, j]); } } } return(valueTree[0, 0]); }
private void DoCalculation() { IsCalculateEnabled = false; RaisePropertyChanged("IsCalculateEnabled"); ErrorCode errcode = FirstValidation(); if (errcode != ErrorCode.OK) { Result = Utility.getErrorMsg(errcode); } else { var prob = new AmericanOptionProb( _spotPrice, _volatility, _interestRate, _maturity, _strikePrice, _stepsNum, _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"); }