📄 rq_price.c
字号:
random_factors, ud.terminal_vals, rq_random_normal, &ud, mc_init, mc_path_init, mc_heston_timestep, mc_euro_payoff, mc_free, &sim_results ); *result = sim_results.mean * exp(-ud.r * tau_opt_delivery); RQ_FREE(ud.terminal_vals); RQ_FREE(ud.timestep_vals); return (err == 0);}/* General code*/static shortvalidate_model(const char *modelid){ int i; for (i = 0; i < sizeof(model_defs) / sizeof(struct model_def); i++) if (!strcmp(model_defs[i].name, modelid)) return i; return -1;}static shortvalidate_model_params(int model_offset){ short err = 0; int i; for (i = 0; model_defs[model_offset].params_required[i] != '\0'; i++) { switch (model_defs[model_offset].params_required[i]) { case 'O': if (strlen(option_type) == 0) { printf("The option-type must be specified\n"); err++; } break; case 'S': if (underlying_price == 0.0) { printf("The underlying-price option must be specified\n"); err++; } break; case 'K': if (strike_price == 0.0) { printf("The strike-price option must be specified\n"); err++; } break; case 'B': if (barrier_price == 0.0) { printf("The barrier-price option must be specified\n"); err++; } break; case 'U': if (upper_barrier == 0.0) { printf("The upper-barrier option must be specified\n"); err++; } break; case 'L': if (lower_barrier == 0.0) { printf("The lower-barrier option must be specified\n"); err++; } break; case 'A': if (underlying_avg == 0.0) { printf("The average-price option must be specified\n"); err++; } break; case 'I': if (int_rate_dom == 0.0) { printf("The int-rate-dom must be specified\n"); err++; } break;/* We'll let the foreign interest rate be zero. case 'J': if (int_rate_for == 0.0) { printf("The int-rate-for must be specified\n"); err++; } break;*/ case 'V': if (volatility == 0.0) { printf("The volatility must be specified\n"); err++; } break; case 'X': if (tau_opt_expiry == 0.0) { printf("The tau-opt-expiry option must be specified\n"); err++; } break; case 'D': if (tau_opt_delivery == 0.0) { printf("The tau-opt-delivery option must be specified\n"); err++; } break; case 'Z': if (tau_opt_lifespan == 0.0) { printf("The tau-opt-lifespan option must be specified\n"); err++; } break; case 'Y': if (tau_period == 0.0) { printf("The tau-period option must be specified\n"); err++; } break; case 'M': if (num_timesteps <= 0) { printf("The num-timesteps option must be specified\n"); err++; } break; case 'N': if (num_iterations <= 0) { printf("The num-iterations option must be specified\n"); err++; } break; case 'G': if (grid_size <= 0) { printf("The grid-size option must be specified\n"); err++; } break; } } return err;}intmain(int argc, char **argv){ unsigned curpos = 1; /* start off from the option AFTER the program name */ char name[256]; char value[256]; int ret; int model_offset; double optval = 0; int precision = DEFAULT_PRECISION; model[0] = option_type[0] = '\0'; while ((ret = bh_getopt(argc, argv, &curpos, optdefs, sizeof(optdefs)/sizeof(struct bh_optdef), name, 256, value, 256)) != BH_GETOPT_RESULT_DONE) { if (ret < 0 || !strcmp(name, "help")) { bh_getopt_help(optdefs, sizeof(optdefs)/sizeof(struct bh_optdef)); exit(0); } else if (!strcmp(name, "version")) { printf("This is rq_price based on Risk Quantify version %s\n", VERSION); exit(0); } else if (!strcmp(name, "help-model")) { int model_offset = -1; if (value[0] == '\0' || (model_offset = validate_model(value)) < 0) { int i; printf("You can pass the following options to the model parameter\n"); for (i = 0; i < sizeof(model_defs) / sizeof(struct model_def); i++) { printf(" %s - %s\n", model_defs[i].name, model_defs[i].desc_short ); } exit(0); } else { if (model_defs[model_offset].desc_long) { int i; printf("%s\n", model_defs[model_offset].desc_long); printf("\nThis model requires the following parameters:\n"); for (i = 0; model_defs[model_offset].params_required[i] != '\0'; i++) { char optshort = model_defs[model_offset].params_required[i]; int j; int max_j = sizeof(optdefs) / sizeof(struct bh_optdef); for (j = 0; j < max_j; j++) { if (optdefs[j].optshort == optshort) { bh_getopt_help_option(&optdefs[j]); break; } } if (j == max_j) { printf("ERROR: option %c not found\n", optshort); } } } exit(0); } } else if (!strcmp(name, "model")) bh_strcpy(model, value, sizeof(model)); else if (!strcmp(name, "option-type")) bh_strcpy(option_type, value, sizeof(option_type)); else if (!strcmp(name, "underlying-price")) underlying_price = atof(value); else if (!strcmp(name, "strike-price")) strike_price = atof(value); else if (!strcmp(name, "barrier-price")) barrier_price = atof(value); else if (!strcmp(name, "upper-barrier")) upper_barrier = atof(value); else if (!strcmp(name, "lower-barrier")) lower_barrier = atof(value); else if (!strcmp(name, "average-price")) underlying_avg = atof(value); else if (!strcmp(name, "int-rate-dom")) { int_rate_dom = 0.0; if (tolower(*value) == 'd') { char *p = strchr(value, ':'); if (p) { int_rate_dom_df = atof(p+1); if (int_rate_dom_df > 0.0) do_int_rate_dom_df_conversion = 1; } } if (int_rate_dom == 0.0 && !do_int_rate_dom_df_conversion) int_rate_dom = atof(value); } else if (!strcmp(name, "int-rate-for")) { int_rate_for = 0.0; if (tolower(*value) == 'd') { char *p = strchr(value, ':'); if (p) { int_rate_for_df = atof(p+1); if (int_rate_for_df > 0.0) do_int_rate_for_df_conversion = 1; } } if (int_rate_for == 0.0 && !do_int_rate_for_df_conversion) int_rate_for = atof(value); } else if (!strcmp(name, "volatility")) volatility = atof(value); else if (!strcmp(name, "tau-opt-expiry")) tau_opt_expiry = atof(value); else if (!strcmp(name, "tau-opt-delivery")) tau_opt_delivery = atof(value); else if (!strcmp(name, "tau-opt-lifespan")) tau_opt_lifespan = atof(value); else if (!strcmp(name, "tau-period")) tau_period = atof(value); else if (!strcmp(name, "num-timesteps")) num_timesteps = atol(value); else if (!strcmp(name, "num-iterations")) num_iterations = atol(value); else if (!strcmp(name, "grid-size")) grid_size = atol(value); else if (!strcmp(name, "precision")) precision = atoi(value); } if (strlen(model) == 0) { printf("ERROR: A model identifier must be specified!\n"); exit(-1); } model_offset = validate_model(model); if (model_offset < 0) { printf("ERROR: An invalid model was specified!\n"); exit(-2); } if (do_int_rate_dom_df_conversion && tau_opt_delivery > 0.0) int_rate_dom = -log(int_rate_dom_df) / tau_opt_delivery; if (do_int_rate_for_df_conversion && tau_opt_delivery > 0.0) int_rate_for = -log(int_rate_for_df) / tau_opt_delivery; if (validate_model_params(model_offset) > 0) { printf("FATAL: Required parameters not specified\n"); exit(-3); } if (!(*model_defs[model_offset].fn)(&optval)) { printf("Error in valuation\n"); exit(-4); } printf("%.*f\n", precision, optval); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -