/// <summary> /// The method "Price" is dedicated to compute the price of the European Call or Put /// </summary> /// <param name=product>The product to be priced</param> /// <returns>The expected price</returns> public override double Price(FinancialProduct product) { double result = 0; EuropeanOption euroOption = (product as EuropeanOption); if (euroOption != null) { //do something with it double d1 = (Log(euroOption.S / euroOption.K) + (euroOption.R + 0.5 * Pow(euroOption.Sigma, 2)) * euroOption.T) / (euroOption.Sigma * Sqrt(euroOption.T)); double d2 = (Log(euroOption.S / euroOption.K) + (euroOption.R - 0.5 * Pow(euroOption.Sigma, 2)) * euroOption.T) / (euroOption.Sigma * Sqrt(euroOption.T)); if (euroOption.OptionType == Constants.EuroOptionType.Call) { result = (euroOption.S * Cndf(d1) - euroOption.K * Exp(-euroOption.R * euroOption.T) * Cndf(d2)); } else if (euroOption.OptionType == Constants.EuroOptionType.Put) { result = (euroOption.K * Exp(-euroOption.R * euroOption.T) * Cndf(-d2) - euroOption.S * Cndf(-d1)); } } return(Math.Round(result, iPrecision)); }
/// <summary> /// The method CheckInputData represents parts that check the input data, converts them to the suitable formats /// and build the product to be priced later, /// it contains the definition of the input part, the verification of these ones and the display of the output results /// </summary> /// <param name=product>The product to be build and returned</param> /// <param name=iPrecision>The precision of Round of the final result, </param> /// <param name=strErrorMsg>The error Message that will be returned in case of issue</param> /// <returns>bool: the method was successfully finished or not</returns> private bool CheckInputData(out FinancialProduct product, ref int iPrecision, ref string strErrorMsg) { string strStockPrice = StockPriceTextBox.Text.ToString(); string strStrikePrice = strikePriceTextBox.Text.ToString(); string strTimeToMaturity = timeToMatTextBox.Text.ToString(); string strStdDerivUnderlying = StdDerivUnderlyingTextBox.Text.ToString(); string strRiskFreeInterestRate = RiskFreeInterestRateTextBox.Text.ToString(); string strFloatPrecision = floatPrecisionTextBox.Text.ToString(); double dStockPrice = 0; double dStrikePrice = 0; double dTimeToMaturity = 0; double dStdDerivUnderlying = 0; double dRiskFreeInterestRate = 0; bool bDataInput = true; Constants.Culture culture = 0; if (cultureComboBox.SelectedIndex > -1) { if (cultureComboBox.SelectedItem.ToString().Equals(Constants.CULTURE_GB)) { culture = Constants.Culture.GB; } else if (cultureComboBox.SelectedItem.ToString().Equals(Constants.CULTURE_FR)) { culture = Constants.Culture.FR; } else if (cultureComboBox.SelectedItem.ToString().Equals(Constants.CULTURE_ES)) { culture = Constants.Culture.ES; } } else { product = null; strErrorMsg = "Please select a choice in the culture box"; Console.WriteLine("Please select a choice in the culture box "); return(false); } if (bDataInput) { if (ConvertStringToDouble(strStockPrice, culture, out dStockPrice)) { Console.WriteLine("StockPrice --> {0}", dStockPrice); } else { strErrorMsg = "Please insert a correct format for the field Stock price, the suitable format is float"; Console.WriteLine("Unable to parse StockPrice '{0}'.", strStockPrice); product = null; return(false); } } if (bDataInput) { if (ConvertStringToDouble(strStrikePrice, culture, out dStrikePrice)) { Console.WriteLine("StrikePrice --> {0}", dStrikePrice); } else { strErrorMsg = "Please insert a correct format for the field Strike price, the suitable format is float"; Console.WriteLine("Unable to parse StrikePrice '{0}'.", strStrikePrice); product = null; return(false); } } if (bDataInput) { if (ConvertStringToDouble(strTimeToMaturity, culture, out dTimeToMaturity)) { Console.WriteLine("TimeToMaturity --> {0}", dTimeToMaturity); } else if (EvaluateDoubleWithSplit(strTimeToMaturity, culture, out dTimeToMaturity)) { Console.WriteLine("TimeToMaturity --> {0}", dTimeToMaturity); } else { strErrorMsg = "Please insert a correct format for the field Time to maturity in years, the suitable format is integer"; Console.WriteLine("Unable to parse TimeToMaturity '{0}'.", strTimeToMaturity); product = null; return(false); } } if (bDataInput) { if (ConvertStringToDouble(strStdDerivUnderlying, culture, out dStdDerivUnderlying)) { Console.WriteLine("StdDerivUnderlying --> {0}", dStdDerivUnderlying); } else { strErrorMsg = "Please insert a correct format for the field Standard deviation of underlying stock, the suitable format is float"; Console.WriteLine("Unable to parse StdDerivUnderlying '{0}'.", strStdDerivUnderlying); product = null; return(false); } } if (bDataInput) { if (ConvertStringToDouble(strRiskFreeInterestRate, culture, out dRiskFreeInterestRate)) { Console.WriteLine("RiskFreeInterestRate --> {0}", dRiskFreeInterestRate); } else { strErrorMsg = "Please insert a correct format for the field Risk-free interest rate, the suitable format is float"; Console.WriteLine("Unable to parse RiskFreeInterestRate '{0}'.", strRiskFreeInterestRate); product = null; return(false); } } Constants.EuroOptionType euroOptionType = 0; if (bDataInput) { if (optionTypeComboBox.SelectedIndex > -1) { if (optionTypeComboBox.SelectedItem.ToString().Equals(Constants.CALL)) { euroOptionType = Constants.EuroOptionType.Call; // todo if } else if (optionTypeComboBox.SelectedItem.ToString().Equals(Constants.PUT)) { euroOptionType = Constants.EuroOptionType.Put; // todo if } } else { strErrorMsg = "Please select a choice in the Option type box"; Console.WriteLine("Please select a choice in the Option type box"); product = null; return(false); } } if (bDataInput) { if (int.TryParse(strFloatPrecision, out iPrecision)) { Console.WriteLine("RiskFreeInterestRate --> {0}", iPrecision); } else { strErrorMsg = "Please insert a correct format for the field Risk-free interest rate, the suitable format is float"; Console.WriteLine("Unable to parse RiskFreeInterestRate '{0}'.", strFloatPrecision); product = null; return(false); } } product = new EuropeanOption(dStockPrice, dStrikePrice, dTimeToMaturity, dStdDerivUnderlying, dRiskFreeInterestRate, euroOptionType); return(bDataInput); }