Exemplo n.º 1
0
        /// <summary>
        /// Gets the implied vol.
        /// </summary>
        /// <param name="stock">The stock.</param>
        /// <param name="exp">The exp.</param>
        /// <param name="volGuess">The vol guess.</param>
        /// <param name="strike">The strike.</param>
        /// <param name="call">if set to <c>true</c> [call].</param>
        /// <param name="style">The style.</param>
        /// <param name="prem">The prem.</param>
        /// <param name="gridSteps">The grid steps.</param>
        /// <returns></returns>
        public static double GetImpliedVol(Stock stock, DateTime exp, double volGuess, double strike, bool call, string style, double prem, int gridSteps)
        {
            DateTime date0     = stock.Date;
            DateTime exp0      = exp;
            double   spot0     = Convert.ToDouble(stock.Spot);
            double   strike0   = strike;
            string   paystyle0 = call ? "C" : "P";
            string   exercise0 = style;
            //Override vol guess to test if it effects performance
            double fwd    = stock.GetForward(date0, exp);
            var    option = new AmOptionAnalytics(date0, exp0, spot0, strike0, volGuess, exercise0, paystyle0, stock.RateCurve, stock.Dividends, gridSteps);
            double vol0   = option.OptSolveVol(prem, fwd);

            return(vol0);
        }
Exemplo n.º 2
0
        public void TestImpliedVolPut()
        {
            DateTime date0    = DateTime.Today;
            DateTime exp      = date0.AddDays(90);
            DateTime ex       = date0.AddDays(20);
            double   spot     = 100;
            double   strike   = 100;
            double   vol      = 0.50;
            string   paystyle = "P";

            DateTime[]      dates = { date0, exp };
            double[]        rates = { 0.05, 0.05 };
            RateCurve       rc    = new RateCurve("AUD", "Continuous", date0, dates, rates);
            List <Dividend> divs  = new List <Dividend> {
                new Dividend(ex, 20)
            };
            AmOptionAnalytics amInstr  = new AmOptionAnalytics(date0, exp, spot, strike, vol, "A", paystyle, rc, divs, 120);
            AmOptionAnalytics euInstr  = new AmOptionAnalytics(date0, exp, spot, strike, 2 * vol, "E", paystyle, rc, divs, 120);
            double            am       = amInstr.Price();
            double            eu       = euInstr.Price();
            AmOptionAnalytics amInstr0 = new AmOptionAnalytics(date0, exp, spot, strike, 0.20, "A", paystyle, rc, divs, 120);
            AmOptionAnalytics euInstr0 = new AmOptionAnalytics(date0, exp, spot, strike, 0.80, "E", paystyle, rc, divs, 120);
            double            avol0    = amInstr0.OptSolveVol(am, 100);
            double            evol0    = euInstr0.OptSolveVol(eu, 100);
            Stock             stock    = new Stock(date0, 100, "dummy", "BHP", rc, divs);
            double            vol1     = OptionHelper.GetImpliedVol(stock, exp, strike, false, "A", am, 120);
            double            vol11    = OptionHelper.GetImpliedVol(stock, exp, strike, false, "American", am, 120);
            double            vol0     = OptionHelper.GetImpliedVol(stock, exp, strike, false, "E", eu, 120);
            double            vol00    = OptionHelper.GetImpliedVol(stock, exp, strike, false, "European", eu, 120);

            Assert.AreEqual(avol0, 0.50, 0.001);
            Assert.AreEqual(evol0, 1.00, 0.001);
            Assert.AreEqual(vol1, avol0, 0.001);
            Assert.AreEqual(vol11, avol0, 0.001);
            Assert.AreEqual(vol0, evol0, 0.001);
            Assert.AreEqual(vol00, evol0, 0.001);
        }
Exemplo n.º 3
0
        public void TestImpliedVolCall()
        {
            DateTime date0    = DateTime.Today;
            DateTime exp      = date0.AddDays(90);
            DateTime ex       = date0.AddDays(20);
            double   spot     = 100;
            double   strike   = 100;
            double   vol      = 0.50;
            string   paystyle = "C";

            DateTime[]      dates = { date0, exp };
            double[]        rates = { 0.05, 0.05 };
            RateCurve       rc    = new RateCurve("AUD", "Continuous", date0, dates, rates);
            List <Dividend> divs  = new List <Dividend> {
                new Dividend(ex, 20)
            };
            AmOptionAnalytics amInstr  = new AmOptionAnalytics(date0, exp, spot, strike, vol, "A", paystyle, rc, divs, 120);
            AmOptionAnalytics euInstr  = new AmOptionAnalytics(date0, exp, spot, strike, 2 * vol, "E", paystyle, rc, divs, 120);
            double            am       = amInstr.Price();
            double            eu       = euInstr.Price();
            AmOptionAnalytics amInstr0 = new AmOptionAnalytics(date0, exp, spot, strike, 0.20, "A", paystyle, rc, divs, 120);
            AmOptionAnalytics euInstr0 = new AmOptionAnalytics(date0, exp, spot, strike, 0.80, "E", paystyle, rc, divs, 120);
            double            avol0    = amInstr0.OptSolveVol(am, 100);
            double            evol0    = euInstr0.OptSolveVol(eu, 100);
            var      stock             = new Stock(date0, 100, "dummy", "BHP", rc, divs);
            DateTime start             = DateTime.Now;
            double   vol1 = OptionHelper.GetImpliedVol(stock, exp, 0.3, strike, true, "A", am, 120);
            DateTime end  = DateTime.Now;
            double   x1   = end.Subtract(start).Milliseconds;

            start = DateTime.Now;
            double vol11 = OptionHelper.GetImpliedVol(stock, exp, 0.2, strike, true, "American", am, 120);

            end = DateTime.Now;
            double x2 = end.Subtract(start).Milliseconds;

            start = DateTime.Now;
            double vol0 = OptionHelper.GetImpliedVol(stock, exp, 0.8, strike, true, "E", eu, 120);

            end = DateTime.Now;
            double x3 = end.Subtract(start).Milliseconds;

            start = DateTime.Now;
            double vol00 = OptionHelper.GetImpliedVol(stock, exp, 0.8, strike, true, "European", eu, 120);

            end = DateTime.Now;
            double x4 = end.Subtract(start).Milliseconds;

            Assert.AreEqual(avol0, 0.50, 0.001);
            Assert.AreEqual(evol0, 1.00, 0.001);

            Assert.AreEqual(vol1, avol0, 0.001);
            Assert.AreEqual(vol11, avol0, 0.001);

            Assert.AreEqual(vol0, evol0, 0.001);
            Assert.AreEqual(vol00, evol0, 0.001);

            Debug.Print("Time 1 : " + x1);
            Debug.Print("Time 2 : " + x2);
            Debug.Print("Time 3 : " + x3);
            Debug.Print("Time 4 : " + x4);
        }