⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rq_yc.c

📁 风险财务控制库 Risk Quantify is an open source financial library, with a focus on managing the risk of fi
💻 C
📖 第 1 页 / 共 2 页
字号:
/*** rq_yc.c**** Written by Brett Hutley - brett@hutley.net**** Copyright (C) 2004,2005 Brett Hutley**** This file is part of the Risk Quantify Library**** Risk Quantify is free software; you can redistribute it and/or** modify it under the terms of the GNU Library General Public** License as published by the Free Software Foundation; either** version 2 of the License, or (at your option) any later version.**** Risk Quantify is distributed in the hope that it will be useful,** but WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU** Library General Public License for more details.**** You should have received a copy of the GNU Library General Public** License along with Risk Quantify; if not, write to the Free** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA*//* -- includes ---------------------------------------------------- */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <rq.h>#include <assert.h>#include "bh_getopt.h"#include "bh_strcpy.h"/* -- prototypes -------------------------------------------------- */static rq_asset_t build_asset_ccy(int num_args, const char **args);static rq_asset_t build_asset_irdiscount(int num_args, const char **args);static rq_asset_t build_asset_irswap(int num_args, const char **args);/* -- defines ----------------------------------------------------- */#define MAX_CMD_LINE_TOK_LEN 4096/* -- structures -------------------------------------------------- *//* Command line options */struct bh_optdef optdefs[] = {	{ "help", 'h', BH_GETOPT_ARGTYPE_NONE, NULL, "Display help" },	{ "version", 'v', BH_GETOPT_ARGTYPE_NONE, NULL, "Display the version" },    { "delimiter", 'd', BH_GETOPT_ARGTYPE_REQUIRED, "delim", "The delimter between fields in the definitions (default is ',')" },    { "asset", 'a', BH_GETOPT_ARGTYPE_REQUIRED, "assetdef", "An asset definition" },    { "rate", 'r', BH_GETOPT_ARGTYPE_REQUIRED, "ratedef", "A rate definition" },    { "bootstrap-config", 'b', BH_GETOPT_ARGTYPE_REQUIRED, "bootstrapdef", "A bootstrap configuration" },    { "config-file", 'c', BH_GETOPT_ARGTYPE_REQUIRED, "config-file", "The config file for the bootstrapper" },    { "calc-zero", 'Z', BH_GETOPT_ARGTYPE_REQUIRED, "date", "Calculate a zero rate for a particular date" },    { "curve", 'C', BH_GETOPT_ARGTYPE_REQUIRED, "curve_id" "The id of the curve to build" },    { "list-assets", 'A', BH_GETOPT_ARGTYPE_NONE, NULL, "List the assets currently in the asset manager" },    { "market-date", 'D', BH_GETOPT_ARGTYPE_REQUIRED, "YYYY-MM-DD", "Set the market date (defaults to today)" },    { "list-points", 'L', BH_GETOPT_ARGTYPE_NONE, NULL, "List the term structure points" },    { "output-field", 'O', BH_GETOPT_ARGTYPE_REQUIRED, NULL, "Output the specified field" }};struct asset_builder {    const char *type_id;    rq_asset_t (*build_func)(int num_args, const char **args);} asset_builders[] = {    { "Ccy", build_asset_ccy },    { "Discount", build_asset_irdiscount },    { "Swap", build_asset_irswap }};/* -- statics ----------------------------------------------------- */static char *delims = ",";static int free_delims = 0;static rq_linked_list_t output_fields;static const char *output_delim = "\t";static const char *date_format = "yyyy-mm-dd";/* -- code -------------------------------------------------------- */static rq_asset_tbuild_asset_ccy(int num_args, const char **args){    if (num_args != 3)    {        printf("Wrong number of arguments when trying to build a currency.\n");        return NULL;    }    return rq_asset_ccy_build(args[0], args[1], atoi(args[2]));}static rq_asset_tbuild_asset_irdiscount(int num_args, const char **args){    struct rq_term term;    if (num_args != 4)    {        printf("Wrong number of arguments when trying to build a discount asset.\n");        return NULL;    }    rq_term_parse(&term, args[2], "/");    return rq_asset_irdiscount_build(        args[0],         args[1],         &term,        rq_day_count_convention_from_string(args[3])        );}static rq_asset_tbuild_asset_irswap(int num_args, const char **args){    struct rq_term tenor;    struct rq_term freq;    if (num_args != 6)    {        printf("Wrong number of arguments when trying to build a swap asset.\n");        return NULL;    }    rq_term_parse(&tenor, args[2], "/");    rq_term_parse(&freq, args[3], "/");    return rq_asset_irswap_build(        args[0], /* asset id */        args[1], /* ccy code */        &tenor,        &freq,        rq_day_count_convention_from_string(args[4]),        rq_date_roll_convention_from_string(args[5])        );}static voidlist_assets(rq_asset_mgr_t asset_mgr){    rq_string_list_t asset_type_list = rq_asset_mgr_get_asset_type_list(asset_mgr);    unsigned i;    for (i = 0; i < rq_string_list_size(asset_type_list); i++)    {        rq_asset_list_t asset_list;        unsigned int a_i;        const char *asset_type = rq_string_list_get_at(            asset_type_list,            i            );        printf("%*.*s\n", strlen(asset_type), strlen(asset_type), "----------");        printf("%s\n", asset_type);        printf("%*.*s\n", strlen(asset_type), strlen(asset_type), "==========");        asset_list = rq_asset_mgr_list_assets_for_type(asset_mgr, asset_type);        for (a_i = 0; a_i < rq_asset_list_size(asset_list); a_i++)        {            rq_asset_t asset = rq_asset_list_get_at(asset_list, a_i);            printf("%s\n", rq_asset_get_asset_id(asset));        }        rq_asset_list_free(asset_list);    }    rq_string_list_free(asset_type_list);}static intparse_asset(rq_asset_mgr_t asset_mgr, const char *record){    char tok[MAX_CMD_LINE_TOK_LEN];    const char *ptr = record;    int tok_i;    const char *arguments[20]; /* can't have more than 20 arguments */    for (tok_i = 0; tok_i < 20 && rq_tokenizer_get_token(&ptr, tok, MAX_CMD_LINE_TOK_LEN, delims, "", ""); tok_i++)    {        arguments[tok_i] = strdup(tok);    }    /* tok_i now has the number of arguments      */    /* find the builder */    if (tok_i > 0)    {        int builder_i;        int i;        for (builder_i = 0;              builder_i < sizeof(asset_builders) / sizeof(struct asset_builder);             builder_i++)        {            if (!strcmp(arguments[0], asset_builders[builder_i].type_id))            {                /* OK, we've found our builder */                rq_asset_t asset = (*asset_builders[builder_i].build_func)(tok_i-1, arguments+1);                if (!RQ_IS_NULL(asset))                {                    rq_asset_mgr_add(asset_mgr, asset);                }                break;            }        }        if (builder_i == sizeof(asset_builders) / sizeof(struct asset_builder))        {            printf(                "Warning: Couldn't find a builder for asset type: %s\n",                 arguments[0]                );        }        /* free the arguments */        for (i = 0; i < tok_i; i++)            free((char *)arguments[i]);    }	return 0;}static voidrate_free(void *p){    rq_rate_free((rq_rate_t)p);}static rq_rate_tparse_rate(const char *record){    rq_rate_t rate;    char tok[MAX_CMD_LINE_TOK_LEN];    const char *ptr = record;    int tok_i;    const char *rate_class_id = NULL;    const char *asset_id = NULL;    rq_date observation_date = 0;    double value = 0.0;    RQ_INIT(rate);    for (tok_i = 0; rq_tokenizer_get_token(&ptr, tok, MAX_CMD_LINE_TOK_LEN, delims, "", ""); tok_i++)    {        switch (tok_i)        {            case 0:                rate_class_id = strdup(tok);                asset_id = strdup(tok);                break;            case 1:                observation_date = rq_date_parse(tok, RQ_DATE_FORMAT_YMD);                break;            case 2:                value = atof(tok);                break;                            default:                break;        }    }    if (rate_class_id && asset_id && observation_date)    {        rate = rq_rate_build(            rate_class_id,            asset_id,            RQ_RATE_TYPE_SIMPLE, /* will be removing this field. */            observation_date,            0, /* filled out later */            value            );    }    if (rate_class_id)        free((char *)rate_class_id);    if (asset_id)        free((char *)asset_id);    return rate;}static voidparse_bootstrap_config(    rq_bootstrap_config_mgr_t bootstrap_config_mgr,    const char *record    ){    rq_bootstrap_config_t bootstrap_config;    char tok[MAX_CMD_LINE_TOK_LEN];    const char *ptr = record;    int tok_i;    const char *curve_id = NULL;    const char *asset_id = NULL;    RQ_INIT(bootstrap_config);    for (tok_i = 0; rq_tokenizer_get_token(&ptr, tok, MAX_CMD_LINE_TOK_LEN, delims, "", ""); tok_i++)    {        switch (tok_i)        {            case 0:                curve_id = strdup(tok);                break;            case 1:                asset_id = strdup(tok);                break;            case 2:                bootstrap_config = rq_bootstrap_config_build(                    curve_id,                    asset_id,                    rq_termstruct_type_from_string(tok)                    );                break;			case 3:				rq_bootstrap_config_set_bootstrap_method_id(bootstrap_config, tok);				break;			case 4:                if (strlen(tok) > 0)                    rq_bootstrap_config_set_curve_id1(bootstrap_config, tok);				break;			case 5:                if (strlen(tok) > 0)                    rq_bootstrap_config_set_curve_id2(bootstrap_config, tok);				break;            default:                rq_bootstrap_config_add_rate_class_id(bootstrap_config, tok);                break;        }    }    if (!RQ_IS_NULL(bootstrap_config))        rq_bootstrap_config_mgr_add(            bootstrap_config_mgr,            bootstrap_config            );    else    {        printf("Warning: Bootstrap config not created\n");    }    if (curve_id)        free((char *)curve_id);    if (asset_id)        free((char *)asset_id);}static voidoutput_date(rq_date d){    char buf[40];    printf("%s", rq_date_to_string(buf, date_format, d));}static voidlist_yield_curve(rq_system_t system, rq_yield_curve_t yc){    unsigned i;    for (i = 0; i < rq_yield_curve_size(yc); i++)    {        struct rq_yield_curve_elem *yce = rq_yield_curve_element_at(yc, i);        if (rq_linked_list_size(output_fields) == 0)        {            rq_date dates[1024];            unsigned num_dates;            struct rq_term term;            double zero_rate;            double par_rate;            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;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -