-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockData.h
More file actions
174 lines (143 loc) · 5.86 KB
/
StockData.h
File metadata and controls
174 lines (143 loc) · 5.86 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
// Created by mahit on 10/27/2025.
//
#pragma once
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <cmath>
using namespace std;
struct stockInformation {
string date; double open; double high; double low; double close; int volume; int openInt;
stockInformation(
string p_date, double p_open, double p_high, double p_low, double p_close, int p_volume, int p_openInt
) : date(p_date), open(p_open), high(p_high),low(p_low), close(p_close), volume(p_volume), openInt(p_openInt)
{}
};
struct stockDataPoint {
vector<double> stockInputCategories;
double target;
};
class dataLoader {
vector<double> means;
vector<double> stddevs;
public:
const vector<stockDataPoint> readFile(const string &filePath) {
ifstream file(filePath);
if (!file.is_open()) {
cout << "File could not be opened" << endl;
}
vector<stockDataPoint> vec;
string line = "";
getline(file, line);
line = "";
while (getline(file, line)) {
string date; double open; double high; double low;
double close; int volume; int openInt;
string tempString = "";
stringstream inputString(line);
//Getting the date
getline(inputString, date, ',');
//Getting open value
getline(inputString, tempString, ',');
open = stod(tempString);
//Getting high value
getline(inputString, tempString, ',');
high = stod(tempString);
//Getting low value
getline(inputString, tempString, ',');
low = stod(tempString);
//Getting close value
getline(inputString, tempString, ',');
close = stod(tempString);
//Getting volume value
getline(inputString, tempString, ',');
volume = stoi(tempString);
//Getting openInt
getline(inputString, tempString, ',');
openInt = stoi(tempString);
stockInformation stockInfo(date, open, high, low, close, volume, openInt);
stockDataPoint trainingData;
trainingData.stockInputCategories.push_back(1.0f); //Implicit Bias
trainingData.stockInputCategories.push_back(stockInfo.open);
trainingData.stockInputCategories.push_back(stockInfo.high);
trainingData.stockInputCategories.push_back(stockInfo.low);
trainingData.stockInputCategories.push_back((double)stockInfo.volume);
trainingData.stockInputCategories.push_back((double)stockInfo.openInt);
trainingData.target = stockInfo.close;
vec.push_back(trainingData);
line = "";
}
return vec;
}
pair<vector<stockDataPoint>, vector<stockDataPoint>> splitData(const vector<stockDataPoint>& allData) {
int totalSize = allData.size();
int trainingSize = (int)(totalSize * 0.8);
vector<stockDataPoint> trainingData(allData.begin(), allData.begin() + trainingSize);
vector<stockDataPoint> testData(allData.begin() + trainingSize, allData.end());
return make_pair(trainingData, testData);
}
void scaleData(vector<stockDataPoint>& trainingData, vector<stockDataPoint>& testData) {
if (trainingData.empty()) {
cout << "Cannot scale empty training data." << endl;
return;
}
int numFeatures = trainingData[0].stockInputCategories.size();
int trainingSize = trainingData.size();
means.resize(numFeatures, 0.0);
stddevs.resize(numFeatures, 0.0);
//Calculate Mean for each feature ---
for (int i = 1; i < numFeatures; i++) {
double sum = 0.0;
for (int j = 0; j < trainingSize; j++) {
sum += trainingData[j].stockInputCategories[i];
}
means[i] = sum / trainingSize;
}
//Calculate Standard Deviation for each feature ---
for (int i = 1; i < numFeatures; i++) {
double varianceSum = 0.0;
for (int j = 0; j < trainingSize; j++) {
varianceSum += pow(trainingData[j].stockInputCategories[i] - means[i], 2);
}
// Add a very small number (epsilon) to prevent division by zero
stddevs[i] = sqrt(varianceSum / trainingSize) + 1e-8;
}
//Apply Scaling to trainingData
for (int i = 0; i < trainingSize; i++) {
for (int j = 1; j < numFeatures; j++) {
trainingData[i].stockInputCategories[j] = (trainingData[i].stockInputCategories[j] - means[j]) / stddevs[j];
}
}
//Apply Scaling to testData
int testSize = testData.size();
for (int i = 0; i < testSize; i++) {
for (int j = 1; j < numFeatures; j++) {
testData[i].stockInputCategories[j] = (testData[i].stockInputCategories[j] - means[j]) / stddevs[j];
}
}
}
pair<vector<vector<double>>, vector<double>> extractFeaturesAndTargets(const vector<stockDataPoint>& data) {
vector<vector<double>> features;
vector<double> targets;
features.reserve(data.size());
targets.reserve(data.size());
for (const auto& point : data) {
features.push_back(point.stockInputCategories);
targets.push_back(point.target);
}
return make_pair(features, targets);
}
vector<double> scaleSingleInput(const vector<double>& rawFeatures) {
vector<double> scaledFeatures = rawFeatures;
if (means.size() != rawFeatures.size() + 1) {
cerr << "Feature mismatch in SSI!" << endl;
return rawFeatures;
}
for (int i = 0; i < rawFeatures.size(); i++) {
scaledFeatures[i] = (rawFeatures[i] - means[i+1]) / stddevs[i+1];
}
return scaledFeatures;
}
};