public static object HestonLookbackOptionPriceMC(double underlying, double riskFreeRate, double kappa, double theta, double sigma, double rho, double v0, double maturity, 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.HestonLookbackOptionPriceMC( new HestonParametersGrading(underlying, riskFreeRate, kappa, theta, sigma, rho, v0), new OptionGrading(maturity), 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("HestonAsianOptionPriceMC: unknown error: " + e.Message); } }
public static void Task8() { // Variance Process Values Console.WriteLine("Task 8"); // 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[] Maturity = new double[5] { 1, 3, 5, 7, 9 }; VarianceProcessParameters varParams = new VarianceProcessParameters(Kappa, Theta, Sigma, V0, Rho); HestonModelParameters hestonModel = new HestonModelParameters(InitialStockPrice, RiskFreeRate, varParams); // ************Task 7 Print**************** System.Console.WriteLine("*********************"); HestonModelParamPrint(varParams, hestonModel); //Prepare csv var csv = new StringBuilder(); String newLine = string.Format("T, Price"); 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]); MonteCarloSettings monteCarloSettings = new MonteCarloSettings(NumberOfTrials, NumberOfTimeSteps); Option maturity = new Option(Maturity[i]); double price = Heston.HestonLookbackOptionPriceMC(hestonModel, maturity, monteCarloSettings); System.Console.WriteLine("T={0}, C_MC={1}", Maturity[i], price); newLine = string.Format("{0}, {1}", Maturity[i], price); csv.AppendLine(newLine); } //Write to csv File.WriteAllText(@"./task8.csv", csv.ToString()); System.Console.WriteLine("*********************"); System.Console.WriteLine("\n\n"); }