예제 #1
0
        public static object HestonOneOptionPriceMC(double underlying,
                                                    double riskFreeRate,
                                                    double kappa,
                                                    double theta,
                                                    double sigma,
                                                    double rho,
                                                    double v0,
                                                    double maturity,
                                                    double strike,
                                                    string type,
                                                    int numSamplePaths,
                                                    int numSteps,
                                                    [ExcelArgument(Description = "Timeout set to 5 mins if not specified")]
                                                    int timeOutInMinutes)
        {
            try
            {
                if (ExcelDnaUtil.IsInFunctionWizard())
                {
                    return(null);
                }

                if (timeOutInMinutes.Equals(0))
                {
                    timeOutInMinutes = 5;
                }

                var task = Task.Run(() => Heston.HestonEuropeanOptionPriceMC(
                                        new HestonParametersGrading(underlying, riskFreeRate, kappa, theta, sigma, rho, v0),
                                        new EuropeanOptionGrading(maturity, strike, GetTypeOfVanillaEuropean(type)),
                                        new MonteCarloSettingsGrading(numSamplePaths, numSteps)));
                if (task.Wait(TimeSpan.FromMinutes(timeOutInMinutes)))
                {
                    return(task.Result);
                }
                else
                {
                    throw new Exception($"Timed out ({timeOutInMinutes} mins)");
                }
            }
            catch (Exception e)
            {
                return("HestonOneOptionPriceMC: unknown error: " + e.Message);
            }
        }
예제 #2
0
        public static void Task4()
        {
            // Variance Process Values
            Console.WriteLine("Task 4");
            // Variance Process Values
            double Kappa = 2.0;
            double Theta = 0.06;
            double Sigma = 0.4;
            double V0    = 0.04;
            double Rho   = 0.5;

            // Heston Model Params
            double InitialStockPrice = 100;
            double RiskFreeRate      = 0.1;

            // Option Params
            double     StrikePrice = 100;
            PayoffType Type        = PayoffType.Call;
            double     Maturity    = 1;

            VarianceProcessParameters varParams =
                new VarianceProcessParameters(Kappa, Theta, Sigma, V0, Rho);

            HestonModelParameters hestonModel =
                new HestonModelParameters(InitialStockPrice, RiskFreeRate, varParams);

            // ************Task 4 Print****************
            System.Console.WriteLine("*********************");
            HestonModelParamPrint(varParams, hestonModel);

            EuropeanOption euOption =
                new EuropeanOption(StrikePrice, Type, Maturity);

            double priceForm = Heston.HestonEuropeanOptionPrice(hestonModel, euOption);

            System.Console.WriteLine("K={0}, T={1}, refPrice={2}", StrikePrice,
                                     Maturity, priceForm);

            //Prepare csv
            var    csv     = new StringBuilder();
            String newLine = string.Format("Trials, Time Steps, relError");

            csv.AppendLine(newLine);

            for (int i = 3; i < 5; i++)
            {
                // MC Simulation Params
                int      NumberOfTrials = (int)Math.Pow(10, i);
                double[] factor         = new double[3] {
                    0.5, 1, 2
                };
                for (int j = 0; j < 3; j++)
                {
                    int NumberOfTimeSteps = (int)Math.Ceiling(factor[j] * 365 * Maturity);
                    MonteCarloSettings monteCarloSettings =
                        new MonteCarloSettings(NumberOfTrials, NumberOfTimeSteps);

                    double price = Heston.HestonEuropeanOptionPriceMC(hestonModel, euOption, monteCarloSettings);
                    System.Console.WriteLine("K={0}, T={1}, #Trials={2}, #TimeSteps={3}, rel_error={4}", StrikePrice,
                                             Maturity, NumberOfTrials, NumberOfTimeSteps, Math.Abs(priceForm - price) / priceForm);
                    newLine = string.Format("{0}, {1}, {2}", NumberOfTrials, NumberOfTimeSteps, Math.Abs(priceForm - price) / priceForm);
                    csv.AppendLine(newLine);
                }
            }

            //Write to csv
            File.WriteAllText(@"./task4.csv", csv.ToString());

            System.Console.WriteLine("*********************");
            System.Console.WriteLine("\n\n");
        }
예제 #3
0
        public static void Task3()
        {
            // Variance Process Values
            Console.WriteLine("Task 3");
            // Variance Process Values
            double Kappa = 2.0;
            double Theta = 0.06;
            double Sigma = 0.4;
            double V0    = 0.04;
            double Rho   = 0.5;

            // Heston Model Params
            double InitialStockPrice = 100;
            double RiskFreeRate      = 0.1;

            // Option Params
            double     StrikePrice = 100;
            PayoffType Type        = PayoffType.Call;

            double[] Maturity = new double[5] {
                1, 2, 3, 4, 15
            };

            VarianceProcessParameters varParams =
                new VarianceProcessParameters(Kappa, Theta, Sigma, V0, Rho);

            HestonModelParameters hestonModel =
                new HestonModelParameters(InitialStockPrice, RiskFreeRate, varParams);

            // ************Task 3 Print****************
            System.Console.WriteLine("*********************");
            HestonModelParamPrint(varParams, hestonModel);

            //Prepare csv
            var    csv     = new StringBuilder();
            String newLine = string.Format("K, T, Price, refPrice");

            csv.AppendLine(newLine);

            for (int i = 0; i < 5; i++)
            {
                // MC Simulation Params
                int NumberOfTrials    = (int)1e5;
                int NumberOfTimeSteps = (int)Math.Ceiling(365 * Maturity[i]);

                EuropeanOption euOption =
                    new EuropeanOption(StrikePrice, Type, Maturity[i]);

                MonteCarloSettings monteCarloSettings =
                    new MonteCarloSettings(NumberOfTrials, NumberOfTimeSteps);
                double priceForm = Heston.HestonEuropeanOptionPrice(hestonModel, euOption);

                double price = Heston.HestonEuropeanOptionPriceMC(hestonModel, euOption, monteCarloSettings);
                System.Console.WriteLine("K={0}, T={1}, C_MC={2}, C_form={3}", StrikePrice,
                                         Maturity[i], price, priceForm);

                newLine = string.Format("{0}, {1}, {2}, {3}", StrikePrice, Maturity[i], price, priceForm);
                csv.AppendLine(newLine);
            }

            //Write to csv
            File.WriteAllText(@"./task3.csv", csv.ToString());

            System.Console.WriteLine("*********************");
            System.Console.WriteLine("\n\n");
        }