📄 rq_yc.c
字号:
rq_term_fill(&term, 0, 0, 3, 0); output_date(yce->date); printf("%s", output_delim); printf("%.12f", yce->discount_factor); printf("%s", output_delim); zero_rate = rq_yield_curve_get_simple_rate(yc, yce->date, day_count); printf("%.12f", zero_rate ); printf("%s", output_delim); num_dates = rq_date_roll_generate_dates( dates, 120, yc->from_date, yce->date, &term, RQ_ROLL_CONVENTION_NONE, RQ_DATE_ROLL_MOD_FOLLOWING, NULL, 0, RQ_DATE_ROLL_STUB_POSITION_NONE, 15 /* allow 15 days error on date creation */ ); if (num_dates > 1) { /* skip the first date. */ par_rate = rq_yield_curve_get_par_rate(yc, num_dates-1, dates+1, 4); } else par_rate = zero_rate; printf("%.12f", par_rate ); } else { rq_linked_list_iterator_t lli = rq_linked_list_begin(output_fields); while (!rq_linked_list_iterator_at_end(lli)) { const char *field = rq_linked_list_iterator_get_data(lli); if (!strcmp(field, "date")) output_date(yce->date); else if (!strcmp(field, "df")) printf("%.12f", yce->discount_factor); lli = rq_linked_list_iterator_incr(lli); if (!rq_linked_list_iterator_at_end(lli)) printf("%s", output_delim); } } printf("\n"); }}intmain(int argc, char **argv){ unsigned curpos = 1; /* start off from the option AFTER the program name */ char name[MAX_CMD_LINE_TOK_LEN]; char value[MAX_CMD_LINE_TOK_LEN]; int ret; rq_system_t system = rq_system_alloc(); rq_market_t market; rq_asset_mgr_t asset_mgr = rq_system_get_asset_mgr(system); rq_date market_date = rq_date_today(); rq_linked_list_t rate_list = rq_linked_list_alloc(rate_free); rq_linked_list_iterator_t rate_list_iter; rq_rate_mgr_t rate_mgr; rq_bootstrap_config_t bootstrap_config; rq_bootstrap_adapter_mgr_t bootstrap_adapter_mgr = rq_bootstrap_adapter_mgr_alloc(); enum rq_termstruct_type termstruct_type = RQ_TERMSTRUCT_TYPE_INVALID; const char *curve_id = NULL; int list_points = 0; void *curve_p; /**< The constructed term structure */ FILE *config_file = NULL; short calc_zero = 0; rq_date zero_date; RQ_INIT(market); /* initialize the market to NULL */ output_fields = rq_linked_list_alloc(free); /* initialize the output fields linked list */ /* Add the standard bootstrap adapters into the manager */ rq_bootstrap_adapter_mgr_add_standard_adapters(bootstrap_adapter_mgr); while ((ret = bh_getopt(argc, argv, &curpos, optdefs, sizeof(optdefs)/sizeof(struct bh_optdef), name, MAX_CMD_LINE_TOK_LEN, value, MAX_CMD_LINE_TOK_LEN)) != BH_GETOPT_RESULT_DONE) { if (ret < 0 || !strcmp(name, "help")) { bh_getopt_help(optdefs, sizeof(optdefs)/sizeof(struct bh_optdef)); goto exit1; } else if (!strcmp(name, "version")) { printf("This is rq_yc based on Risk Quantify version %s\n", VERSION); goto exit1; } else if (!strcmp(name, "delimiter")) { delims = strdup(value); free_delims = 1; } else if (!strcmp(name, "asset")) { parse_asset(asset_mgr, value); } else if (!strcmp(name, "list-assets")) { list_assets(asset_mgr); } else if (!strcmp(name, "market-date")) { rq_date new_date = rq_date_parse(value, RQ_DATE_FORMAT_YMD); if (!new_date) { printf("Warning: Couldn't parse the market date (should be in YYYY-MM-DD format)\n"); } else market_date = new_date; } else if (!strcmp(name, "rate")) { rq_rate_t rate = parse_rate(value); if (!RQ_IS_NULL(rate)) { rq_linked_list_append(rate_list, rate); } } else if (!strcmp(name, "bootstrap-config")) { parse_bootstrap_config( rq_system_get_bootstrap_config_mgr(system), value ); } else if (!strcmp(name, "curve")) { char curvebuf[4096]; char *ptr; strcpy(curvebuf, value); ptr = strchr(curvebuf, ':'); if (ptr) *ptr = '\0'; termstruct_type = rq_termstruct_type_from_string(curvebuf); if (!ptr || termstruct_type == RQ_TERMSTRUCT_TYPE_INVALID) { printf("Error: The curve specification must be 'CURVE_TYPE:CurveName' where CURVE_TYPE is one of\n"); printf(" YIELD_CURVE ---- Yield Curve\n"); printf(" FORWARD_CURVE -- Forward Curve\n"); printf(" VOL_SURFACE ---- Vol Surface\n"); goto exit1; } curve_id = strdup(ptr+1); } else if (!strcmp(name, "list-points")) { list_points = 1; } else if (!strcmp(name, "output-field")) { rq_linked_list_append(output_fields, strdup(value)); } else if (!strcmp(name, "config-file")) { config_file = fopen(value, "r"); if (!config_file) { printf("Error: Can't open file '%s'\n", value); } } else if (!strcmp(name, "calc-zero")) { rq_date new_date = rq_date_parse(value, RQ_DATE_FORMAT_YMD); if (!new_date) { printf("Warning: Couldn't parse the zero date (should be in YYYY-MM-DD format)\n"); } else { zero_date = new_date; calc_zero = 1; } } } if (config_file) { while (!feof(config_file)) { char buf[MAX_CMD_LINE_TOK_LEN]; if (fgets(buf, MAX_CMD_LINE_TOK_LEN, config_file)) { char *p = strchr(buf, '\r'); if (p) *p = '\0'; p = strchr(buf, '\n'); if (p) *p = '\0'; if (*buf != '#') { char *p = strchr(buf, ':'); if (p) { *p = '\0'; strcpy(name, buf); strcpy(value, p+1); if (!strcmp(name, "asset")) { parse_asset(asset_mgr, value); } else if (!strcmp(name, "market-date")) { rq_date new_date = rq_date_parse(value, RQ_DATE_FORMAT_YMD); if (!new_date) { printf("Warning: Couldn't parse the market date (should be in YYYY-MM-DD format)\n"); } else market_date = new_date; } else if (!strcmp(name, "rate")) { rq_rate_t rate = parse_rate(value); if (!RQ_IS_NULL(rate)) { rq_linked_list_append(rate_list, rate); } } else if (!strcmp(name, "bootstrap-config")) { parse_bootstrap_config( rq_system_get_bootstrap_config_mgr(system), value ); } } } } } fclose(config_file); } if (!curve_id) { printf("Error: Must specify the curve to build\n"); goto exit1; } /* some validation of the arguments */ if (termstruct_type != RQ_TERMSTRUCT_TYPE_YIELD_CURVE && termstruct_type != RQ_TERMSTRUCT_TYPE_FORWARD_CURVE && termstruct_type != RQ_TERMSTRUCT_TYPE_VOL_SURFACE) { printf("Error: The term structure must currently be a yield curve/forward curve/vol surface\n"); goto exit1; } /* construct the market */ market = rq_market_alloc(market_date); rate_mgr = rq_market_get_rate_mgr(market); /* add the rates into the market object. We do a rate clone here because the rate list is going to delete the rate that it manages. */ rate_list_iter = rq_linked_list_begin(rate_list); while (!rq_linked_list_iterator_at_end(rate_list_iter)) { rq_rate_t rate = (rq_rate_t)rq_linked_list_iterator_get_data(rate_list_iter); /* find the asset in the asset_mgr. */ rq_asset_t asset = rq_asset_mgr_get( asset_mgr, rq_rate_get_asset_id(rate) ); if (asset == NULL) { printf("Error: Couldn't find the asset '%s'\n", rq_rate_get_asset_id(rate)); goto exit1; } rq_rate_set_value_date( rate, rq_asset_get_value_date(asset, market_date) ); rq_rate_mgr_add(rate_mgr, rq_rate_clone(rate)); rate_list_iter = rq_linked_list_iterator_incr(rate_list_iter); } /* now bootstrap the curve */ bootstrap_config = rq_bootstrap_config_mgr_find( rq_system_get_bootstrap_config_mgr(system), curve_id, termstruct_type ); if (!bootstrap_config) { printf("Couldn't find the bootstrap configuration for curve '%s'\n", curve_id); goto exit1; } curve_p = rq_bootstrap_adapter_mgr_build( bootstrap_adapter_mgr, termstruct_type, rq_bootstrap_config_get_bootstrap_method_id(bootstrap_config), curve_id, system, market ); if (!curve_p) { printf("Warning: Couldn't build the term structure\n"); } else { /* term structure built */ if (termstruct_type == RQ_TERMSTRUCT_TYPE_YIELD_CURVE) { rq_yield_curve_t yc = (rq_yield_curve_t)curve_p; //rq_yield_curve_set_curve_start_extrapolation_method(yc, RQ_YIELD_CURVE_EXTRAPOLATION_USE_LAST_ZERO); rq_yield_curve_set_interpolation_method(yc, RQ_YIELD_CURVE_INTERPOLATION_LINEAR_ZERO); /* output the yield curve? */ if (list_points) list_yield_curve(system, yc); if (calc_zero) { rq_asset_t asset_ccy = rq_asset_mgr_get(rq_system_get_asset_mgr(system), rq_yield_curve_get_underlying_asset_id(yc)); enum rq_day_count_convention day_count = RQ_DAY_COUNT_ACTUAL_365; if (rq_asset_ccy_get_days_per_year(asset_ccy) == 360) day_count = RQ_DAY_COUNT_ACTUAL_360; printf( "%.10f\n", rq_yield_curve_get_simple_rate(yc, zero_date, day_count) ); } } else if (termstruct_type == RQ_TERMSTRUCT_TYPE_FORWARD_CURVE) { rq_forward_curve_t fc = (rq_forward_curve_t)curve_p; } else if (termstruct_type == RQ_TERMSTRUCT_TYPE_VOL_SURFACE) { rq_vol_surface_t vs = (rq_vol_surface_t)curve_p; } else { assert(0); } } exit1: if (free_delims) free(delims); rq_bootstrap_adapter_mgr_free(bootstrap_adapter_mgr); rq_system_free(system); if (!RQ_IS_NULL(market)) rq_market_free(market); rq_linked_list_free(output_fields); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -