-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataModule.h
108 lines (93 loc) · 3.33 KB
/
DataModule.h
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#ifndef DATA_MODULE_H
#define DATA_MODULE_H
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <unordered_map>
#include <map>
#include <stdexcept>
struct TimeSeriesData {
double open;
double high;
double low;
double close;
int volume;
// Convert to a market data format suitable for portfolio updates
std::unordered_map<std::string, double> toMarketData() const {
return {
{"Open", open},
{"High", high},
{"Low", low},
{"Close", close}
};
}
};
class DataModule {
public:
// Function to load and parse time series data from CSV
bool loadTimeSeriesCSV(const std::string& filePath) {
std::ifstream file(filePath);
if (!file.is_open()) {
std::cerr << "Failed to open CSV file: " << filePath << std::endl;
return false;
}
std::string line;
int lineNumber = 0;
while (std::getline(file, line)) {
lineNumber++;
if (lineNumber == 1) {
// Skip header line if present
continue;
}
std::istringstream stream(line);
std::string timestamp, open, high, low, close, volume;
try {
// Parse each column (comma-separated)
std::getline(stream, timestamp, ',');
std::getline(stream, open, ',');
std::getline(stream, high, ',');
std::getline(stream, low, ',');
std::getline(stream, close, ',');
std::getline(stream, volume, ',');
// Validate and convert
if (timestamp.empty() || open.empty() || high.empty() || low.empty() || close.empty() || volume.empty()) {
std::cerr << "Error: Malformed line (" << lineNumber << ") -> " << line << std::endl;
continue;
}
TimeSeriesData tsData = {
std::stod(open),
std::stod(high),
std::stod(low),
std::stod(close),
std::stoi(volume)
};
timeSeriesData[timestamp] = tsData;
}
catch (const std::exception& ex) {
std::cerr << "Error parsing line (" << lineNumber << "): " << line << " -> " << ex.what() << std::endl;
}
}
file.close();
return true;
}
// Function to print time series data
void printTimeSeriesData() const {
for (const auto& [timestamp, data] : timeSeriesData) {
std::cout << timestamp << " -> "
<< "Open: " << data.open << ", "
<< "High: " << data.high << ", "
<< "Low: " << data.low << ", "
<< "Close: " << data.close << ", "
<< "Volume: " << data.volume << std::endl;
}
}
// Getter function to retrieve the time series data
const std::map<std::string, TimeSeriesData>& getTimeSeriesData() const {
return timeSeriesData;
}
private:
std::map<std::string, TimeSeriesData> timeSeriesData; // Maps timestamp to time series data
};
#endif // DATA_MODULE_H