public static void Main()
    {
        int typeB = 2;  // Binomial Lattice Type
        int typeT = 3;  // Trinomial Lattice Type

        int depth = 4;  // Number of periods of time

        double val = 4.0;

        Lattice <double> lattice1 = new Lattice <double>(depth, typeB, val);
        Lattice <double> lattice2 = new Lattice <double>(depth, typeT, val);


        // Examining the vector at base of lattice
        Vector <double> base1 = lattice1.BasePyramidVector();
        Vector <double> base2 = lattice2.BasePyramidVector();

        // Print columms of lattice
        for (int j = lattice1.MinIndex; j <= lattice1.MaxIndex; j++)
        {
            lattice1.PyramidVector(j).print();
        }

        string s = Console.ReadLine();

        // Arrays
        int             startIndex = lattice1.MinIndex;
        Vector <double> xarr       = new Vector <double>(depth + 1, startIndex);

        xarr[xarr.MinIndex] = 0.0;
        double T       = 1.0;
        int    NT      = 10;
        double delta_T = T / NT;

        for (int j = xarr.MinIndex + 1; j <= xarr.MaxIndex; j++)
        {
            xarr[j] = xarr[j - 1] + delta_T;
        }

        Console.WriteLine(base1.Size); Console.WriteLine(base2.Size);

        ExcelMechanisms exl = new ExcelMechanisms();

        try
        {
            exl.printLatticeInExcel(lattice2, xarr, "Lattice");
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }
Exemplo n.º 2
0
    /*  public static double EarlyImpl(double P, double S)
     * {
     *
     *    double K = 10.0;
     *
     *    if (P > K - S)
     *    {
     *        return P;
     *    }
     *    return K - S;
     * }*/

    // This could be made into a member function of Option

    public static void Main()
    {
        // Phase I: Create and initialise the option
        IOptionFactory fac = getFactory();

        int N = 200;

        Console.Write("Number of time steps: ");
        N = Convert.ToInt32(Console.ReadLine());

        double S;

        Console.Write("Underlying price: ");
        S = Convert.ToDouble(Console.ReadLine());

        Option opt = fac.create();

        double k = opt.T / N;

        // Create basic lattice
        double discounting = Math.Exp(-opt.r * k);

        // Phase II: Create the binomial method and forward induction
        BinomialLatticeStrategy binParams = getStrategy(opt.sig, opt.r, k, S, opt.K, N);     // Factory
        BinomialMethod          bn        = new BinomialMethod(discounting, binParams, N);

        bn.modifyLattice(S);

        // Phase III: Backward Induction and compute option price
        Vector <double> RHS = new Vector <double>(bn.BasePyramidVector());

        if (binParams.bType == BinomialType.Additive)
        {
            RHS[RHS.MinIndex] = S * Math.Exp(N * binParams.downValue());
            for (int j = RHS.MinIndex + 1; j <= RHS.MaxIndex; j++)
            {
                RHS[j] = RHS[j - 1] * Math.Exp(binParams.upValue() - binParams.downValue());
            }
        }

        Vector <double> Pay = opt.PayoffVector(RHS);

        double pr = bn.getPrice(Pay);

        Console.WriteLine("European {0}", pr);

        // Binomial method with early exercise
        BinomialMethod bnEarly = new BinomialMethod(discounting, binParams, N, opt.EarlyImpl);

        bnEarly.modifyLattice(S);
        Vector <double> RHS2 = new Vector <double>(bnEarly.BasePyramidVector());
        Vector <double> Pay2 = opt.PayoffVector(RHS2);
        double          pr2  = bnEarly.getPrice(Pay2);

        Console.WriteLine("American {0}", pr2);


        // Display in Excel; first create array of asset mesh points
        int             startIndex = 0;
        Vector <double> xarr       = new Vector <double>(N + 1, startIndex);

        xarr[xarr.MinIndex] = 0.0;
        for (int j = xarr.MinIndex + 1; j <= xarr.MaxIndex; j++)
        {
            xarr[j] = xarr[j - 1] + k;
        }

        // Display lattice in Excel
        ExcelMechanisms exl = new ExcelMechanisms();

        try
        {
            // public void printLatticeInExcel(Lattice<double> lattice, Vector<double> xarr, string SheetName)
            string sheetName = "Lattice";
            exl.printLatticeInExcel(bnEarly.getLattice(), xarr, sheetName);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }
Exemplo n.º 3
0
        public void C9_9_ExcelOutput()
        {
            var opt = _testOption;

            var steps = 50;
            var S     = opt.K;

            double k = opt.T / steps;

            double discounting = Math.Exp(-opt.r * k);

            var binParams = new CRRStrategy(opt.sig, opt.r, k);;  // Factory
            var bn        = new BinomialMethod(discounting, binParams, steps, opt.EarlyImpl);

            bn.ModifyLattice(opt.K);

            // Phase III: Backward Induction and compute option price
            Vector <double> RHS = new Vector <double>(bn.BasePyramidVector());

            if (binParams.BinomialType == BinomialType.Additive)
            {
                RHS[RHS.MinIndex] = S * Math.Exp(steps * binParams.DownValue);
                for (int j = RHS.MinIndex + 1; j <= RHS.MaxIndex; j++)
                {
                    RHS[j] = RHS[j - 1] * Math.Exp(binParams.UpValue - binParams.DownValue);
                }
            }

            var pay = opt.PayoffVector(RHS);

            var pr = bn.GetPrice(pay);

            // Display lattice in Excel
            var file = Path.GetTempFileName();

            file = Path.ChangeExtension(file, "xlsx");
            ExcelMechanisms exl = new ExcelMechanisms(file);

            try
            {
                // Display in Excel; first create array of asset mesh points
                int             startIndex = 0;
                Vector <double> xarr       = new Vector <double>(steps + 1, startIndex);
                xarr[xarr.MinIndex] = 0.0;
                for (int j = xarr.MinIndex + 1; j <= xarr.MaxIndex; j++)
                {
                    xarr[j] = xarr[j - 1] + k;
                }

                string sheetName = "Lattice";

                exl.printLatticeInExcel(bn.GetOptionLattice, xarr, sheetName);

                exl.Save();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            Assert.AreEqual(pr, 3.0732, 0.01);
        }