Esempio n. 1
0
        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));
            }
        }
Esempio n. 2
0
        private void Button_Click_1(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.January, 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 Inoprice = Convert.ToDouble(Resultam.Text);
            double vol1     = americanOption1.impliedVolatility(Inoprice, stochasticProcess, .0001);

            vol1 = Math.Round(vol1, 4) * 100;
            double delta1 = Math.Round(americanOption2.delta(), 2);
            double gamma1 = Math.Round(americanOption2.gamma(), 2);
            double theta1 = Math.Round(europeanOption.theta() / 365, 2);
            double vega1  = Math.Round(europeanOption.vega() / 100, 2);

            //Resultam.Text = opprice4.ToString();
            Resultam_Delta.Text = delta1.ToString();
            Resultam_Gamma.Text = gamma1.ToString();
            Resultam_Theta.Text = theta1.ToString();
            Resultam_Vega.Text  = vega1.ToString();
            Resultvol.Text      = vol1.ToString();
        }