1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
typedef boost::gregorian::date bdate;
std::ifstream in;
std::string line;
std::vector<std::string> lines;
// Try opening the file
in.open(filename, std::ifstream::in);
std::vector<std::string> outerArray; // All blocks separated by ,
while (!IO::safeGetline(in, line).eof())
lines.push_back(line);
// Container tools
std::vector<std::string> header;
// Getting the asset names, number of assets, start date and end date
boost::split(header, lines.front(), boost::is_any_of(","));
const unsigned int nbassets = (int)(header.size() + 1) / 3;
std::vector<std::vector<double> > quotes(nbassets, std::vector<double>(0));
std::vector<std::vector<bdate> > dates(nbassets, std::vector<bdate>(0));
bdate date;
std::string assetname;
// Collecting the dates and prices
for (unsigned int i = 1; i < lines.size(); i++)
{
// Clear the containers
outerArray.clear();
boost::split(outerArray, lines[i], boost::is_any_of(","));
// Add the historical price of each asset if it trades
for (unsigned int j = 0; j < nbassets; j++) {
if (outerArray[3 * j] != "") {
// Get the date and trading date of the line
date = TDate::Text2BDate(outerArray[3 * j]);
dates[j].push_back(date);
quotes[j].push_back(atof(outerArray[3 * j + 1].c_str())); // (j+1) because of the date in column 0
}
}
}
bdate TDate::Text2BDate(std::string text) {
// Container and separate all three parts of date (separated by /)
std::vector<std::string> innerArrayDiv;
boost::split(innerArrayDiv, text, boost::is_any_of("/"));
int day = std::atoi(innerArrayDiv[0].c_str());
int month = std::atoi(innerArrayDiv[1].c_str());
int year = std::atoi(innerArrayDiv[2].c_str());
return bdate(year, month, day);
}
|