📄 rq_price.c
字号:
return 0; } if (option_type[1] == 'C' || option_type[1] == 'c') is_call = 1; else if (option_type[1] == 'P' || option_type[1] == 'p') is_call = 0; else { printf("Error: Unknown option-type parameter - must be one of:\n"); printf(" 'IC' - In Call\n"); printf(" 'IP' - In Put\n"); printf(" 'OC' - Out Call\n"); printf(" 'OP' - Out Put\n"); return 0; } *result = rq_pricing_double_barrier( in, is_call, underlying_price, strike_price, lower_barrier, upper_barrier, int_rate_dom, int_rate_for, volatility, tau_opt_expiry, tau_opt_delivery ); return 1;}short average_rate_geometric(double *result){ short is_call; if (*option_type == 'C' || *option_type == 'c') is_call = 1; else if (*option_type == 'P' || *option_type == 'p') is_call = 0; else { printf("Error: Unknown option-type parameter - must be call or put\n"); return 0; } *result = rq_pricing_average_rate_geometric( is_call, underlying_price, underlying_avg, strike_price, int_rate_dom, int_rate_for, volatility, tau_opt_lifespan, tau_opt_expiry ); return 1;}short average_rate_turnbullwakeman(double *result){ short is_call; if (*option_type == 'C' || *option_type == 'c') is_call = 1; else if (*option_type == 'P' || *option_type == 'p') is_call = 0; else { printf("Error: Unknown option-type parameter - must be call or put\n"); return 0; } *result = rq_pricing_average_rate_turnbullwakeman( is_call, underlying_price, underlying_avg, strike_price, int_rate_dom, int_rate_for, volatility, tau_opt_lifespan, tau_opt_expiry, tau_period ); return 1;}short average_rate_levy(double *result){ short is_call; if (*option_type == 'C' || *option_type == 'c') is_call = 1; else if (*option_type == 'P' || *option_type == 'p') is_call = 0; else { printf("Error: Unknown option-type parameter - must be call or put\n"); return 0; } *result = rq_pricing_average_rate_levy( is_call, underlying_price, underlying_avg, strike_price, int_rate_dom, int_rate_for, volatility, tau_opt_lifespan, tau_opt_expiry ); return 1;}static short barone_adesi_whaley(double *result){ short is_call; if (*option_type == 'C' || *option_type == 'c') is_call = 1; else if (*option_type == 'P' || *option_type == 'p') is_call = 0; else { printf("Error: Unknown option-type parameter - must be call or put\n"); return 0; } *result = rq_pricing_barone_adesi_whaley( is_call, underlying_price, strike_price, int_rate_dom, int_rate_for, volatility, tau_opt_expiry ); return 1;}static short bjerksund_stensland(double *result){ short is_call; if (*option_type == 'C' || *option_type == 'c') is_call = 1; else if (*option_type == 'P' || *option_type == 'p') is_call = 0; else { printf("Error: Unknown option-type parameter - must be call or put\n"); return 0; } *result = rq_pricing_bjerksund_stensland( is_call, underlying_price, strike_price, int_rate_dom, int_rate_for, volatility, tau_opt_expiry ); return 1;}static short findiff_equity(double *result){ short is_call; double *mesh; double *value; double *vold; double *ivalue; double *a; double *b; double *c; double *tvalue; int err; if (*option_type == 'C' || *option_type == 'c') is_call = 1; else if (*option_type == 'P' || *option_type == 'p') is_call = 0; else { printf("Error: Unknown option-type parameter - must be call or put\n"); return 0; } mesh = (double *)RQ_MALLOC(sizeof(double) * (num_iterations+1)); value = (double *)RQ_MALLOC(sizeof(double) * (num_iterations+1)); vold = (double *)RQ_MALLOC(sizeof(double) * (num_iterations+1)); ivalue = (double *)RQ_MALLOC(sizeof(double) * (num_iterations+1)); a = (double *)RQ_MALLOC(sizeof(double) * (num_iterations+1)); b = (double *)RQ_MALLOC(sizeof(double) * (num_iterations+1)); c = (double *)RQ_MALLOC(sizeof(double) * (num_iterations+1)); tvalue = (double *)RQ_MALLOC(sizeof(double) * (num_timesteps+1)); *result = rq_pricing_finite_differences_equity_american( is_call, underlying_price, strike_price, int_rate_dom, int_rate_for, volatility, tau_opt_expiry, tau_opt_delivery, num_timesteps, num_iterations, mesh, value, vold, ivalue, a, b, c, tvalue, &err ); RQ_FREE(mesh); RQ_FREE(value); RQ_FREE(vold); RQ_FREE(ivalue); RQ_FREE(a); RQ_FREE(b); RQ_FREE(c); RQ_FREE(tvalue); return (err == 0);}struct mc_user_defined { short is_call; double *timestep_vals; double *terminal_vals; double S; double K; double r; double v; double dt;};static void mc_init(void *user_defined){ struct mc_user_defined *ud = (struct mc_user_defined *)user_defined;}static void mc_path_init(void *user_defined, unsigned long path, double *values){ struct mc_user_defined *ud = (struct mc_user_defined *)user_defined; values[0] = ud->S; values[1] = ud->v;}static double mc_heston_timestep(void *user_defined, unsigned long step, double *values, double *factors, double *timestep_vals){ struct mc_user_defined *ud = (struct mc_user_defined *)user_defined; double S = values[0]; double v = values[1]; S += (ud->r * S * ud->dt + v * S * factors[0]); /* printf("%f\n", S); */ values[0] = S; values[1] = v; return S;}static double mc_euro_payoff(void *user_defined, unsigned long path, double *values, double *timestep_vals, unsigned long num_timesteps){ struct mc_user_defined *ud = (struct mc_user_defined *)user_defined; double S = values[0]; double payoff; if (ud->is_call) payoff = S - ud->K; else payoff = ud->K - S; if (payoff > 0) return payoff; return 0;}void mc_free(void *user_defined){}static short monte_carlo_european_heston(double *result){ struct mc_user_defined ud; struct rq_simulation_results sim_results; double values[2]; double random_factors[2]; short err; rq_matrix_t correl_matrix = rq_matrix_build_with_values( 2, 2, 1.0, 0.0, 0.0, 1.0 ); if (*option_type == 'C' || *option_type == 'c') ud.is_call = 1; else if (*option_type == 'P' || *option_type == 'p') ud.is_call = 0; else { printf("Error: Unknown option-type parameter - must be call or put\n"); return 0; } ud.terminal_vals = (double *)RQ_CALLOC(sizeof(double), (num_iterations+1)); ud.timestep_vals = (double *)RQ_CALLOC(sizeof(double), (num_timesteps+1)); ud.S = underlying_price; ud.K = strike_price; ud.r = int_rate_dom; ud.v = volatility; ud.dt = rq_pricing_monte_carlo_multi_factor_get_dt( tau_opt_delivery, num_timesteps ); err = rq_pricing_monte_carlo_multi_factor( values, tau_opt_delivery, correl_matrix, num_iterations, num_timesteps, ud.timestep_vals,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -