Exemplo n.º 1
0
    // Method: given a volatility guess and a price will return implied vol
    public double ImplVol(double price)
    {
        // We try to translate in c# the idea of Paul Wilmott Introduces Quantitative Finance ch 10
        double error        = 1E-10;                                         // precision of calculation
        double implYieldVol = FromPriceVolToYieldVol(this.S, this.PriceVol); // the starting vol value is the one used in the constructor

        // data used in iteration process
        double priceError;                                    // price error
        double dv = error + 1;                                // vega correction
        double price_calculated = 0.0, vega_calculated = 0.0; // used to iterate

        // my option to iterate
        STFutOption myOpt = new STFutOption(this.COP, this.S, this.X, this.DaysToExpiry, implYieldVol);

        // iteration
        while (Math.Abs(dv) > error)
        {
            // I iterate according to vega information
            price_calculated = 0; vega_calculated = 0;

            myOpt.new_YieldVol = implYieldVol;
            price_calculated   = myOpt.Price();
            vega_calculated    = myOpt.Vega();

            priceError    = price_calculated - price;
            dv            = priceError / vega_calculated;
            implYieldVol -= dv;
        }
        ;
        return(implYieldVol);
    }
Exemplo n.º 2
0
    // Class STFutOption
    public static void Example2()
    {
        // Option data for opt1 and opt2
        double CoP          = -1;
        double underlying   = 99.23;
        double strike       = 99.25;
        double daysToExpiry = 82;
        double yieldVol     = 0.449; // (_priceVol * _underlying) / (100 - _underlying);

        // option data only for
        double      daysPerYear = 360;
        STFutOption myOpt1      = new STFutOption(CoP, underlying, strike, daysToExpiry, yieldVol);
        STFutOption myOpt2      = new STFutOption(CoP, underlying, strike, daysToExpiry, yieldVol, daysPerYear);

        // Showing Data
        Console.WriteLine("CoP = {0}; underlying = {1}; strike = {2}; daysToExpiry = {3}; yieldVol = {4}",
                          CoP, underlying, strike, daysToExpiry, yieldVol);

        Console.WriteLine("daysPerYear Opt1 = {0} daysPerYear Opt2 = {1}", 365, daysPerYear);
        Console.WriteLine("CoP\t opt1: {0}\t opt2: {1}", myOpt1.COP, myOpt2.COP);
        Console.WriteLine("Price\t opt1: {0:F4}\t opt2: {1:F4}", myOpt1.Price() / 100, myOpt2.Price() / 100);
        Console.WriteLine("Delta\t opt1: {0:F4}\t opt2: {1:F4}", myOpt1.Delta(), myOpt2.Delta());
        Console.WriteLine("Gamma\t opt1: {0:F4}\t opt2: {1:F4}", myOpt1.Gamma(), myOpt2.Gamma());
        Console.WriteLine("Theta\t opt1: {0:F4}\t opt2: {1:F4}", myOpt1.Theta(), myOpt2.Theta());
        Console.WriteLine("Vega \t opt1: {0:F4}\t opt2: {1:F4}", myOpt1.Vega(), myOpt2.Vega());

        // Changing data:now opt2 become a call, underlying and expiry change
        myOpt2.SwitchCallPut(); // now is a call
        underlying = 99.05;     // new price
        daysToExpiry--;         // a day pass

        // updating options
        myOpt1.new_DaysToExpiry = daysToExpiry;
        myOpt2.new_DaysToExpiry = daysToExpiry;
        myOpt1.new_S            = underlying;
        myOpt2.new_S            = underlying;

        // showing new data
        Console.WriteLine("underlying = {0}; daysToExpiry = {1}", underlying, daysToExpiry);
        Console.WriteLine("CoP\t opt1: {0}\t opt2: {1}", myOpt1.COP, myOpt2.COP);
        Console.WriteLine("Price\t opt1: {0:F4}\t opt2: {1:F4}", myOpt1.Price() / 100, myOpt2.Price() / 100);
        Console.WriteLine("Delta\t opt1: {0:F4}\t opt2: {1:F4}", myOpt1.Delta(), myOpt2.Delta());
        Console.WriteLine("Gamma\t opt1: {0:F4}\t opt2: {1:F4}", myOpt1.Gamma(), myOpt2.Gamma());
        Console.WriteLine("Theta\t opt1: {0:F4}\t opt2: {1:F4}", myOpt1.Theta(), myOpt2.Theta());
        Console.WriteLine("Vega \t opt1: {0:F4}\t opt2: {1:F4}", myOpt1.Vega(), myOpt2.Vega());
    }