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
|
#include "graph.h"
int zoom_months[9], zoom_change_x_months[9];
void graph_main(const char* ticker_name_string) {
double* price_data = api_get_hist_5y(ticker_name_string);
if (price_data == NULL) // If invalid symbol or cryptocurrency
RET_MSG("Invalid symbol.")
int temp[] = {60, 48, 36, 24, 12, 9, 6, 3, 1};
memcpy(zoom_months, temp, sizeof(zoom_months));
int temp2[] = {12, 12, 12, 12, 12, 3, 3, 3, 2};
memcpy(zoom_change_x_months, temp2, sizeof(zoom_change_x_months));
initscr();
noecho(); // Don't echo keystrokes
keypad(stdscr, TRUE); // Enables extra keystrokes
curs_set(0); // Hides cursor
time_t now = time(NULL);
struct tm today_date = *localtime(&now), start_date = today_date, furthest_back_date = today_date, end;
start_date.tm_year -= 5, furthest_back_date.tm_year -= 5;
int ch, zoom = ZOOM_5y;
double seconds = difftime(mktime(&today_date), mktime(&furthest_back_date));
int trading_days = (int) ((1.0 / DAYS_TO_BUSINESS_DAYS_RATIO) * seconds / 86400.0); // Total trading days to print
int total_data_points = 0;
for (int i = 0; price_data[i] != '\0'; i++)
total_data_points++;
int difference = trading_days - total_data_points;
if (difference > 0) // On initial print, scrub to make sure that there are 5 years worth of data points
price_data = graph_fill_empty(price_data, total_data_points, trading_days);
graph_print(price_data, &start_date, zoom); // Initial graph of 5 year history
while (1) { // Main input loop
ch = getch();
if (ch == 'q') // Quits program on "q"
break;
if ((ch == KEY_UP && zoom != ZOOM_1m) || (ch == KEY_DOWN && zoom != ZOOM_5y) ||
(zoom != ZOOM_5y && (ch == KEY_LEFT || ch == KEY_RIGHT))) { // UP / DOWN / LEFT / RIGHT
if (ch == KEY_UP) {
zoom++;
start_date.tm_mon += zoom_change_x_months[zoom];
} else if (ch == KEY_RIGHT || ch == KEY_DOWN) {
if (ch == KEY_DOWN)
zoom--;
if (zoom == ZOOM_1m)
start_date.tm_mon += 1; // Moves only one month if zoom is 1 month
else start_date.tm_mon += zoom_change_x_months[zoom];
end = start_date; // If trying to go past current date, set start to (zoom level - current date)
end.tm_mon += zoom_months[zoom];
if (difftime(mktime(&end), mktime(&today_date)) > 0) {
start_date = today_date;
start_date.tm_mon -= zoom_months[zoom];
}
} else { // LEFT
if (zoom == ZOOM_1m)
start_date.tm_mon -= 1; // Moves only one month if zoom is 1 month
else start_date.tm_mon -= zoom_change_x_months[zoom];
if (difftime(mktime(&start_date), mktime(&furthest_back_date)) < 0)
start_date = furthest_back_date; // Can't go back past furthest_date
}
graph_print(price_data, &start_date, zoom);
}
}
endwin();
free(price_data);
}
void graph_print(const double* points, struct tm* start_time, int zoom) {
move(0, 0); // Instead of clear()ing, move to the top left corner and re-print
int cols, rows;
getmaxyx(stdscr, rows, cols);
cols -= 11; // 10 offset to give space for graph labels + 1 for right side
rows -= rows % ROWS_SPACING; // Round down to multiple of 5
if (cols < 10 || rows < 10) // Exits if the terminal is too small
RET_MSG("Terminal not large enough.")
time_t now = time(NULL);
struct tm end_date = *start_time, * five_y = localtime(&now);
end_date.tm_mon += zoom_months[zoom], five_y->tm_year -= 5;
double seconds = difftime(mktime(&end_date), mktime(start_time));
int trading_days = (int) ((1.0 / DAYS_TO_BUSINESS_DAYS_RATIO) * seconds / 86400.0); // Total trading days to print
seconds = difftime(mktime(start_time), mktime(five_y));
int starting_index = (int) ((1.0 / DAYS_TO_BUSINESS_DAYS_RATIO) * seconds / 86400.0);
double max = points[starting_index], min = points[starting_index];
int k = 0;
while (max == EMPTY) // If initial max is EMPTY, get first non-EMPTY value
max = points[++k];
if (k > 0) // Do the same thing with min
min = max;
for (int i = starting_index + 1; i < trading_days + starting_index; i++) {
if (points[i] != EMPTY) { // Ignore EMPTY values
if (points[i] > max) // Find max and min values for graph upper/lower bounds
max = points[i];
if (points[i] < min)
min = points[i];
}
}
double line_diff = (max - min) / rows, dat; // Each line includes data point up to line_diff below
for (int i = rows; i >= 0; i--) {
if (i % ROWS_SPACING == 0) // Print y-axis price labels with width 10
printw("%9.2lf ", (max - ((rows - i) * line_diff)));
else printw(" "); // Indent width 10
for (int j = 0; j < cols; j++) {
dat = points[starting_index + (int) ((double) j * trading_days / cols)];
if (dat <= (max - ((rows - i) * line_diff)) && dat > (min + ((i - 1) * line_diff)))
addch(ACS_DIAMOND);
else if (i % ROWS_SPACING == 0 && j % COLS_SPACING == 0) // Cross on corners
addch(ACS_PLUS);
else if (i % ROWS_SPACING == 0) // Horizontal line every ROWS_SPACING lines
addch(ACS_HLINE);
else if (j % COLS_SPACING == 0) // Vertical line every COLS_SPACING lines
addch(ACS_VLINE);
else // Otherwise prints a space
addch(' ');
}
addch('\n'); // Newline on line end
}
printw(" "); // Use to center date labels
char time_string[16];
double x = (DAYS_TO_BUSINESS_DAYS_RATIO * trading_days) / (cols / COLS_SPACING);
struct tm copy = *start_time;
for (int i = 0; i < cols; i++) {
if (i % (2 * COLS_SPACING) == 0 && cols - i > 5) { // Print x-axis date labels every two
if (i != 0)
copy.tm_sec += x * 2.0 * 86400.0;
mktime(©);
strftime(time_string, 16, "%m/%d/%Y", ©);
printw("%s ", time_string); // Width 2 * COLS_SPACING
}
}
printw("\n\n"); // Empty line as spacing
for (int i = 0; i < cols / 2 - 8; i++)
addch(' '); // Center text
const char* str[9] = {"5y", "4y", "3y", "2y", "1y", "9m", "6m", "3m", "1m"}; // Zoom level
for (int i = 0; i < 9; i++) {
if (zoom == i)
attron(A_STANDOUT); // Highlight current zoom level
addstr(str[i]);
attroff(A_STANDOUT);
addch(' ');
}
}
double* graph_fill_empty(double* points, int size, int trading_days) {
int difference = trading_days - size;
points = realloc(points, (size_t) sizeof(double) * (trading_days + 1)); // Realloc for number of trading days
pointer_alloc_check(points);
points[trading_days] = '\0';
memmove(&points[difference], points, sizeof(double) * size); // Move points to end
for (int i = 0; i < difference; i++) // Initialize newly allocated bytes as EMPTY
points[i] = EMPTY;
return points;
}
|