public static object HestonAsianOptionPriceMC(double underlying, double riskFreeRate, double kappa, double theta, double sigma, double rho, double v0, double maturity, double strike, object monitoringTimes, 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.HestonAsianOptionPriceMC( new HestonParametersGrading(underlying, riskFreeRate, kappa, theta, sigma, rho, v0), new AsianOptionGrading(ConvertToVector <double>(monitoringTimes), 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("HestonAsianOptionPriceMC: unknown error: " + e.Message); } }
public static void Task7() { // Variance Process Values Console.WriteLine("Task 7"); // 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[3] { 1, 2, 3 }; 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); // Monitoring Times List <double> monTimes1 = new List <double> { 0.75, 1.00 }; List <double> monTimes2 = new List <double> { 0.25, 0.5, 0.75, 1.00, 1.25, 1.5, 1.75 }; List <double> monTimes3 = new List <double> { 1.00, 2.00, 3.00 }; List <List <double> > MonitoringTimes = new List <List <double> > { monTimes1, monTimes2, monTimes3 }; //Prepare csv var csv = new StringBuilder(); String newLine = string.Format("K, T, Price"); csv.AppendLine(newLine); for (int i = 0; i < 3; i++) { // MC Simulation Params int NumberOfTrials = (int)1e5; int NumberOfTimeSteps = (int)Math.Ceiling(365 * Maturity[i]); AsianOption asianOption = new AsianOption(StrikePrice, Type, Maturity[i], MonitoringTimes[i]); MonteCarloSettings monteCarloSettings = new MonteCarloSettings(NumberOfTrials, NumberOfTimeSteps); double price = Heston.HestonAsianOptionPriceMC(hestonModel, asianOption, monteCarloSettings); System.Console.WriteLine("K={0}, T={1}, C_MC={2}", StrikePrice, Maturity[i], price); newLine = string.Format("{0}, {1}, {2}", StrikePrice, Maturity[i], price); csv.AppendLine(newLine); } //Write to csv File.WriteAllText(@"./task7.csv", csv.ToString()); System.Console.WriteLine("*********************"); System.Console.WriteLine("\n\n"); }