public EuropeanOption(double s, double k, double t, double sigma, double r, Constants.EuroOptionType optionType) { this.S = s; this.K = k; this.T = t; this.Sigma = sigma; this.R = r; this.OptionType = optionType; }
public void Test() { //Triple A == Arrange, Act, Assert //Arrange int iPrecision = 4; double dStockPrice = 50; double dStrikePrice = 55; double dTimeToMaturity = 1; double dStdDerivUnderlying = 0.2; double dRiskFreeInterestRate = 0.09; Constants.EuroOptionType euroOptionType = Constants.EuroOptionType.Call; //Act FinancialProduct product = new EuropeanOption(dStockPrice, dStrikePrice, dTimeToMaturity, dStdDerivUnderlying, dRiskFreeInterestRate, euroOptionType); var pricer = new EuropeanOptionPricer(iPrecision); double dResultPricing = pricer.Price(product); //Assert Assert.AreEqual(dResultPricing.ToString(), "3.8617"); }
/// <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); }