Пример #1
0
        public override double[,] MakingOptionValueTree(double K, double r, double sigma, double T, int N, double div, double[,] StTree)
        {
            // Initial class Precalculate
            PreCalculate precal = new PreCalculate();

            precal.sigma = sigma;
            precal.r     = r;
            precal.T     = T;
            precal.N     = N;
            precal.div   = div;
            // Get pu, pm, pd under discount
            double disc_pu = precal.Get_disc_pu();
            double disc_pm = precal.Get_disc_pm();
            double disc_pd = precal.Get_disc_pd();

            //Initiate St and outcome Vt.
            double[,] St = new double[2 * N + 1, N + 1];
            double[,] Vt = new double[2 * N + 1, N + 1];
            // Copy old array that got from method to the new array.
            for (int i = 0; i <= 2 * N; i++)
            {
                for (int j = 0; j <= N; j++)
                {
                    St[i, j] = StTree[i, j];
                }
            }

            // Calculate the option value of last step if it is a call option.
            if (Iscall == true)
            {
                for (int j = (2 * N); j >= 0; j--)
                {
                    Vt[j, N] = Math.Max(St[j, N] - K, 0);
                }
            }
            // Calculate the option value of last step if it is a put option.
            else
            {
                for (int j = (2 * N); j >= 0; j--)
                {
                    Vt[j, N] = Math.Max(K - St[j, N], 0);
                }
            }


            for (int i = N - 1; i >= 0; i--)
            {
                for (int j = 2 * i; j >= 0; j--)
                {
                    // Every node, except that on the last step, equals expectation of nodes' on last step.
                    Vt[j, i] = disc_pu * Vt[j, i + 1] + disc_pm * Vt[j + 1, i + 1] + disc_pd * Vt[j + 2, i + 1];
                }
            }

            return(Vt);
        }
Пример #2
0
        public double[,] MakeUnderlyingTree(double S, double sigma, double T, int N)
        {
            PreCalculate precal = new PreCalculate();

            precal.sigma = sigma;
            precal.T     = T;
            precal.N     = N;

            // Get St on every node.
            // Get discount rate
            double edx = precal.Get_edx();
            // Get size of moving price dx
            double dx = precal.Get_dx();

            //Initiate the outcome St.
            double[,] St = new double[2 * N + 1, N + 1];

            // Calculate the underlying price if it always goes down in the whole period.
            St[2 * N, N] = S * Math.Exp(-N * dx);

            // Calculate all the possible underlying price at the last step.
            for (int j = (2 * N - 1); j >= 0; j--)
            {
                St[j, N] = St[j + 1, N] * edx;
            }

            // Calculate all underlying prices St.
            for (int i = N - 1; i >= 0; i--)
            {
                for (int j = 2 * i; j >= 0; j--)
                {
                    // Every node, except that on the last step, has node on the next step which has the same price.
                    St[j, i] = St[j + 1, i + 1];
                }
            }

            return(St);
        }
Пример #3
0
        public void Output_Calculation(double S, double K, double r, double T, double sigma, int N, double div)
        {
            // Get essential classes' instances.
            PreCalculate            precal = new PreCalculate();
            TrinomialUnderlyingTree tree   = new TrinomialUnderlyingTree();
            Option             opt         = new Option();
            EurOption          Eur_option  = new EurOption();
            AmericanOption     Amer_option = new AmericanOption();
            Greek              greeks      = new Greek();
            VegaRhoCalculation vega_cal    = new VegaRhoCalculation();

            // Initial array St and Vt
            double[,] St = new double[2 * N + 1, N + 1];
            double[,] Vt = new double[2 * N + 1, N + 1];

            // Initial interval that used in Greeks
            double diff_sigma = 0.001 * sigma;
            double diff_r     = 0.001 * r;

            // Judge if this is an European Option
            if (IsEuropean == true)
            {
                //Judge if this is a call option.
                if (IsCall == true)
                {
                    Console.WriteLine("The Underlying Prices Trinomial Tree:");
                    // Initial the parameters
                    Eur_option.Iscall   = true;
                    vega_cal.IsEuropean = true;
                    vega_cal.Iscall     = true;

                    // Ilerate underlying trinomial tree St.
                    for (int i = 0; i <= 2 * N; i++)
                    {
                        for (int j = 0; j <= N; j++)
                        {
                            St[i, j] = Math.Round(tree.MakeUnderlyingTree(S, sigma, T, N)[i, j], 2);
                            Console.Write(St[i, j].ToString() + "\t");
                        }
                        Console.WriteLine();
                    }

                    Console.WriteLine();
                    Console.WriteLine("The Option Value Trinomial Tree:");

                    // Ilerate option value trinomial tree Vt
                    for (int i = 0; i <= 2 * N; i++)
                    {
                        for (int j = 0; j <= N; j++)
                        {
                            Vt[i, j] = Math.Round(Eur_option.MakingOptionValueTree(K, r, sigma, T, N, div, St)[i, j], 2);
                            Console.Write(Vt[i, j].ToString() + "\t");
                        }
                        Console.WriteLine();
                    }
                    // Output the option price
                    Console.WriteLine("European Call Option Price is" + "  " + Vt[0, 0].ToString());
                    Console.WriteLine();

                    // Output Greeks
                    Console.WriteLine("Greeks of this option ");
                    greeks.St = St;
                    greeks.Vt = Vt;
                    double delta = greeks.Delta();
                    Console.WriteLine("Delta =" + "  " + delta.ToString());
                    double gamma = greeks.Gamma();
                    Console.WriteLine("Gamma =" + "  " + gamma.ToString());
                    double theta = greeks.Theta(T, N);
                    Console.WriteLine("Theta =" + "  " + theta.ToString());
                    double vega = vega_cal.GetVegaRho(diff_sigma, diff_r, S, K, r, sigma, T, N, div)[0, 0];
                    double rho  = vega_cal.GetVegaRho(diff_sigma, diff_r, S, K, r, sigma, T, N, div)[0, 1];
                    Console.WriteLine("Vega =" + "  " + vega.ToString());
                    Console.WriteLine("Rho =" + "  " + rho.ToString());
                    Console.ReadLine();
                }
                // If this is a put option.
                else
                {
                    Console.WriteLine("The Underlying Prices Trinomial Tree:");
                    // Initial the parameters
                    Eur_option.Iscall   = false;
                    vega_cal.IsEuropean = true;
                    vega_cal.Iscall     = false;
                    // Ilerate underlying trinomial tree St.
                    for (int i = 0; i <= 2 * N; i++)
                    {
                        for (int j = 0; j <= N; j++)
                        {
                            St[i, j] = Math.Round(tree.MakeUnderlyingTree(S, sigma, T, N)[i, j], 2);
                            Console.Write(St[i, j].ToString() + "\t");
                        }
                        Console.WriteLine();
                    }

                    Console.WriteLine();
                    Console.WriteLine("The Option Value Trinomial Tree:");

                    // Ilerate option value trinomial tree Vt
                    for (int i = 0; i <= 2 * N; i++)
                    {
                        for (int j = 0; j <= N; j++)
                        {
                            Vt[i, j] = Math.Round(Eur_option.MakingOptionValueTree(K, r, sigma, T, N, div, St)[i, j], 2);
                            Console.Write(Vt[i, j].ToString() + "\t");
                        }
                        Console.WriteLine();
                    }
                    // Output option price.
                    Console.WriteLine("European Put Option Price is" + "  " + Vt[0, 0].ToString());
                    Console.WriteLine();

                    // Output Greeks
                    Console.WriteLine("Greeks of this option :");
                    greeks.St = St;
                    greeks.Vt = Vt;
                    double delta = greeks.Delta();
                    Console.WriteLine("Delta =" + "  " + delta.ToString());
                    double gamma = greeks.Gamma();
                    Console.WriteLine("Gamma =" + "  " + gamma.ToString());
                    double theta = greeks.Theta(T, N);
                    Console.WriteLine("Theta =" + "  " + theta.ToString());
                    double vega = vega_cal.GetVegaRho(diff_sigma, diff_r, S, K, r, sigma, T, N, div)[0, 0];
                    double rho  = vega_cal.GetVegaRho(diff_sigma, diff_r, S, K, r, sigma, T, N, div)[0, 1];
                    Console.WriteLine("Vega =" + "  " + vega.ToString());
                    Console.WriteLine("Rho =" + "  " + rho.ToString());
                    Console.ReadLine();
                }
            }
            // If this is an American Option
            else
            {
                //Judge if this is a call option.
                if (IsCall == true)
                {
                    Console.WriteLine("The Underlying Prices Trinomial Tree:");
                    // Initial the parameters
                    Amer_option.Iscall  = true;
                    vega_cal.IsEuropean = false;
                    vega_cal.Iscall     = true;
                    // Ilerate underlying trinomial tree St.
                    for (int i = 0; i <= 2 * N; i++)
                    {
                        for (int j = 0; j <= N; j++)
                        {
                            St[i, j] = Math.Round(tree.MakeUnderlyingTree(S, sigma, T, N)[i, j], 2);
                            Console.Write(St[i, j].ToString() + "\t");
                        }
                        Console.WriteLine();
                    }

                    Console.WriteLine();
                    Console.WriteLine("The Option Value Trinomial Tree:");

                    // Ilerate option value trinomial tree Vt
                    for (int i = 0; i <= 2 * N; i++)
                    {
                        for (int j = 0; j <= N; j++)
                        {
                            Vt[i, j] = Math.Round(Amer_option.MakingOptionValueTree(K, r, sigma, T, N, div, St)[i, j], 2);
                            Console.Write(Vt[i, j].ToString() + "\t");
                        }
                        Console.WriteLine();
                    }
                    // Output American call option price
                    Console.WriteLine("American Call Option Price is" + "  " + Vt[0, 0].ToString());
                    Console.WriteLine();

                    // Output Greeks
                    Console.WriteLine("Greeks of this option:");
                    greeks.St = St;
                    greeks.Vt = Vt;
                    double delta = greeks.Delta();
                    Console.WriteLine("Delta =" + "  " + delta.ToString());
                    double gamma = greeks.Gamma();
                    Console.WriteLine("Gamma =" + "  " + gamma.ToString());
                    double theta = greeks.Theta(T, N);
                    Console.WriteLine("Theta =" + "  " + theta.ToString());
                    double vega = vega_cal.GetVegaRho(diff_sigma, diff_r, S, K, r, sigma, T, N, div)[0, 0];
                    double rho  = vega_cal.GetVegaRho(diff_sigma, diff_r, S, K, r, sigma, T, N, div)[0, 1];
                    Console.WriteLine("Vega =" + "  " + vega.ToString());
                    Console.WriteLine("Rho =" + "  " + rho.ToString());
                    Console.ReadLine();
                }
                //If this is a put option.
                else
                {
                    Console.WriteLine("The Underlying Prices Trinomial Tree:");
                    // Initial the parameters
                    Amer_option.Iscall  = false;
                    vega_cal.IsEuropean = false;
                    vega_cal.Iscall     = false;
                    // Ilerate underlying trinomial tree St.
                    for (int i = 0; i <= 2 * N; i++)
                    {
                        for (int j = 0; j <= N; j++)
                        {
                            St[i, j] = Math.Round(tree.MakeUnderlyingTree(S, sigma, T, N)[i, j], 2);
                            Console.Write(St[i, j].ToString() + "\t");
                        }
                        Console.WriteLine();
                    }

                    Console.WriteLine();
                    Console.WriteLine("The Option Value Trinomial Tree:");

                    // Ilerate option value trinomial tree Vt
                    for (int i = 0; i <= 2 * N; i++)
                    {
                        for (int j = 0; j <= N; j++)
                        {
                            Vt[i, j] = Math.Round(Amer_option.MakingOptionValueTree(K, r, sigma, T, N, div, St)[i, j], 2);
                            Console.Write(Vt[i, j].ToString() + "\t");
                        }
                        Console.WriteLine();
                    }

                    // Output American call option price
                    Console.WriteLine("American Put Option Price is" + "  " + Vt[0, 0].ToString());
                    Console.WriteLine();

                    // Output Greeks
                    Console.WriteLine("Greeks of this option:");
                    greeks.St = St;
                    greeks.Vt = Vt;
                    double delta = greeks.Delta();
                    Console.WriteLine("Delta =" + "  " + delta.ToString());
                    double gamma = greeks.Gamma();
                    Console.WriteLine("Gamma =" + "  " + gamma.ToString());
                    double theta = greeks.Theta(T, N);
                    Console.WriteLine("Theta =" + "  " + theta.ToString());
                    double vega = vega_cal.GetVegaRho(diff_sigma, diff_r, S, K, r, sigma, T, N, div)[0, 0];
                    double rho  = vega_cal.GetVegaRho(diff_sigma, diff_r, S, K, r, sigma, T, N, div)[0, 1];
                    Console.WriteLine("Vega =" + "  " + vega.ToString());
                    Console.WriteLine("Rho =" + "  " + rho.ToString());
                    Console.ReadLine();
                }
            }
        }