-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMulGetData.cpp
318 lines (291 loc) · 8.53 KB
/
MulGetData.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
#include "curl.h"
#include "Share.h"
#include "GetData.hpp"
#include "MulGetData.h"
#include <stdio.h>
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <locale>
#include <locale.h>
#include <iomanip>
#include <fstream>
#include <thread>
#include <stack>
#include <map>
#include <ctime>
#include <mutex>
#include <string>
using namespace std;
void MulGetspyAlsoGetCrumb(Share& spy, string& sCrumb, string& sCookies) {
string startTime = getTimeinSeconds("2016-09-01T16:00:00");
string endTime = getTimeinSeconds("2018-04-30T16:00:00");
spy.SetTicker("spy");
struct MemoryStruct data;
data.memory = NULL;
data.size = 0;
FILE *fp;
CURL * handle;
handle = curl_easy_init();
CURLcode result;
if (handle)
{
curl_easy_setopt(handle, CURLOPT_URL, "https://finance.yahoo.com/quote/AMZN/history");
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(handle, CURLOPT_COOKIEJAR, "cookies.txt");
curl_easy_setopt(handle, CURLOPT_COOKIEFILE, "cookies.txt");
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data2);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, (void *)&data);
result = curl_easy_perform(handle);
if (result != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(result));
//return 1;
}
char cKey[] = "CrumbStore\":{\"crumb\":\"";
char *ptr1 = strstr(data.memory, cKey);
char *ptr2 = ptr1 + strlen(cKey);
char *ptr3 = strstr(ptr2, "\"}");
if (ptr3 != NULL)
*ptr3 = NULL;
sCrumb = ptr2;
fp = fopen("cookies.txt", "r");
char cCookies[100];
if (fp) {
while (fscanf(fp, "%s", cCookies) != EOF);
fclose(fp);
}
else
cerr << "cookies.txt does not exists" << endl;
sCookies = cCookies;
free(data.memory);
data.memory = NULL;
data.size = 0;
string urlA = "https://query1.finance.yahoo.com/v7/finance/download/";
string symbol = spy.GetTicker();
string urlB = "?period1=";
string urlC = "&period2=";
string urlD = "&interval=1d&events=history&crumb=";
string url = urlA + symbol + urlB + startTime + urlC + endTime + urlD + sCrumb;
const char * cURL = url.c_str();
const char * cookies = sCookies.c_str();
curl_easy_setopt(handle, CURLOPT_COOKIE, cookies); // Only needed for 1st stock
curl_easy_setopt(handle, CURLOPT_URL, cURL);
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data2);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, (void *)&data);
result = curl_easy_perform(handle);
if (result != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(result));
//return 1;
}
stringstream sData;
sData.str(data.memory);
string sValue, sDate;
double dValue = 0;
string line;
map<Date, double> curprice;
stack<Date> dates;
stack<double> adj;
getline(sData, line);
while (getline(sData, line)) {
//cout << line << endl;
sDate = line.substr(0, line.find_first_of(','));
line.erase(line.find_last_of(','));
sValue = line.substr(line.find_last_of(',') + 1);
dValue = strtod(sValue.c_str(), NULL);
tm t{};
istringstream d(sDate);
d >> get_time(&t, "%Y-%m-%d");
int year = t.tm_year+1900;
int mon = t.tm_mon+1;
int day = t.tm_mday;
Date dd(year, mon, day);
dates.push(dd);
adj.push(dValue);
}
while (!dates.empty()) {
curprice[dates.top()] = adj.top();
dates.pop();
adj.pop();
}
spy.SetPrice(curprice);
spy.SetStartDate((curprice.begin())->first);
spy.SetEndDate((--curprice.end())->first);
std::map<Date, double> ret;
for (auto i = curprice.begin(); i != --curprice.end(); i++) {
auto j = i;
j++;
ret[j->first] = (j->second - i->second) / i->second;
}
spy.SetReturn(ret);
free(data.memory);
data.size = 0;
cout << spy.GetTicker();
}
else
{
fprintf(stderr, "Curl init failed!\n");
}
curl_easy_cleanup(handle);
}
void MulAllStock(map<string, Stock>& all_stocks) {
ifstream file("SP500.csv");
string line;
if (!file.is_open()) {
cerr << "Load SP500data.csv Failed" << endl;
system("pause");
}
while (getline(file, line)) {
istringstream ss(line);
string token;
Stock temp;
getline(ss, token, ',');
temp.SetTicker(token);
getline(ss, token, ',');
string date = token;
getline(ss, token, ',');
double est = stod(token);
getline(ss, token, ',');
temp.SetSurprise((stod(token) - est) / est*100);
getline(ss, token, ',');
int y = stoi(token);
getline(ss, token, ',');
int m = stoi(token);
getline(ss, token, ',');
int d = stoi(token);
Date rep(y, m, d);
temp.SetReportDate(rep);
all_stocks[temp.GetTicker()] = temp;
}
cout << "Reading Mission Completed" << endl;
}
void Muldownload(Stock& stock, string sCrumb, string sCookies) {
//barrier.lock();
struct MemoryStruct data;
data.memory = NULL;
data.size = 0;
FILE *fp;
CURL * handle;
handle = curl_easy_init();
CURLcode result;
if (handle)
{
//curl_easy_setopt(handle, CURLOPT_URL, "https://finance.yahoo.com/quote/AMZN/history");
/*curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data2);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, (void *)&data);
free(data.memory);
data.memory = NULL;
data.size = 0;
*/
string urlA = "https://query1.finance.yahoo.com/v7/finance/download/";
string symbol = stock.GetTicker();
string urlB = "?period1=";
string urlC = "&period2=";
string startTime = getTimeinSeconds("2016-08-01T16:00:00");
string endTime = getTimeinSeconds("2018-04-30T16:00:00");
string urlD = "&interval=1d&events=history&crumb=";
string url = urlA + symbol + urlB + startTime + urlC + endTime + urlD + sCrumb;
const char * cURL = url.c_str();
const char * cookies = sCookies.c_str();
curl_easy_setopt(handle, CURLOPT_COOKIE, cookies); // Only needed for 1st stock
curl_easy_setopt(handle, CURLOPT_URL, cURL);
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0);
//curl_easy_setopt(handle, CURLOPT_COOKIEJAR, "cookies.txt");
curl_easy_setopt(handle, CURLOPT_COOKIEFILE, "cookies.txt");
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data2);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, (void *)&data);
result = curl_easy_perform(handle);
if (result != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(result));
//return stock_price;
}
istringstream sData(data.memory);
string sValue, sDate;
double dValue = 0;
string line;
map<Date, double> curprice;
stack<Date> dates;
stack<double> adj;
int count = 0;
getline(sData, line);
while (getline(sData, line)) {
//cout << line << endl;
sDate = line.substr(0, line.find_first_of(','));
line.erase(line.find_last_of(','));
sValue = line.substr(line.find_last_of(',') + 1);
dValue = strtod(sValue.c_str(), NULL);
tm t{};
istringstream d(sDate);
d >> get_time(&t, "%Y-%m-%d");
int year = t.tm_year+1900;
int mon = t.tm_mon+1;
int day = t.tm_mday;
Date dd(year, mon, day);
if (dd > stock.GetReportDate()) {
dates.push(dd);
adj.push(dValue);
count++;
}
else {
dates.push(dd);
adj.push(dValue);
}
if (count == 30) {
break;
}
}
while (!dates.empty()) {
if (dates.top() < stock.GetReportDate()) {
curprice[dates.top()] = adj.top();
dates.pop();
adj.pop();
count--;
}
else {
curprice[dates.top()] = adj.top();
dates.pop();
adj.pop();
}
if (count == -30) {
break;
}
}
stock.SetPrice(curprice);
stock.SetStartDate((curprice.begin())->first);
stock.SetEndDate((--curprice.end())->first);
std::map<Date, double> ret;
for (auto i = curprice.begin(); i != --curprice.end(); i++) {
auto j = i;
j++;
ret[j->first] = (j->second - i->second) / i->second;
}
stock.SetReturn(ret);
free(data.memory);
data.size = 0;
cout << stock.GetTicker();
}
else {
fprintf(stderr, "Curl init failed!\n");
}
curl_easy_cleanup(handle);
//barrier.unlock();
}
void MulTimeForMagic(map<string, Stock>& all_stocks, Share& spy) {
vector<thread> mythread;
string sCrumb;
string sCookies;
cout << "Note: If any bug shows up or the thread stucks, run again or restart VS. You would be informed if download completed."<< endl;
curl_global_init(CURL_GLOBAL_DEFAULT);
MulGetspyAlsoGetCrumb(spy, sCrumb, sCookies);
for (map<string, Stock>::iterator itr = all_stocks.begin(); itr != all_stocks.end(); itr++)
mythread.push_back(thread(Muldownload, ref(itr->second), sCrumb, sCookies));
for (auto &t : mythread)
t.join();
curl_global_cleanup();
cout << endl<< "Download completed. Total downloaded stocks: " << all_stocks.size() << endl;
}