CSV Importer

class quotek::datasource::csv

csv is a class that helps importing financial data stored in CSV files. it has multiple import features depending on what user wants to achieve.

Inherits from quotek::datasource::datasource

Public Functions

csv(std::string source_file, char separator)

csv class constructor .

Parameters
  • source_file -

    Source of the CSV file, under the form scheme://path_to_csv. Scheme can be either: *file:// , for local csv files. *http:// , for remote, csv files that can be fetched over http.

  • separator -

    string that tells what is the columns separator in the csv.

~csv()

rss class destructor

void from_string(std::string content)

from_string loads csv data from string instead of file.

Parameters
  • content -

    csv string content.

void setTimeColumn(int dt_colnum, std::string dt_format)

This function allows to tell which column in the csv contains the date/time data (if one exists), and which format it is. The format follows the usual UNIX date() format expression.

Parameters
  • dt_colnum -

    integer telling which column in the csv contains datatime data. It is zero-starting, meaning that first column is 0, second is 1, etc..

  • dt_format -

    date string format, essential for date parsing. For example “%Y-%m-%d %H:%M:%S” as date format means the csv contains dates like “2015-09-15 16:00:00”. If the time format in the CSV is an epoch timestamp, just put “%s” in it.

void setOffset(int offset)

Sets a line offset at which to start importing data. Can be useful for very large CSV files for which user only wants a part.

Parameters
  • offset -

    integer representing the line of the csv file to start at.

void addFilter(std::string filter)

Adds a new line filter to the instanciated csv object. A line filter allows to take only the CSV lines that match the filter. filters must be provided as regular expression strings for later compilation. See the c++ <regex> documentation for further infos about how to use them. Note about the filters list: filters add each other (boolean AND), so if you want to perform OR operations, you will have to put them in a single regex.

Parameters
  • filter -

    regex string to provide for filtering.

void clearFilters()

clears all the previously added filters of the instanciated csv object.

std::vector<std::string> getFilters()

This function returns the list of CSV filters of the instanciated csv object.

Return
list of the csv filters, with the form of a vector.

quotek::data::records import_records(int nb_records, int value_colnum)

Imports data in the source csv as quotek records.

Return
a quotek::data::records container.
Parameters
  • nb_records -

    Sets the maximum size of lines to import, if user wants to import only a part of the data nb_records = 0 means import all.

  • value_colnum -

    Tells which column to take record value from. It is zero-starting, meaning that first column is 0, second is 1, etc..

std::vector<quotek::data::history> import_history(int nb_lines, int open_colnum, int high_colnum, int low_colnum, int close_colnum)

Imports data in the source csv as a vector of quotek history objects.

Return
a vector of quotek::data::history objects.
Parameters
  • nb_lines -

    maximum number of history lines the user wants to import. 0 means import all.

  • open_column -

    zero-starting index of the column containing the open value.

  • high_colnum -

    zero-starting index of the column containing the high value.

  • low_colnum -

    zero-starting index of the column containing the low value.

  • close_colnum -

    zero-starting index of the column containing the close value.

Example

In the following example, we will assume that we have a csv file called “dow_values.csv” with the following content:

#DATE, #CLOSING_VALUE
2016-08-01, 17590.1
2016-08-02, 17640.5
2016-08-03, 17610.2
2016-08-04, 17580.7
#include <quotek/quotek.hpp>
#include <quotek/datasources/csv.hpp>
using namespace quotek::datasource;
using namespace quotek::data;

int main(int argc, char** argv) {

  //We declare a new csv object.
  csv csv1("file://./dow_values.csv",',');

  //We skip the first header line.
  csv1.setOffset(1);

  //We import the four lines of data using column 1 of the csv file
  records r1_dow = csv1.import_records(4,1);

  //We check that we got some data in r1_dow.
  assert( r1_dow.size() > 0 );

  //We check that it did import the correct data
  std::cout << "DOW FIRSTVAL:" << r1_dow[0].value << std::endl;

}