Time series & QuantLib

We wrote an example code implementing the TimeSeries class of the QuantLib library.

This code actually parses a *.csv file that contains a time downloaded from Yahoo! Finance. Each line in the file contains a date and the corresponding open, high, low, close, volume and adjusted close.

Here, the QuantLib::TimeSeries object is created using the adjusted close, but the code is easy to adapt for other needs.

Below is the code, the results outputted to the console, and the source data file.

Copyright (C) 2013, Edouard 'tagoma' Tallent
QL timeseries wrapper 
QuantCorner @ https://quantcorner.wordpress.com


QuantLib::TimeSeries<double> PriceSeries(char* filename)
    // Read the file provided via command line
    std::ifstream in (filename);
    std::string line;
    std::vector lines;
    while (in >> line)

    // Container tools
    std::vector dates;
    std::vector quotes;

    for (unsigned int i = 0; i < lines.size(); i++)
            std::vector outerArray;

            boost::split(outerArray, lines[i], boost::is_any_of(","));

            std::vector innerArray;
            boost::split(innerArray, outerArray[0], boost::is_any_of("-")); 

            QuantLib::Year year = (QuantLib::Year) std::stoi(innerArray[0]);
            QuantLib::Month month = (QuantLib::Month) std::stoi(innerArray[1]);
            QuantLib::Day day = (QuantLib::Day) std::stoi(innerArray[2]);

            dates.push_back(QuantLib::Date(day, month, year));

    // Create a QuantLib::TimeSeries object
    QuantLib::TimeSeries series(dates.begin(), dates.end(), quotes.begin());

    // Return the time series
    return series;

int main(int argc, char *argv[])
    // Source file
    char* filename = argv[1];

    // Call to the function
    QuantLib::TimeSeries mySeries = PriceSeries(filename);

    // Below are implementations of some methods of QL Timeseries//

    // Is the time series empty?
    std::cout << "Is the series empty? (0 = not empty)\t" << mySeries.empty() << std::endl;

    // Start date of the time series
    std::cout << "Start date of the time series:\t" << mySeries.firstDate() << std::endl;

    // Last date of the time series
    std::cout << "Last date of the time series:\t" << mySeries.lastDate() << std::endl;

    // What was the Adj.close value on November 14th, 2012?
    std::cout << "Adjusted close on November 14th, 2012:\t" << mySeries[QuantLib::Date(14, QuantLib::Nov, 2012)] << std::endl;

    return 0;


Many thanks to the guys at Wilmott for their tips, and to Daniel Duffy (one of the authors of Introduction to the Boost C++ Libraries) for answering all my questions.


3 pensamientos en “Time series & QuantLib

  1. Mauricio Osorio

    Bonjour Monsieur Édouard

    I’m a mac user and when I check the date format in the csv file, it’s different from the one you show in the TOT_Quotes.csv figures. The format that I identify is: dd-mmm-yy (12-Jan-14). There is no way to cast from string “Jan” to Month 6.
    I just have to make minor changes to your code and it also work (from Xcode):

    Best regards

    Mauricio Bedoya


    using namespace std;
    using namespace QuantLib;
    using namespace boost::assign;

    Date DateFormatter(const string& date, const string& delimiter, const Integer& Day_Position, const Integer& Month_Position, const Integer& Year_Position)
    Regular expression: from 20-Jun-14
    to a vector containing 20,Jun,14
    const boost::regex delim(delimiter);
    boost::sregex_token_iterator token(date.begin(), date.end(), delim, -1);
    boost::sregex_token_iterator end;

    std::vector D;

    QL_REQUIRE(D.size()==3, “Error: Insufficient Data”);

    Construction of a boost::gregorian::date
    object and identify: day, month, year
    string newDate = D[Month_Position]+ “-” + D[Day_Position] + “-” + “20” + D[Year_Position];

    boost::gregorian::date boost_date_format = boost::gregorian::from_us_string(newDate);
    int d = boost_date_format.day();
    int m = boost_date_format.month();
    int y = boost_date_format.year();

    Construction of a QuantLib::Date

    return Date ((Day(d)),(Month(m)),(Year(y)));

    int main(int argc,char* argv[])

    // Identify number of arguments.
    std::cout << "Have " << argc << " arguments:" << std::endl;
    for (int i = 0; i < argc; ++i) {
    std::cout << argv[i] << std::endl;

    char* Filename = argv[1]; // Then use 1

    std::ifstream file(Filename);
    string line;

    std::vector Dates;
    std::vector Open;
    std::vector High, Low, Close, Volumen;

    if (file !=NULL)
    std::vector data;

    // Ignore first line
    getline(file, line);

    // Get the rest of the lines
    const boost::regex re(“,”);
    boost::sregex_token_iterator token(line.begin(),line.end(),re,-1);
    boost::sregex_token_iterator end;

    Dates+=DateFormatter(data[0], “-“, 0, 1, 2);
    TimeSeries TS(Dates.begin(), Dates.end(), Close.begin());

    cout << TS[Date(20,Jun,2014)];

    return 0;

    catch (std::exception& e)
    std::cerr << e.what() << endl;

  2. édouard Autor de la entrada

    Hi Mauricio.
    They are several turnarounds to overcome your date format. Thanks for your solution. I personally have no experience with Mac.
    I don’t get well your comment “There is no way to cast from string “Jan” to Month 6.” Did you mean ‘Jun’, instead?

  3. Pingback: OHLC time series in Quantlib | Quant Corner


Por favor, inicia sesión con uno de estos métodos para publicar tu comentario:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )


Conectando a %s