Exemplo n.º 1
0
    // Implied Vol
    public static void Example12()
    {
        // My Option data
        double      CoP          = 1;
        double      underlying   = 99.29;
        double      strike       = 99.375;
        double      daysToExpiry = 154;
        double      yieldVol     = 0.46;
        STFutOption myOpt        = new STFutOption(CoP, underlying, strike, daysToExpiry, yieldVol);

        // Printing some data
        Console.WriteLine("Given yieldVol: {0:F6} price is: {1:F6}", yieldVol, myOpt.Price());
        double newPrice    = 0.05;                    // changing price
        double newYieldVol = myOpt.ImplVol(newPrice); // implied vol

        Console.WriteLine("Given price: {0:F6} impliedVol is: {1:F6}", newPrice, newYieldVol);
        // now I use implied vol calculated to check if newPrice return
        myOpt.new_YieldVol = newYieldVol;
        Console.WriteLine("Given yieldVol: {0:F6} price is: {1:F6}", newYieldVol, myOpt.Price());
        Console.WriteLine("Using for 'F6' format for output");

        // Checking YieldVol to PriceVol and vice versa
        Console.WriteLine(STFutOption.FromYieldVolToPriceVol(94.53, 0.2592));
        Console.WriteLine(STFutOption.FromPriceVolToYieldVol(94.53, 0.015));
    }
Exemplo n.º 2
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.º 3
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());
    }
Exemplo n.º 4
0
    // Tensor: simulation of values of an option. Row prices, column vols, nThird daysToMaturity
    public static void Example5()
    {
        // Values are hard-coded, can be generalized. Data for rows, columns, nthrd
        Vector <double> Prices         = new Vector <double>(new double[] { 99.10, 99.15, 99.20, 99.25, 99.30 }, 0);
        Vector <double> DaysToMaturity = new Vector <double>(new double[] { 82, 81, 80, 79, 78, 76 }, 0);
        Vector <double> Vols           = new Vector <double>(new double[] { 0.35, 0.40, 0.45 }, 0);

        // Create my option
        STFutOption opx1  = new STFutOption(1, 99.20, 99.25, 82, 0.40);
        STFutOption opxk  = new STFutOption(1, 99.10, 99.25, 82, 0.40);
        double      dummy = opxk.Price();

        // Create my tensor
        Tensor <double> MyTensor = new Tensor <double>(Prices.MaxIndex + 1, Vols.MaxIndex + 1, DaysToMaturity.MaxIndex + 1, 0, 0, 0);

        // Populating tensor
        for (int t = DaysToMaturity.MinIndex; t <= DaysToMaturity.MaxIndex; t++)
        {
            for (int r = Prices.MinIndex; r <= Prices.MaxIndex; r++)
            {
                for (int c = Vols.MinIndex; c <= Vols.MaxIndex; c++)
                {
                    opx1.new_DaysToExpiry = DaysToMaturity[t];
                    opx1.new_S            = Prices[r];
                    opx1.new_Sigma        = Vols[c];
                    MyTensor[r, c, t]     = opx1.Price() / 100.0;
                }
            }
        }

        // Printing out
        for (int t = DaysToMaturity.MinIndex; t <= DaysToMaturity.MaxIndex; t++)
        {
            Console.WriteLine("Now is Day: {0}", t);
            for (int r = Prices.MinIndex; r <= Prices.MaxIndex; r++)
            {
                Console.Write("{0:F2} (Vols{1:F2}/{2:F2}/{3:F2})  :", Prices[r], Vols[0], Vols[1], Vols[2]);
                for (int c = Vols.MinIndex; c <= Vols.MaxIndex; c++)
                {
                    Console.Write("{0:F5} ", MyTensor[r, c, t]);
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
Exemplo n.º 5
0
    // Class ListedSTFutOption and AssocMatrix
    public static void Example4()
    {
        // Stir (Short Term Interest Rate ) Option example.
        // (Please check details on www.euronext.com) Example is based on Black Formula

        // I create my option
        STFutOption opx1 = new STFutOption(1, 97.935, 98.25, 175, 0.4756);

        // option deltas
        Console.WriteLine("{0}, {1}", opx1.Price(), opx1.Delta());
        opx1.SwitchCallPut();
        Console.WriteLine("{0}, {1}", opx1.Price(), opx1.Delta());

        // Call and put checking deltas and call put parity
        double      S            = 97.935;
        double      K            = 98.25;
        double      DaysToExpiry = 175;
        STFutOption Call         = new STFutOption(1, S, K, 175, 0.4756);
        STFutOption Put          = new STFutOption(-1, S, K, 175, 0.4756);

        Console.WriteLine("{0},{1},{2}", Call.Delta(), Put.Delta(), Call.Delta() - Put.Delta()); // Call/Put delta are reversed
        Console.WriteLine("{0}", Call.Price() - Put.Price() - S + K);                            // Call/Put parity

        // I will crate a matrix showing deltas of a option, for different underlying price (rows) and passing the time (columns)
        // header Row: different value of underlying. the center is the current one
        // header column: passing the time, less days to maturity

        // Time passing - column
        int    d_columns = 20;  // how many days from and including today
        double shift_c   = 1.0; // interval in days between each columns

        // Changing value of underlying
        int    d_rows  = 15;   // how many values plus or minus the value
        double shift_h = 0.10; // interval in underlying

        NumericMatrix <double> deltaMatrix = new NumericMatrix <double>(d_rows * 2 + 1, d_columns);

        // Underlying value
        double       d_r        = -d_rows;
        Set <double> underlying = new Set <double>();

        for (int i = 0; i < deltaMatrix.Rows; i++)
        {
            underlying.Insert(S + d_r * shift_h);
            d_r++;
        }

        // Days to maturity
        double       d_c  = 0;
        Set <double> days = new Set <double>();

        for (int i = 0; i < deltaMatrix.Columns; i++)
        {
            days.Insert(DaysToExpiry + d_c * shift_c);
            d_c--;
        }

        // Populate DeltaMatrix
        int my_r = 1;
        int my_c = 1;

        foreach (double valueS in underlying)
        {
            Call.new_S = valueS;

            foreach (double valueDays in days)
            {
                Call.new_DaysToExpiry   = valueDays;
                deltaMatrix[my_r, my_c] = Call.Delta();
                my_c++;
            }

            my_r++;
            my_c = 1;
        }

        // Creating AssocMatrix
        AssocMatrix <double, double, double> OutMatrix = new AssocMatrix <double, double, double>(underlying, days, deltaMatrix);

        // Print associative matrices in Excel, to "StirDeltas" sheet
        ExcelMechanisms exl = new ExcelMechanisms();

        exl.printAssocMatrixInExcel <double, double, double>(OutMatrix, "StirDeltas");
    }