-
Notifications
You must be signed in to change notification settings - Fork 0
/
transactioncounter.c
513 lines (486 loc) · 14.2 KB
/
transactioncounter.c
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#include "date.h"
#include "dividenddownload.h"
#include "genlib.h"
#include "investment.h"
#include "stockcounter.h"
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// see transactioncounter.h for documentation
// ALL FUNCTIONS REQUIRE VALID POINTERS
// transaction types
static const int BUY = 1;
static const int SELL = 2;
static const int DIV = 3;
static const char *S_BUY = "Buy";
static const char *S_SELL = "Sell";
static const char *S_DIV = "Dividend";
static const char *transaction_fname = "transactioncounter.csv";
static const char *tempname = "transactiontemp.txt";
// prints the line of the transaction
// if type is DIV, n is the number of shares owned, and p is the dividend per
// share if type is not DIV, ex_d can be NULL requires: fp is open to write
// ticker is a null-terminated string (not asserted)
// n, p > 0
static void transaction_print(FILE *fp, const char *ticker, int n, double p,
struct date *d, int type, struct date *ex_d) {
assert(fp);
assert(ticker);
assert(d);
assert(p > 0);
assert(n > 0);
char *date = malloc(DATE_LENGTH * sizeof(char));
date_string(d, date);
fprintf(fp, "%s,%s ,", date, ticker);
free(date);
if (type == BUY)
fprintf(fp, "%s", S_BUY);
else if (type == SELL) {
fprintf(fp, "%s", S_SELL);
n = -n;
} else
fprintf(fp, "%s", S_DIV);
fputc(',', fp);
if (ex_d) {
char *ex_date = malloc(DATE_LENGTH * sizeof(char));
date_string(ex_d, ex_date);
fprintf(fp, "%s", ex_date);
free(ex_date);
}
fputc(',', fp);
fprintf(fp, "%lf,%d,%lf\n", p, n, p * n);
}
// reads a line of fp and assigns the column valules to the pointers
// then returns the fp pointer to where it started
// ALL values can be NULL except for fp
// requires: all date pointers must be either NULL or assigned to a date
static void transaction_read(FILE *fp, struct date **d, char *ticker, int *type,
struct date **ex_d, double *price, int *shares,
double *total_amount) {
assert(fp);
fpos_t position;
fgetpos(fp, &position);
char c;
if (d) {
char date_s[DATE_LENGTH];
fgets(date_s, DATE_LENGTH, fp);
if (*d) {
date_destroy(*d);
*d = NULL;
}
*d = string_to_date(date_s);
}
while (fgetc(fp) != ',')
;
if (ticker) {
fscanf(fp, "%s", ticker);
}
while (fgetc(fp) != ',')
;
if (type) {
c = fgetc(fp);
if (c == S_BUY[0])
*type = BUY;
else if (c == S_SELL[0])
*type = SELL;
else
*type = DIV;
}
while (fgetc(fp) != ',')
;
if (ex_d) {
// we still want to check if there is an ex_d here, because sometimes we
// will need to save the date without knowing if the transaction is a
// dividend or not
fpos_t pos2;
fgetpos(fp, &pos2);
c = fgetc(fp);
fsetpos(fp, &pos2);
if (c != ',') {
char ex_date_s[DATE_LENGTH];
fgets(ex_date_s, DATE_LENGTH, fp);
if (*ex_d) {
date_destroy(*ex_d);
*ex_d = NULL;
}
*ex_d = string_to_date(ex_date_s);
} else {
if (*ex_d) {
date_destroy(*ex_d);
*ex_d = NULL;
}
}
}
while (fgetc(fp) != ',')
;
if (price)
fscanf(fp, "%lf", price);
while (fgetc(fp) != ',')
;
if (shares)
fscanf(fp, "%d", shares);
while (fgetc(fp) != ',')
;
if (total_amount)
fscanf(fp, "%lf", total_amount);
fsetpos(fp, &position);
}
// copies the header lines of r, the csv file, into w
// if w is NULL it'll just skip through r
// requires: r is open to read
static void copy_header(FILE *r, FILE *w) {
assert(r);
const int NUM_HEADER_LINES = 2;
char c;
int count = 0;
while (count < NUM_HEADER_LINES) {
c = fgetc(r);
if (w)
fputc(c, w);
if (c == '\n')
count++;
}
}
// replaces the top line of the csv with a new CAGR and total value
// requires: cagr, tot > 0
static void update_header(const char *cur_fname, double cagr, double tot) {
assert(tot > 0);
assert(cagr > 0);
FILE *fp = fopen(cur_fname, "r");
FILE *temp = fopen(tempname, "w");
while (fgetc(fp) != '\n')
;
cagr -= 1;
cagr *= 100;
fprintf(temp, "CAGR: %lf%%, Total Portfolio Value: $%lf\n", cagr, tot);
while (!next_eof(fp))
fputc(fgetc(fp), temp);
fclose(fp);
fclose(temp);
remove(cur_fname);
rename(tempname, cur_fname);
}
// builds the counter csv file from scratch from the existing transactions csv
// file
static void build_counter(bool update_price) {
counter_reset();
FILE *fp = fopen(transaction_fname, "r");
if (!fp)
return;
copy_header(fp, NULL);
int num_shares;
char ticker[MAX_TICKER_LENGTH];
int type;
while (!next_eof(fp)) {
transaction_read(fp, NULL, (char *)ticker, &type, NULL, NULL, &num_shares,
NULL);
if (type != DIV)
counter_change_shares((const char *)ticker, abs(num_shares),
num_shares > 0, update_price);
while (fgetc(fp) != '\n')
;
}
fclose(fp);
}
int transaction_execute(const char *ticker, int n, double p, struct date *d,
bool buy) {
assert(n > 0);
assert(p > 0);
assert(ticker);
assert(d);
int type;
if (buy)
type = BUY;
else
type = SELL;
// checks counter to see if you can sell
build_counter(false);
int ret = counter_num_shares(ticker);
if (ret == TICKER_NOT_FOUND && !buy)
return TICKER_NOT_FOUND;
if (n > ret && !buy)
return NOT_ENOUGH_SHARES;
FILE *fp;
char ticker_transaction_fname[MAX_TICKER_LENGTH + 4];
const char *cur_fname;
strcpy(ticker_transaction_fname, ticker);
strcat(ticker_transaction_fname, ".csv");
for (int i = 0; i < 2; i++) {
if (i == 0)
cur_fname = transaction_fname;
else
cur_fname = ticker_transaction_fname;
fp = fopen(cur_fname, "r");
// if there's no file then we create one with the header
if (!fp) {
fp = fopen(cur_fname, "w");
fprintf(fp, "CAGR:,Total Portfolio "
"Value:\nDate,Ticker,Transaction_Type,Ex_Dividend_Date,Price/"
"Amount,Shares,Total_Amount\n");
transaction_print(fp, ticker, n, p, d, type, NULL);
fclose(fp);
build_counter(false);
if (i == 0)
continue;
return SUCCESS;
}
FILE *temp = fopen(tempname, "w");
copy_header(fp, temp);
// keep going until all the dates earlier than this transaction are passed,
// so the csv is in chronological order
int found_date = 0;
int num_shares = 0;
int cur_num_shares = 0;
struct date *cur_d = NULL;
char cur_tick[MAX_TICKER_LENGTH];
int cur_type;
char c;
while (!next_eof(fp)) {
transaction_read(fp, &cur_d, (char *)cur_tick, &cur_type, NULL, NULL,
&cur_num_shares, NULL);
if (!found_date && date_compare(cur_d, d) > 0) {
if (!buy && n > num_shares) {
date_destroy(cur_d);
fclose(fp);
fclose(temp);
remove(tempname);
build_counter(false);
return NOT_ENOUGH_SHARES;
}
found_date = 1;
transaction_print(temp, ticker, n, p, d, type, NULL);
}
// if the tickers match, we have to update num_shares
if (strcmp(ticker, (const char *)cur_tick) == 0)
num_shares += cur_num_shares;
c = fgetc(fp);
while (c != '\n') {
fputc(c, temp);
c = fgetc(fp);
}
fputc('\n', temp);
}
if (!found_date) {
if (!buy && n > num_shares) {
date_destroy(cur_d);
fclose(fp);
fclose(temp);
remove(tempname);
build_counter(false);
return NOT_ENOUGH_SHARES;
}
transaction_print(temp, ticker, n, p, d, type, NULL);
}
date_destroy(cur_d);
fclose(fp);
fclose(temp);
remove(cur_fname);
rename(tempname, cur_fname);
build_counter(false);
}
return SUCCESS;
}
// adds a dividend line for ticker on date d
// requires: div > 0
// ticker is a null-terminated string (not asserted)
static void transaction_dividend(const char *ticker, double div, struct date *d,
struct date *ex_d) {
assert(ticker);
assert(d);
assert(ex_d);
assert(div > 0);
char ticker_transaction_fname[MAX_TICKER_LENGTH + 4];
const char *cur_fname;
strcpy(ticker_transaction_fname, ticker);
strcat(ticker_transaction_fname, ".csv");
for (int i = 0; i < 2; i++) {
if (i == 0)
cur_fname = transaction_fname;
else
cur_fname = ticker_transaction_fname;
FILE *fp = fopen(cur_fname, "r");
if (!fp)
return;
FILE *temp = fopen(tempname, "w");
copy_header(fp, temp);
// keep going until all the dates earlier than this transaction are passed,
// and count how many stocks of ticker are owned
int found_date = 0;
int num_shares = 0;
int cur_num_shares = 0;
char c;
struct date *cur_d = NULL;
char cur_tick[MAX_TICKER_LENGTH];
int cur_type;
while (!next_eof(fp)) {
transaction_read(fp, &cur_d, (char *)cur_tick, &cur_type, NULL, NULL,
&cur_num_shares, NULL);
if (!found_date && date_compare(cur_d, d) > 0) {
found_date = 1;
if (num_shares > 0)
transaction_print(temp, ticker, num_shares, div, d, DIV, ex_d);
} else if (cur_type == DIV && date_compare(cur_d, d) == 0) {
date_destroy(cur_d);
fclose(fp);
fclose(temp);
remove(tempname);
return;
}
// if the tickers match and the transaction isn't a dividend, we have to update num_shares
if (strcmp(ticker, (const char *)cur_tick) == 0 && cur_type != DIV)
num_shares += cur_num_shares;
c = fgetc(fp);
while (c != '\n') {
fputc(c, temp);
c = fgetc(fp);
}
fputc('\n', temp);
}
if (!found_date && next_eof(fp) && num_shares > 0)
transaction_print(temp, ticker, num_shares, div, d, DIV, ex_d);
date_destroy(cur_d);
fclose(fp);
fclose(temp);
remove(cur_fname);
rename(tempname, cur_fname);
}
}
int transaction_stock_split(const char *ticker, int n, bool reverse) {
assert(ticker);
assert(n >= 1);
char c;
int shares;
double price;
bool found = 0;
struct date *d = NULL;
int type;
char cur_tick[MAX_TICKER_LENGTH];
char ticker_transaction_fname[MAX_TICKER_LENGTH + 4];
const char *cur_fname;
strcpy(ticker_transaction_fname, ticker);
strcat(ticker_transaction_fname, ".csv");
for (int i = 0; i < 2; i++) {
d = NULL;
if (i == 0)
cur_fname = transaction_fname;
else
cur_fname = ticker_transaction_fname;
FILE *fp = fopen(cur_fname, "r");
if (!fp)
return TICKER_NOT_FOUND;
FILE *temp = fopen(tempname, "w");
copy_header(fp, temp);
while (!next_eof(fp)) {
transaction_read(fp, &d, (char *)cur_tick, &type, NULL, &price, &shares,
NULL);
if (strcmp((const char *)cur_tick, ticker) == 0) {
found = 1;
if (reverse) {
shares /= n;
price *= n;
} else {
shares *= n;
price /= n;
}
transaction_print(temp, ticker, abs(shares), price, d, type, NULL);
while (fgetc(fp) != '\n')
;
} else {
c = fgetc(fp);
while (c != '\n') {
fputc(c, temp);
c = fgetc(fp);
}
fputc('\n', temp);
}
}
date_destroy(d);
fclose(fp);
fclose(temp);
remove(cur_fname);
rename(tempname, cur_fname);
}
build_counter(false);
if (!found)
return TICKER_NOT_FOUND;
return SUCCESS;
}
// calculates the cagr, adds it to the csv, and also returns the cagr,
// i.e. 1.15 for 15%
double calculate_cagr() {
double cagr = 1;
// adds the dividends to the transaction csv
build_counter(true);
struct tickerlist *tl = tickerlist_create();
int len = tickerlist_size(tl);
struct date *ex_d = NULL;
struct date *d = NULL;
double div;
const char *ticker;
FILE *div_fp;
for (int i = 0; i < len; i++) {
ticker = tickerlist_nth(tl, i);
dividend_history_download(ticker);
div_fp = dividend_open();
while (!next_eof(div_fp)) {
dividend_read_line(div_fp, &d, &ex_d, &div);
transaction_dividend(ticker, div, d, ex_d);
}
fclose(div_fp);
dividend_delete();
}
if (d)
date_destroy(d);
if (ex_d)
date_destroy(ex_d);
char ticker_transaction_fname[MAX_TICKER_LENGTH + 4];
const char *cur_fname;
const char *cur_ticker;
FILE *fp;
for (int j = 0; j <= len; j++) {
if (j == len)
cur_fname = transaction_fname;
else {
cur_ticker = tickerlist_nth(tl, j);
strcpy(ticker_transaction_fname, cur_ticker);
strcat(ticker_transaction_fname, ".csv");
cur_fname = ticker_transaction_fname;
}
// adds everything from the csv to the investment
fp = fopen(cur_fname, "r");
// if there's no file we return a default cagr
if (!fp) {
return cagr;
}
copy_header(fp, NULL);
struct investment *i = investment_create();
double amount;
int type;
struct date *cur_d = NULL;
while (!next_eof(fp)) {
transaction_read(fp, &cur_d, NULL, &type, NULL, NULL, NULL, &amount);
if (type == BUY)
investment_buy(amount, i, cur_d);
if (type == SELL)
investment_sell(amount * -1, i, cur_d);
if (type == DIV)
investment_add_dividend(amount, i);
while (fgetc(fp) != '\n')
;
}
date_destroy(cur_d);
fclose(fp);
if (j == len)
investment_final_value(counter_total_value(), i);
else
investment_final_value(counter_ticker_value(cur_ticker), i);
cagr = investment_cagr(i);
double tot = investment_get_total(i);
investment_destroy(i);
update_header(cur_fname, cagr, tot);
}
tickerlist_destroy(tl);
return cagr;
}