Thursday, 2 May 2013

boost::property_tree

property tree can be used for configuration.

example:

Configuration file:

/**
 * <config>
 *    <date begin="20030110" end="20050505" backtest_period="50" rebalance_period="5"/>
 *    <portfolio>
 *        <symbol>IBM</symbol>
 *        <symbol>MSFT</symbol>
 *        <symbol>APPL</symbol>
 *    </portfolio>
 *    <benchmark method="SP500">
 *    </benchmark>
 * </config>
 */

Source code:

        pt::ptree ptConfig;
        ifstream fstream(config.c_str());
        pt::xml_parser::read_xml(fstream, ptConfig);

        // get begin and end
        m_sBeginDate = ptConfig.get<string>("config.date.<xmlattr>.begin");
        m_sEndDate = ptConfig.get<string>("config.date.<xmlattr>.end");
       
        m_sBacktestPeriod = ptConfig.get<string>("config.date.<xmlattr>.backtest_period");
        m_sRebalancePeriod = ptConfig.get<string>("config.date.<xmlattr>.rebalance_period");

        // get portfolio symbol
        unsigned int assetIdx = 0;

        BOOST_FOREACH(pt::ptree::value_type &v,
            ptConfig.get_child("config.portfolio"))
        {
            symbols.insert(make_pair(v.second.data(), assetIdx));
            assetIdx++;
        }

        m_benchMethod = ptConfig.get<string>("config.benchmark.<xmlattr>.method");

No comments:

Post a Comment