public void testFdImpliedVol() { var settlementDate = new Date(26, 2, 2015); Settings.setEvaluationDate(settlementDate); var calendar = new TARGET(); var dayCounter = new Actual365Fixed(); const double volatility = 0.5; var underlyingQuote = new Handle <Quote>(new SimpleQuote(3227)); var flatTermStructure = new Handle <YieldTermStructure>(new FlatForward(settlementDate, 0.05, dayCounter)); var flatDividendYield = new Handle <YieldTermStructure>(new FlatForward(settlementDate, 0, dayCounter)); var flatVolatility = new Handle <BlackVolTermStructure>(new BlackConstantVol(settlementDate, calendar, volatility, dayCounter)); var process = new BlackScholesMertonProcess(underlyingQuote, flatDividendYield, flatTermStructure, flatVolatility); var exercise = new AmericanExercise(new Date(1, 12, 2015)); var pricingEngine = new FDDividendAmericanEngine(process); var payoff = new PlainVanillaPayoff(Option.Type.Put, 3200); var dividendDates = new[] { new Date(1, 3, 2015) }; var dividendAmounts = new[] { 10d }; var option = new DividendVanillaOption(payoff, exercise, dividendDates.ToList(), dividendAmounts.ToList()); option.setPricingEngine(pricingEngine); var npv = option.NPV(); var impliedVol = option.impliedVolatility(npv, process); const double tolerance = 3.0e-3; if (Math.Abs(impliedVol - volatility) > tolerance) { QAssert.Fail(string.Format("Implied volatility calculation failed. Expected {0}. Actual {1}", volatility, impliedVol)); } }
public void testFDDividendAmericanEngine() { /* * Valuation date: 20 July 2018 * Maturity date: 17 Aug 2018 * Type: Call * Spot: 2900 * Strike: 2800 * Volatility: 20 % * Interest rate: 0 % * * Dividend(paid one day before expiry) * Date: 16 Aug 2018 * Value: 40 * * NPV = 124.37658 */ var result = 124.37658; var settlementDate = new Date(20, 7, 2018); Settings.setEvaluationDate(settlementDate); var calendar = new TARGET(); var dayCounter = new Actual365Fixed(); var spot = new Handle <Quote>(new SimpleQuote(2900)); var qRate = new Handle <Quote>(new SimpleQuote(0.0)); var rRate = new Handle <Quote>(new SimpleQuote(0.0)); var vol = new Handle <Quote>(new SimpleQuote(0.2)); var flatDividendYield = new Handle <YieldTermStructure>(new FlatForward(settlementDate, qRate, dayCounter)); var flatTermStructure = new Handle <YieldTermStructure>(new FlatForward(settlementDate, rRate, dayCounter)); var flatVolatility = new Handle <BlackVolTermStructure>(new BlackConstantVol(settlementDate, calendar, vol, dayCounter)); var process = new BlackScholesMertonProcess(spot, flatDividendYield, flatTermStructure, flatVolatility); var exercise = new AmericanExercise(new Date(17, 8, 2018)); var pricingEngine = new FDDividendAmericanEngine(process); var payoff = new PlainVanillaPayoff(Option.Type.Call, 2800); var dividendDates = new[] { new Date(16, 8, 2018) }; var dividendAmounts = new[] { 40d }; var option = new DividendVanillaOption(payoff, exercise, dividendDates.ToList(), dividendAmounts.ToList()); option.setPricingEngine(pricingEngine); var npv = option.NPV(); const double tolerance = 1.0e-5; if (Math.Abs(npv - result) > tolerance) { QAssert.Fail(string.Format("NPV calculation failed. Expected {0}. Actual {1}", result, npv)); } }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(FDDividendAmericanEngine obj) { return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr); }
private void Button_Click(object sender, RoutedEventArgs e) { Option.Type optionType; if (CallorPut.Text == "Call") { optionType = Option.Type.Call; } else { optionType = Option.Type.Put; } double underlyingPrice = Convert.ToDouble(Stockprice.Text); double strikePrice = Convert.ToDouble(Strikeprice.Text); double dividendYield = 0.0; double riskFreeRate = Convert.ToDouble(Intrate.Text); double volatility = Convert.ToDouble(Resultvol.Text) / 100; Date todaydate = Date.todaysDate(); string expd = Datepick.Text; Date maturityDate = new Date(); if (expd[1].ToString() is "/") { expd = '0' + expd; } if (expd[4].ToString() is "/") { expd = expd.Substring(0, 3) + '0' + expd.Substring(3); } maturityDate = DateParser.parseFormatted(expd, "%m/%d/%Y"); Settings.instance().setEvaluationDate(todaydate); Date settlementDate = new Date(); settlementDate = todaydate; QuantLib.Calendar calendar = new TARGET(); AmericanExercise americanExercise = new AmericanExercise(settlementDate, maturityDate); EuropeanExercise europeanExercise = new EuropeanExercise(maturityDate); DayCounter dayCounter = new Actual365Fixed(); YieldTermStructureHandle flatRateTSH = new YieldTermStructureHandle( new FlatForward(settlementDate, riskFreeRate, dayCounter)); YieldTermStructureHandle flatDividendTSH = new YieldTermStructureHandle( new FlatForward(settlementDate, dividendYield, dayCounter)); BlackVolTermStructureHandle flatVolTSH = new BlackVolTermStructureHandle( new BlackConstantVol(settlementDate, calendar, volatility, dayCounter)); QuoteHandle underlyingQuoteH = new QuoteHandle(new SimpleQuote(underlyingPrice)); BlackScholesMertonProcess stochasticProcess = new BlackScholesMertonProcess(underlyingQuoteH, flatDividendTSH, flatRateTSH, flatVolTSH); PlainVanillaPayoff payoff = new PlainVanillaPayoff(optionType, strikePrice); VanillaOption americanOption = new VanillaOption(payoff, americanExercise); VanillaOption americanOption2 = new VanillaOption(payoff, americanExercise); VanillaOption europeanOption = new VanillaOption(payoff, europeanExercise); //americanOption.setPricingEngine( // new BaroneAdesiWhaleyEngine(stochasticProcess)); //americanOption2.setPricingEngine( // new BinomialVanillaEngine(stochasticProcess, "coxrossrubinstein",1000)); europeanOption.setPricingEngine( new AnalyticEuropeanEngine(stochasticProcess)); //double opprice = Math.Round(americanOption2.NPV(),3); Date divdate1 = new Date(14, Month.December, 2019); DoubleVector divpay = new DoubleVector(); DateVector divDates = new DateVector(); //divpay.Add(.0001); //divDates.Add(divdate1); DividendVanillaOption americanOption1 = new DividendVanillaOption(payoff, americanExercise, divDates, divpay); FDDividendAmericanEngine engine = new FDDividendAmericanEngine(stochasticProcess); americanOption1.setPricingEngine(engine); double opprice4 = americanOption1.NPV(); //double vol1 = americanOption1.impliedVolatility(opprice4, stochasticProcess, .001); double delta1 = Math.Round(americanOption1.delta(), 2); double gamma1 = Math.Round(americanOption1.gamma(), 2); double theta1 = Math.Round(europeanOption.theta() / 365, 2); double vega1 = Math.Round(europeanOption.vega() / 100, 2); double oppricedisplay = Math.Round(opprice4, 3); Resultam.Text = oppricedisplay.ToString(); Resultam_Delta.Text = delta1.ToString(); Resultam_Gamma.Text = gamma1.ToString(); Resultam_Theta.Text = theta1.ToString(); Resultam_Vega.Text = vega1.ToString(); }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(FDDividendAmericanEngine obj) { return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr; }