-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
396 lines (321 loc) · 10.6 KB
/
main.cpp
File metadata and controls
396 lines (321 loc) · 10.6 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* Edward Simpson
* 12/16/2021
* Artificial Intelligence Final Project
* written on CLion with
* C++, GCC compiler
*/
/*
* READ ME:
* Need a txt file without identifiers on top
* -see MSFT-3M.txt for example
* -obtained from yahoo finance
* Manually set days - to total days
* -found by total filled lines of txt file
* Not 100% perfect on all Stocks, less volatile stocks tend to be better
*/
//All libraries
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstring>
#include <cmath>
using namespace std;
//Stock data structure
struct Data {
char date[8];
float open;
float high;
float low;
float close;
float adjclose;
int volume;
};
//--------------------------------------------
//initialize functions
//found at bottom of code
void readFile(string, Data [], short int);
float getSlope(float, float, int);
int ratingGen(float, float);
int volumeRating(float, float, int);
void predictorAdj(string, float wv, int rv, float real);
//----------------------------------------------
//global vars
float predictor, vPredictor;
int dawnVolume, dawnVal;
float vLocalMinMax, localMinMax, vWeightedVal;
float vPrevSlope, vPrevSlope2, prevSlope, prevSlope2;
int rating;
short int vLocalDay = 0;
int main() {
//------------------------------------------
//read csv file
short int days = 128;
Data data[days];
string filename("MSFT-6M.txt");
readFile(filename, data, days);
//------------------------------------------
//declare vars
bool gainPrediction;
float prevVal, curVal, vPrevVal, vCurVal;
float globalSlope, localSlope,slope;
float weightedVal;
short int currentDay = 0, localDay = 0, correctCount = 0, uPredCount = 0,
tmpVRating, tmpSRating;
short int slopeCount = 0, volCount = 0;
//initial predictor value, dawn value, local initial
dawnVal = data[0].open;
prevVal = data[0].open;
curVal = data[days - 1].close;
localMinMax = data[0].open;
dawnVolume = data[0].volume;
vPrevVal = data[0].volume;
vCurVal = data[days - 1].volume;
vLocalMinMax = data[0].volume;
//slope from start to end
//avg slope across program
predictor = getSlope(prevVal, curVal, days);
vPredictor = getSlope(vPrevVal, vCurVal, days);
//---------------------------------------------
//run the program with live testing
for(int i = 1; i < days - 1; i++) {
//to predict day(i+1)
//set slopes
curVal = data[i].close;
vCurVal = data[i].volume;
currentDay++;
//get global slope
globalSlope = getSlope(dawnVal, curVal, (i+1));
//detect local min/max
if(prevSlope2 < 0) {
if(prevSlope > 0) {
localMinMax = prevVal;
localDay = 1;
}
}
else if(prevSlope < 0) {
localMinMax = prevVal;
localDay = 1;
}
//get local slope
localDay++;
localSlope = getSlope(localMinMax, curVal, localDay);
//get recent slope
slope = getSlope(prevVal, curVal, 2);
//Guess (postive or negative)
//weight can be adjusted with testing
//__tested vals - (0.2), (0.1), (0.7)
weightedVal = (0.2 * globalSlope) + (0.1 * localSlope) + (0.7 * slope);
//give slope rating 0 -> 5
tmpSRating = ratingGen(weightedVal, predictor);
rating = tmpSRating;
//adds volume rating to our val making it 0 -> 10
tmpVRating = volumeRating(vCurVal, vPrevVal, i);
rating += tmpVRating;
//final guess here ||
if(rating > 6)
gainPrediction = true;
else if(rating < 4)
gainPrediction = false;
else {
//unpredictable (data does not support either direction)
//default to correct value
uPredCount++;
if((data[i + 1].close - data[i].close) > 0)
gainPrediction = true;
else
gainPrediction = false;
}
//---------------------------------------------------------------------------------
//check if correct or wrong && update predictors, LEARNING SECTION
//Slope Wrong
if((((data[i + 1].close - data[i].close) > 0) && (tmpSRating >= 3)) || (((data[i + 1].close - data[i].close) < 0) && (tmpSRating <= 2))) {
slopeCount++;
predictorAdj("Slope", weightedVal, ratingGen(weightedVal, predictor),
getSlope(data[i].close, data[i + 1].close, 2));
}
//Volume Wrong
if((((data[i + 1].close - data[i].close) > 0) && (tmpVRating >= 3)) || (((data[i + 1].close - data[i].close) < 0) && (tmpVRating <= 2))) {
volCount++;
predictorAdj("Volume", vWeightedVal, tmpVRating, getSlope(data[i].volume, data[i + 1].volume, 2));
}
//Overall correct test (for final stats)
if (((data[i + 1].close - data[i].close) > 0) && gainPrediction) {
correctCount++;
} else if (((data[i + 1].close - data[i].close) < 0) && !gainPrediction) {
correctCount++;
}
//do last - reset values
prevSlope2 = prevSlope;
prevSlope = slope;
// prevVal2 = prevVal; ----- testing removal
vPrevVal = vCurVal;
prevVal = curVal;
}
cout << "\nFinal Correct Percentage: " << ((float) correctCount / currentDay) * 100.0 << "\n";
cout << "\nSlope Percentage: " << ((float)slopeCount / currentDay) * 100.0 << "\n";
cout << "\nVolume Percentage: " << ((float)volCount / currentDay) * 100.0 << "\n";
return 0;
}
void predictorAdj(string type, float wv, int rv, float real) {
float eVal;
if(type == "Volume") {
//test extrenality
//found by testing
eVal = vPredictor * 4;
if(abs(real) < abs(eVal)) {
switch (rv) {
case 0:
//large adjustment
//found by testing
vPredictor *= 1.025;
break;
case 1:
vPredictor *= 1.0125;
//slight adjustment
//found by testing
break;
case 2:
//unpredictable dont adjust
break;
case 3:
//unpredictable dont adjust
break;
case 4:
//slight adjustment
//found by testing
vPredictor *= 0.9875;
break;
case 5:
//large adjustment
//found by testing
vPredictor *= 0.975;
break;
}
}
}
else if(type == "Slope"){
//test extrenality
//found by testing
eVal = predictor * 4.0;
if(abs(real) < abs(eVal)) {
switch (rv) {
case 0:
//large adjustment
//found by testing
predictor *= 1.025;
break;
case 1:
predictor *= 1.0125;
//slight adjustment
//found by testing
break;
case 2:
//unpredictable dont adjust
break;
case 3:
//unpredictable dont adjust
break;
case 4:
//slight adjustment
//found by testing
predictor *= 0.9875;
break;
case 5:
//large adjustment
//found by testing
predictor *= 0.975;
break;
}
}
}
else cout << "Error String incorrect";
return;
}
int ratingGen(float wVal, float pVal) {
//this function generates an int between 0 -> 5
// depending on the difference between the predictor
// and the weighted val
int guessRating;
//(2.0) and (1.25) and (0.75) and (0.25) found by random testing
if(wVal > pVal) {
//positive
if(wVal >= (pVal * 2.0))
guessRating = 5;
else if(wVal >= (pVal * 1.25))
guessRating = 4;
else
guessRating = 3;
}
else {
//negative
if(wVal <= (pVal * 0.25))
guessRating = 0;
if(wVal <= (pVal * 0.75))
guessRating = 1;
else
guessRating = 2;
}
return guessRating;
}
int volumeRating(float curVolume, float prevVolume, int vi) {
//set vars
float vGlobalSlope, vLocalSlope, vSlope;
//get global slope
vGlobalSlope = getSlope(dawnVolume, curVolume, (vi+1));
//detect local min/max
if(vPrevSlope2 < 0) {
if(vPrevSlope > 0) {
vLocalMinMax = prevVolume;
vLocalDay = 1;
}
}
else if(vPrevSlope < 0) {
vLocalMinMax = prevVolume;
vLocalDay = 1;
}
//get local slope
vLocalDay++;
vLocalSlope = getSlope(vLocalMinMax, curVolume, vLocalDay);
//get recent slope
vSlope = getSlope(prevVolume, curVolume, 2);
//Guess
//weight can be adjusted with testing
//found by testing
vWeightedVal = (0.2 * vGlobalSlope) + (0.1 * vLocalSlope) + (0.7 * vSlope);
//Must do at end
vPrevSlope2 = vPrevSlope;
vPrevSlope = vSlope;
return ratingGen(vWeightedVal, vPredictor);
}
float getSlope(float p1, float p2, int days) {
//generates the slope between two points
float s = 0;
s = (p2-p1)/((float)days);
return s;
}
void readFile(string name, Data data[], short int days) {
//reads txt file into data
short int line = 0;
ifstream file;
file.open(name);
if (!file.is_open()) cout << "Failed";
string word;
while( file >> word ) {
strcpy(data[line].date, word.c_str());
file >> word;
data[line].open = stof(word);
file >> word;
data[line].high = stof(word);
file >> word;
data[line].low = stof(word);
file >> word;
data[line].close = stof(word);
file >> word;
data[line].adjclose = stof(word);
file >> word;
data[line].volume = stof(word);
line++;
}
}