-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
248 lines (188 loc) · 4.9 KB
/
main.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
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
// Author: Ari Bolton (8/28/14)
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <string>
#include "Stock.h"
#include <cctype>
#define NUM_STOCKS 2908
using namespace std;
void getData(Stock & st);
void printVals(Stock & st);
Stock* addStock(string & inp, string allSymbols[ NUM_STOCKS ], vector<Stock*> & tStocks);
void init(string syms[ NUM_STOCKS ]);
void runner(int & num, string & inp, string allSymbols[], vector<Stock*> & tStocks);
bool isTracked(string & inp, vector<Stock*> & tStocks);
Stock* getStock(string sym, vector<Stock*> & tStocks);
bool untrackStock(string sym, vector<Stock*> & tStocks);
int main() {
string inp, allSymbols[ NUM_STOCKS ];
vector<Stock*> tStocks;
int num = 10, i, k;
float j;
string str("FB");
init(allSymbols);
while(num > 10)
runner(num, inp, allSymbols, tStocks);
for(k = 0; k < NUM_STOCKS; k++) {
cerr << k << endl;
for(i = 5; i < 40; i++) {
for(j = 1.5; j < 2.5; j += 0.1) {
addStock(allSymbols[ k ], allSymbols, tStocks)->computeBB(i,j);
untrackStock(allSymbols[ k ], tStocks);
}
}
}
for(int i = 0; i < tStocks.size(); i++)
delete tStocks[i];
return 0;
}
void getData(Stock & st) {
float cl, hi, lo, op;
int vo, dummy, i;
char d = 'b';
char dm[15];
string str, nm = "../rawtxtfiles/data_8_28_14/" + st.name + ".txt";
ifstream in(nm);
vector<Day> & vec = *st.data;
for(i = 0; i < 7; i++)
getline(in, str);
d = in.peek();
while(d != EOF)
{
if(d == 'a') { // must remove timestamp
in.getline(dm, 100, ',');
in >> cl >> d >> hi >> d >> lo >> d
>> op >> d >> vo >> d >> dummy;
} else if((int)d > 47 && (int)d < 58) { // must remove number
in.getline(dm, 100, ',');
in >> cl >> d >> hi >> d >> lo >> d
>> op >> d >> vo >> d >> dummy;
} else { // must remove line
in.getline(dm, 100);
}
vec.push_back(Day(cl, hi, lo, op, vo));
d = in.peek();
if(d == '\n')
{
in.get(d);
d = in.peek();
}
} // while
in.close();
} // getData
void printVals(Stock & st) {
vector<Day> & vec = *st.data;
for(int i = 0; i < vec.size() ; i++)
cout << vec[i].close << " " << vec[i].high << " " << vec[i].low
<< " " << vec[i].open << " " << vec[i].vol << endl;
} // printVals
Stock* addStock(string & inp, string allSymbols[ NUM_STOCKS ], vector<Stock*> & tStocks) {
bool isValid = true;
int i;
Stock * st = NULL;
/*
cout << "Enter a stock: ";
getline(cin, inp);
*/
for(i = 0; i < inp.length(); i++) {
if(!isalpha(inp[i])) {
isValid = false;
cout << "Invalid characters inputted\n\n";
return st;
}
}
for(i = 0; i < inp.length(); i++)
inp[i] = toupper(inp[i]);
for(i = 0; i < NUM_STOCKS && allSymbols[i] != inp; i++);
if(isValid && i < NUM_STOCKS) {
st = new Stock(inp);
tStocks.push_back(st);
getData(*(tStocks[tStocks.size() - 1]));
cout << inp << " added\n\n";
return st;
} else {
cout << inp << " not found\n\n";
return st;
}
return st;
}
void init(string syms[ NUM_STOCKS ]) {
ifstream inp("nlisted.txt");
string l;
int i = 0;
while(getline(inp, l))
syms[i++] = l;
inp.close();
}
void runner(int & num, string & inp, string allSymbols[], vector<Stock*> & tStocks) {
int i;
cout << "Choose an option" << endl
<< "0. Exit" << endl
<< "1. Track a new stock" << endl
<< "2. Print a stock's value for the last year" << endl
<< "3. Compute Bollinger Bands" << endl
<< ">> ";
cin >> num;
cin.get();
switch(num) {
case 0:
break;
case 1:
addStock(inp, allSymbols, tStocks);
break;
case 2:
cout << "\nEnter a stock: ";
getline(cin, inp);
if(isTracked(inp, tStocks)) {
cout << endl;
for(i = 0; tStocks[i]->name != inp; i++);
printVals(*(tStocks[i]));
cout << endl;
} else {
cout << "Stock is untracked\n";
}
break;
case 3:
cout << "\nEnter a stock: ";
getline(cin, inp);
if(isTracked(inp, tStocks))
(getStock(inp, tStocks))->computeBB(20,2);
else
cout << "Stock is untracked\n";
break;
default:
break;
}
} // runner
bool isTracked(string & inp, vector<Stock*> & tStocks) {
int i;
for(i = 0; i < inp.length(); i++)
inp[i] = toupper(inp[i]);
for(i = 0; i < tStocks.size() && tStocks[i]->name != inp; i++);
if(i < tStocks.size()) {
return true;
} else {
return false;
}
} //isTracked
bool isValidSymbol(string inp) {
return false;
} //isValidSymbol
Stock* getStock(string sym, vector<Stock*> & tStocks) {
int i;
for(i = 0; i < tStocks.size() && tStocks[i]->name != sym; i++);
if(i == tStocks.size())
return NULL;
else
return tStocks[i];
return NULL;
}
bool untrackStock(string sym, vector<Stock*> & tStocks) {
int i;
for(i = 0; tStocks[i]->name != sym && i < tStocks.size(); i++);
delete tStocks[i];
tStocks.erase(tStocks.begin() + i);
return true;
}