-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal.cpp
407 lines (339 loc) · 13.4 KB
/
final.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//
// main.cpp
// Final project v1216
//
// Created by Lu on 2021/12/16.
//
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <locale>
#include <iomanip>
#include <fstream>
#include <map>
#include <algorithm>
#include "curl/curl.h"
#include "Bootstrap.h"
#include"calculation.h"
#include "Vector.h"
#include "Data.h"
#include "Download.h"
using namespace std;
/*
Vector calculate_return(Vector prices) {
int size = prices.size();
Vector result;
for (int i = 0; i < size - 1; i++) {
result.push_back((prices[i + 1] - prices[i]) / prices[i]);
}
return result;
}
Vector calculate_cum_ret(Vector daily_return) {
int size = daily_return.size();
Vector result;
double cumsum = 0.0;
for (int i = 0; i < size; i++) {
cumsum += daily_return[i];
result.push_back(cumsum);
}
return result;
}
*/
void pop_bs_vec(BootStrap bs, vector<vector<string>>& popvec, vector<string>original_vec, int times) {
for (int i = 0; i < times; i++) {
vector<string> sample = bs.resample(original_vec);
popvec.push_back(sample);
}
}
void plot_caar(map<string, vector<Vector>> calculation_results) {
int dataSize = calculation_results["BEAT ESTIMATE"][1].size();
int N = (dataSize >> 1) + 1;
FILE* gnuplotPipe, * tempDataFile;
const char* tempDataFileName1 = "Beat";
const char* tempDataFileName2 = "Meet";
const char* tempDataFileName3 = "Miss";
double x, y, x2, y2, x3, y3;
int i;
gnuplotPipe = _popen("C:\\PROGRA~1\\gnuplot\\bin\\gnuplot.exe", "w");
if (gnuplotPipe) {
fprintf(gnuplotPipe, "plot \"%s\" with lines, \"%s\" with lines, \"%s\" with lines\n", tempDataFileName1, tempDataFileName2, tempDataFileName3);
fflush(gnuplotPipe);
tempDataFile = fopen(tempDataFileName1, "w");
for (i = 0; i < dataSize; i++) {
x = i - N;
y = calculation_results["BEAT ESTIMATE"][1][i];
fprintf(tempDataFile, "%lf %lf\n", x, y);
}
fclose(tempDataFile);
tempDataFile = fopen(tempDataFileName2, "w");
for (i = 0; i < dataSize; i++) {
x2 = i - N;
y2 = calculation_results["MEET ESTIMATE"][1][i];
fprintf(tempDataFile, "%lf %lf\n", x2, y2);
}
fclose(tempDataFile);
tempDataFile = fopen(tempDataFileName3, "w");
for (i = 0; i < dataSize; i++) {
x3 = i - N;
y3 = calculation_results["MISS ESTIMATE"][1][i];
fprintf(tempDataFile, "%lf %lf\n", x3, y3);
}
fclose(tempDataFile);
fprintf(gnuplotPipe, "set term wxt\n");
fprintf(gnuplotPipe, "set grid\n");
fprintf(gnuplotPipe, "set xlabel 'N - Number of days'\n");
fprintf(gnuplotPipe, "set ylabel 'CAAR'\n");
fprintf(gnuplotPipe, "set title 'CAAR of all groups'\n");
fprintf(gnuplotPipe, "set key left top\n");
fprintf(gnuplotPipe, "set key width '10'\n");
printf("press enter to continue...");
system("pause");
remove(tempDataFileName1);
remove(tempDataFileName2);
remove(tempDataFileName3);
fprintf(gnuplotPipe, "exit \n");
}
else {
printf("gnuplot not found...");
}
}
void showmenu()
{
cout << "----------Menu--------------" << endl;
cout << "1. Enter N." << endl
<< "2. Pull information for one stock." << endl
<< "3. Show AAR, AAR-SD, CAAR and CAAR-STD for one group." << endl
<< "4. Show the gnuplot graph with CAAR for all 3 groups." << endl
<< "5. Exit menu." << endl << endl;
cout << endl;
}
int main()
{
srand(time(NULL));
int N = 0;
Data data;
vector<string> symbolList;
data.populateSymbolVector(symbolList);
//read earnings csv
vector<string> ticker;
vector<string> announce_date;
vector<string> prd_end;
vector<string> estimate;
vector<string> report_earn;
vector<string> surprise;
vector<string> surprise_pct;
vector<string> MissEst_group;
vector<string> MeetEst_group;
vector<string> BeatEst_group;
data.populateEarningVector(ticker, announce_date, prd_end, estimate, report_earn, surprise, surprise_pct);
map<string, string> ticker_date_map; // a map that contains tickers and corresponding day zeros
for (int i = 0; i < (int)ticker.size(); i++) {
//use intersection of symbolList and ticker
if (find(symbolList.begin(), symbolList.end(), ticker[i]) != symbolList.end() && ticker[i] != "") {
ticker_date_map[ticker[i]] = announce_date[i];
}
}
//price_map: ticker and vector of price pair
//benchmark_map: ticker and vector of benchmark price pair
map<string, Vector> price_map, benchmark_map;
map<string, double> IWB_date_map;
map<string, map<string, double>> date_price;
getAdjClose(ticker_date_map, date_price, IWB_date_map);
vector<string> curSymbols;
map<string, Vector>::iterator itr;
vector<string> valid_symbol;
map<string, Vector> ARtable;
string curSymbol;
Vector stockReturn;
Vector benchmarkReturn;
vector<string> sort_vec;
//Bootstrap
BootStrap bootstrap;
vector<vector<string>>BeatEstvec;
vector<vector<string>>MeetEstEstvec;
vector<vector<string>>MissEstEstvec;
int resample_time = 40;
int num_samples = 80;
vector<Vector> BE_cal;
vector<Vector> Me_cal;
vector<Vector> Mi_cal;
//calculate average average AAR,CAAR,AAR_STD,CAAR_STD
calculation cal_be;
map<string, vector<Vector>> calcualtion_results;
//menu
//display items
//get operator
int oper = 0;
string stock_ticker;
string group;
int ticker_index = 0;
Vector daily_return;
Vector cum_daily_return;
int group_selection;
string group_name;
vector<string>::iterator itr1;
while (oper != 5) {
if (N == 0) {
oper = 1;
}
else {
showmenu();
cout << "Please select an option." << endl;
cin >> oper;
}
switch (oper)
{
default:
cout << "Please enter a valid choice." << endl;
break;
case 1:
cout << "Please enter N (N>=60):" << endl;
//int N;
cin >> N;
if (N < 60) {
cout << "Invalid N,automatically set to N=60." << endl;
N = 60;
}
curSymbols.clear();
valid_symbol.clear();
ARtable.clear();
stockReturn.clear();
benchmarkReturn.clear();
BeatEstvec.clear();
MeetEstEstvec.clear();
MissEstEstvec.clear();
BE_cal.clear();
Me_cal.clear();
Mi_cal.clear();
calcualtion_results.clear();
MissEst_group.clear();
MeetEst_group.clear();
BeatEst_group.clear();
sort_vec.clear();
get2N_days(ticker_date_map, N, price_map, benchmark_map, date_price, IWB_date_map);
itr = price_map.begin();
while (itr != price_map.end()) {
curSymbols.push_back(itr->first);
itr++;
}
//Check and erase stocks with less than 2N+1 days of data
//If valid, put symbol into valid_symbol and caluclate returns & AR
for (int i = 0; i < (int)curSymbols.size(); i++) {
curSymbol = curSymbols[i];
if ((int)price_map[curSymbol].size() < 2 * N + 1) {
cout << curSymbol + " days less than 2N + 1!" << endl;
price_map.erase(curSymbol);
benchmark_map.erase(curSymbol);
}
else {
valid_symbol.push_back(curSymbol);
stockReturn = calculateReturn(price_map[curSymbol]);
benchmarkReturn = calculateReturn(benchmark_map[curSymbol]);
ARtable[curSymbol] = stockReturn - benchmarkReturn;
}
}
/*vector<string> sort_vec;*/
for (int i = 0; i < (int)ticker.size(); i++) {
for (int j = 0; j < (int)valid_symbol.size(); j++) {
if (ticker[i] == valid_symbol[j]) {
sort_vec.push_back(ticker[i]);
}
}
}
//Split Valid Symbols into three groups
data.Split(sort_vec, MissEst_group, MeetEst_group, BeatEst_group);
//cout << to_string(MissEst_group.size()) << endl;
//cout << to_string(MeetEst_group.size()) << endl;
//cout << to_string(BeatEst_group.size()) << endl;
//resample data of three groups
pop_bs_vec(bootstrap, BeatEstvec, BeatEst_group, resample_time);
pop_bs_vec(bootstrap, MeetEstEstvec, MeetEst_group, resample_time);
pop_bs_vec(bootstrap, MissEstEstvec, MissEst_group, resample_time);
BE_cal = cal_be.calculate_all(BeatEstvec, 80, 2 * N, resample_time, ARtable);
Me_cal = cal_be.calculate_all(MeetEstEstvec, 80, 2 * N, resample_time, ARtable);
Mi_cal = cal_be.calculate_all(MissEstEstvec, 80, 2 * N, resample_time, ARtable);
//cout << Mi_cal.size() << endl;
calcualtion_results["BEAT ESTIMATE"] = BE_cal;
calcualtion_results["MEET ESTIMATE"] = Me_cal;
calcualtion_results["MISS ESTIMATE"] = Mi_cal;
break;
case 2:
cout << "Please enter stock ticker:" << endl;
cin >> stock_ticker;
itr1 = find(ticker.begin(), ticker.end(), stock_ticker);
if (itr1 != ticker.cend())
{
ticker_index = (int)distance(ticker.begin(), itr1);
if (find(MissEst_group.begin(), MissEst_group.end(), stock_ticker) != MissEst_group.end() && stock_ticker != "") { group = "MISS ESTIMATE"; }
else if (find(MeetEst_group.begin(), MeetEst_group.end(), stock_ticker) != MeetEst_group.end() && stock_ticker != "") { group = "MEET ESTIMATE"; }
else if (find(BeatEst_group.begin(), BeatEst_group.end(), stock_ticker) != BeatEst_group.end() && stock_ticker != "") { group = "BEAT ESTIMATE"; }
daily_return = calculateReturn(price_map[stock_ticker]);
cum_daily_return = calculateCumReturn(daily_return);
cout << "Ticker: " << stock_ticker << endl
<< "Group: " << group << endl
<< "Announce Date: " << announce_date[ticker_index] << endl
<< "Period End: " << prd_end[ticker_index] << endl
<< "EPS Esitmate: " << estimate[ticker_index] << endl
<< "EPS Actual: " << report_earn[ticker_index] << endl
<< "EPS Surprise: " << surprise[ticker_index] << endl
<< "EPS Surprise Percent: " << surprise_pct[ticker_index] << endl
<< endl;
cout << "Daily Prices: " << price_map[stock_ticker] << endl
<< "Cumulative Daily Returns: " << cum_daily_return << endl << endl;
}
else
{
cout << "Please enter a valid ticker. " << endl;
}
break;
case 3:
cout << "Please enter the group selection: " << endl
<< "1. Beat Estimate Group" << endl
<< "2. Meet Estimate Group" << endl
<< "3. Miss Estimate Group" << endl
<< "4. Go back to the previous menu" << endl;
cin >> group_selection;
if (group_selection == 1) group_name = "BEAT ESTIMATE";
else if (group_selection == 2) group_name = "MEET ESTIMATE";
else if (group_selection == 3) group_name = "MISS ESTIMATE";
else if (group_selection == 4) break;
else {
cout << "Please enter a valid choice." << endl;
break;
}
/*
switch (group_selection) {
case 1:
group_name = "BEAT ESTIMATE";
break;
case 2:
group_name = "MEET ESTIMATE";
break;
case 3:
group_name = "MISS ESTIMATE";
break;
case 4:
break;
default:
cout << "Please enter a valid choice." << endl;
break;
}*/
cout << "AAR: " << calcualtion_results[group_name][0] << endl
<< "CAAR: " << calcualtion_results[group_name][1] << endl
<< "AAR_STD: " << calcualtion_results[group_name][2] << endl
<< "CAAR_STD: " << calcualtion_results[group_name][3] << endl << endl;
break;
case 4:
cout << "Plotting CAAR with Gnuplot..." << endl;
plot_caar(calcualtion_results);
break;
case 5:
break;
}
}
return 0;
}