-
Notifications
You must be signed in to change notification settings - Fork 85
/
JSONdata.cpp
389 lines (314 loc) · 10 KB
/
JSONdata.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
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
#include "JSONdata.h"
std::mutex _mtx; //For std::lock_guard
/* Access functions are used to call either the get or set for a
member variable, depending on what is past to them
(hence the boost::optionals)
Using boost::optional over std::optional so I can pass by reference
The point of using these instead of directly calling get or set functions
is to ensure thread safety. This way, no function can look at or set
a var while another thread has the lock. */
//Temp = varible to temporarily hold value to be set via set functions
//Copy = variable that will get a copy of whatever is returned by the get func
void JSONdata::accessUnAdjVol(boost::optional<std::vector<long long>&> copy,
boost::optional<long long> temp)
{
std::lock_guard<std::mutex> guard(_mtx);
if(temp)
setUnAdjVol(*temp);
else if(copy)
getUnAdjVol(*copy);
}
void JSONdata::accessVol(boost::optional<std::vector<long long>&> copy,
boost::optional<long long> temp)
{
std::lock_guard<std::mutex> guard(_mtx);
if(temp)
setVol(*temp);
else if(copy)
getVol(*copy);
}
void JSONdata::accessLabel(boost::optional<std::vector<std::string>&> copy,
boost::optional<std::string> temp)
{
std::lock_guard<std::mutex> guard(_mtx);
if(temp)
setLabel(*temp);
else if(copy)
getLabel(*copy);
}
void JSONdata::accessDate(boost::optional<std::vector<std::string>&> copy,
boost::optional<std::string> temp)
{
std::lock_guard<std::mutex> guard(_mtx);
if(temp)
setDate(*temp);
else if(copy)
getDate(*copy);
}
void JSONdata::accessChgOvrTime(boost::optional<std::vector<double>&> copy,
boost::optional<double> temp)
{
std::lock_guard<std::mutex> guard(_mtx);
if(temp)
setChgOvrTime(*temp);
else if(copy)
getChgOvrTime(*copy);
}
void JSONdata::accessVmap(boost::optional<std::vector<double>&> copy,
boost::optional<double> temp)
{
std::lock_guard<std::mutex> guard(_mtx);
if(temp)
setVmap(*temp);
else if(copy)
getVmap(*copy);
}
void JSONdata::accessChgPer(boost::optional<std::vector<double>&> copy,
boost::optional<double> temp)
{
std::lock_guard<std::mutex> guard(_mtx);
if(temp)
setChgPer(*temp);
else if(copy)
getChgPer(*copy);
}
void JSONdata::accessChange(boost::optional<std::vector<double>&> copy,
boost::optional<double> temp)
{
std::lock_guard<std::mutex> guard(_mtx);
if(temp)
setChange(*temp);
else if(copy)
getChange(*copy);
}
void JSONdata::accessClose(boost::optional<std::vector<double>&> copy,
boost::optional<double> temp)
{
std::lock_guard<std::mutex> guard(_mtx);
if(temp)
setClose(*temp);
else if(copy)
getClose(*copy);
}
void JSONdata::accessLow(boost::optional<std::vector<double>&> copy,
boost::optional<double> temp)
{
std::lock_guard<std::mutex> guard(_mtx);
if(temp)
setLow(*temp);
else if(copy)
getLow(*copy);
}
void JSONdata::accessHigh(boost::optional<std::vector<double>&> copy,
boost::optional<double> temp)
{
std::lock_guard<std::mutex> guard(_mtx);
if(temp)
setHigh(*temp);
else if(copy)
getHigh(*copy);
}
void JSONdata::accessOpen(boost::optional<std::vector<double>&> copy,
boost::optional<double> temp)
{
std::lock_guard<std::mutex> guard(_mtx);
if(temp)
setOpen(*temp);
else if(copy)
getOpen(*copy);
}
/* Here, set functions take in a value (temp), and push it back to their
corresponding member variable vector */
void JSONdata::setOpen(const double &temp)
{
pricingData.open.push_back(temp);
}
void JSONdata::setHigh(const double &temp)
{
pricingData.high.push_back(temp);
}
void JSONdata::setLow(const double &temp)
{
pricingData.low.push_back(temp);
}
void JSONdata::setClose(const double &temp)
{
pricingData.close.push_back(temp);
}
void JSONdata::setChange(const double &temp)
{
pricingData.change.push_back(temp);
}
void JSONdata::setChgPer(const double &temp)
{
pricingData.changePercent.push_back(temp);
}
void JSONdata::setVmap(const double &temp)
{
pricingData.vmap.push_back(temp);
}
void JSONdata::setChgOvrTime(const double &temp)
{
pricingData.changeOverTime.push_back(temp);
}
void JSONdata::setDate(const std::string &temp)
{
pricingData.date.push_back(temp);
}
void JSONdata::setLabel(const std::string &temp)
{
pricingData.label.push_back(temp);
}
void JSONdata::setVol(const long long &temp)
{
pricingData.volume.push_back(temp);
}
void JSONdata::setUnAdjVol(const long long &temp)
{
pricingData.unadjustedVolume.push_back(temp);
}
/* Get functions take in an vector, and make it a copy of whatever vector
the specific get function is for. These can be void because the vector
is passed by reference
These functions are const because we never modify any part of the class,
and a const function will ensure this */
void JSONdata::getOpen(std::vector <double> ©) const
{
copy = pricingData.open;
}
void JSONdata::getHigh(std::vector <double> ©) const
{
copy = pricingData.high;
}
void JSONdata::getLow(std::vector <double> ©) const
{
copy = pricingData.low;
}
void JSONdata::getClose(std::vector <double> ©) const
{
copy = pricingData.close;
}
void JSONdata::getChange(std::vector <double> ©) const
{
copy = pricingData.change;
}
void JSONdata::getChgPer(std::vector <double> ©) const
{
copy = pricingData.changePercent;
}
void JSONdata::getVmap(std::vector <double> ©) const
{
copy = pricingData.vmap;
}
void JSONdata::getChgOvrTime(std::vector <double> ©) const
{
copy = pricingData.changeOverTime;
}
void JSONdata::getDate(std::vector<std::string> ©) const
{
copy = pricingData.date;
}
void JSONdata::getLabel(std::vector<std::string> ©) const
{
copy = pricingData.label;
}
void JSONdata::getVol(std::vector<long long> ©) const
{
copy = pricingData.volume;
}
void JSONdata::getUnAdjVol(std::vector<long long> ©) const
{
copy = pricingData.unadjustedVolume;
}
/* This function will take the data fetched by the IEX::stocks::chartRange()
function. It will then parse that data into its corresponding vector inside
the priceData struct */
void JSONdata::parseIEXdata(const Json::Value &IEXdata)
{
assert(!IEXdata.empty() && isEmpty());
int i = 0;
double temp;
long long temp_lng;
std::string temp_str;
//Step through JSON file until the end is reached
while(i < IEXdata.size()) {
//Parse the JSON data into the struct
temp = IEXdata[i]["open"].asDouble();
accessOpen(boost::none, temp);
temp = IEXdata[i]["low"].asDouble();
accessLow(boost::none, temp);
temp = IEXdata[i]["close"].asDouble();
accessClose(boost::none, temp);
temp = IEXdata[i]["change"].asDouble();
accessChange(boost::none, temp);
temp = IEXdata[i]["changePercent"].asDouble();
accessChgPer(boost::none, temp);
temp = IEXdata[i]["vmap"].asDouble();
accessVmap(boost::none, temp);
temp = IEXdata[i]["changeOverTime"].asDouble();
accessChgOvrTime(boost::none, temp);
temp_lng = IEXdata[i]["volume"].asInt64();
accessVol(boost::none, temp_lng);
temp_lng = IEXdata[i]["unadjustedVolume"].asInt64();
accessUnAdjVol(boost::none, temp_lng);
temp_str = IEXdata[i]["date"].asString();
accessDate(boost::none, temp_str);
temp_str = IEXdata[i]["date"].asString();
accessLabel(boost::none,temp_str);
i++;
}
}
/* Clear all the data out of the vectors
This is done because the data needs to be always as up to date as possible
So, the data is gathered once, passed off to the Tech Analysis class for
calculations, and then cleared and replaced */
void JSONdata::clearJSONstruct(){
pricingData.open.clear();
pricingData.high.clear();
pricingData.low.clear();
pricingData.close.clear();
pricingData.change.clear();
pricingData.changePercent.clear();
pricingData.changePercent.clear();
pricingData.vmap.clear();
pricingData.changeOverTime.clear();
pricingData.date.clear();
pricingData.label.clear();
pricingData.volume.clear();
pricingData.unadjustedVolume.clear();
}
/* This function is used by some assert() statements.
There are times where the program will produce horrible results
if data is empty, this allows the program to use asserts to error check
(The assert should never crash the program, but this lets me be sure)
This is a const function in order to ensure that none of calling object
is modified */
bool JSONdata::isEmpty() const
{
if(!pricingData.open.empty())
return false;
else if(!pricingData.high.empty())
return false;
else if(!pricingData.low.empty())
return false;
else if(!pricingData.close.empty())
return false;
else if(!pricingData.change.empty())
return false;
else if(!pricingData.changePercent.empty())
return false;
else if(!pricingData.vmap.empty())
return false;
else if(!pricingData.changeOverTime.empty())
return false;
else if(!pricingData.date.empty())
return false;
else if(!pricingData.label.empty())
return false;
else if(!pricingData.volume.empty())
return false;
else if(!pricingData.unadjustedVolume.empty())
return false;
else
return true;
}