-
Notifications
You must be signed in to change notification settings - Fork 0
/
CsvReaderForHistoricalData.cpp
72 lines (66 loc) · 2.34 KB
/
CsvReaderForHistoricalData.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "CsvReaderForHistoricalData.h"
namespace MeanVarianceFrontier
{
CsvReaderForHistoricalData::CsvReaderForHistoricalData(std::string csvFileDir, std::string fileName) :
m_fileName{ fileName },
m_csvFilePath{ csvFileDir + m_fileName },
m_csvFile{ m_csvFilePath, std::ifstream::in }
{
if (m_csvFile)
{
readAndPopulateHistoricalPrices();
}
}
CsvReaderForHistoricalData::CsvReaderForHistoricalData(std::string fileName) :
m_fileName{fileName},
m_csvFilePath{ std::experimental::filesystem::current_path().u8string() + "\\data\\" + m_fileName },
m_csvFile{ m_csvFilePath, std::ifstream::in }
{
if (m_csvFile)
{
readAndPopulateHistoricalPrices();
}
}
std::vector<std::pair<boost::gregorian::date, double>> CsvReaderForHistoricalData::getHistoricalPrices() const
{
return m_historicalPrices;
}
void CsvReaderForHistoricalData::readAndPopulateHistoricalPrices()
{
std::string line;
std::stringstream iss;
std::getline(m_csvFile, line);//skip first line
while (std::getline(m_csvFile, line))
{
iss.str(line);
std::string val;
m_historicalPrices.emplace_back(parseDateAndPriceFrom(line));
}
}
std::pair<boost::gregorian::date, double> CsvReaderForHistoricalData::parseDateAndPriceFrom(std::string& line)
{
boost::erase_all(line, "\"");
std::stringstream iss(line);
std::string dateString;
std::string skip;
std::getline(iss, dateString, ',');
auto date = convertStringToDate(dateString);
std::string priceString;
std::getline(iss, priceString, ',');
boost::erase_all(priceString, "\"");
boost::erase_all(priceString, ",");
auto price = ::atof(priceString.c_str());
return std::make_pair(date, price);
}
boost::gregorian::date CsvReaderForHistoricalData::convertStringToDate(std::string dateString)
{
std::stringstream iss(dateString);
std::string month;
std::getline(iss, month, '-');
std::string day;
std::getline(iss, day, '-');
std::string year;
std::getline(iss, year);
return boost::gregorian::from_uk_string(day + "-" + month + "-" + year);
}
}