This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.h
557 lines (463 loc) · 17.9 KB
/
api.h
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/**
* API data is taken from IEX Trading, and Coinmarketcap.
* https://iextrading.com/developer/docs/
* https://coinmarketcap.com/api/documentation/v1
*/
#ifndef TICK_API_H
#define TICK_API_H
typedef enum api_providers {
API_PROVIDER_IEX, API_PROVIDER_ALPHAVANTAGE, API_PROVIDER_COINMARKETCAP, API_PROVIDER_MAX
} Api_Provider;
typedef enum data_level {
DATA_LEVEL_ALL, DATA_LEVEL_CHECK, DATA_LEVEL_MISC, DATA_LEVEL_NEWS, DATA_LEVEL_GRAPH
} Data_Level;
#define CELL_MAX_LENGTH 16
#define DATE_MAX_LENGTH 32
#define SYMBOL_MAX_LENGTH 32
#define SLUG_MAX_LENGTH 64
#define NAME_MAX_LENGTH 128
#define URL_MAX_LENGTH 2048
#define LONG_URL_MAX_LENGTH 65536
#define INFO_MAX_LENGTH 2048
#define NEWS_MAX_LENGTH 10500
#define KEY_MAX_LENGTH 256
#define QUARTERS 4
#define EMPTY (-999)
#define DAYS_TO_BUSINESS_DAYS_RATIO (365.25 / 251.89)
#include <stddef.h>
#include <curl/curl.h>
#include <json-c/json_tokener.h>
#include <pthread.h>
#include "utils.h"
typedef struct key_ring {
Api_Provider providers[API_PROVIDER_MAX];
char keys[API_PROVIDER_MAX][KEY_MAX_LENGTH];
} Key_Ring;
typedef struct ref_data {
char** slugs; // Sorted
char** names;
size_t* sorted_indices;
size_t length;
int64_t time_loaded;
} Ref_Data;
typedef struct news_article {
char headline[INFO_MAX_LENGTH];
char source[INFO_MAX_LENGTH];
char date[DATE_MAX_LENGTH];
char summary[INFO_MAX_LENGTH];
char url[URL_MAX_LENGTH];
char related[INFO_MAX_LENGTH];
} News;
typedef struct info_array Info_Array;
typedef struct info {
int api_provider; // IEX, ALPHAVANTAGE, COINMARKETCAP
char slug[SLUG_MAX_LENGTH]; // API call string -- ex. AAPL, DSENX, RIPPLE, ZCASH
/** API DATA **/
/* Company */
char symbol[SYMBOL_MAX_LENGTH]; // ex. AAPL
char name[NAME_MAX_LENGTH]; // ex. Apple Inc.
char industry[NAME_MAX_LENGTH]; // ex. Computer Hardware
char website[URL_MAX_LENGTH]; // ex. apple.com
char description[INFO_MAX_LENGTH]; // Paragraph description of company
char ceo[NAME_MAX_LENGTH]; // ex. Timothy D. Cook
char issue_type[3]; /* ad – American Depository Receipt (ADR’s)
re – Real Estate Investment Trust (REIT’s)
ce – Closed end fund (Stock and Bond Fund)
si – Secondary Issue
lp – Limited Partnerships
cs – Common Stock
et – Exchange Traded Fund (ETF)
(blank) = Not Available
*/
char sector[NAME_MAX_LENGTH]; // ex. Technology
/* Quote */
int64_t intraday_time; // Unix timestamp of current price
double price; // Current price of security in USD (ex. 174.54)
int64_t marketcap; // Market cap in USD (ex. 890489000000)
int64_t volume_1d; // Volume in shares of security (ex. 33812360)
double pe_ratio; // Price per earnings ratio
/* Stats */
double div_yield; // Percent dividend yield (ex. 1.46)
int64_t revenue; // Revenue in dollars
int64_t gross_profit; // Gross Profit in dollars
int64_t cash; // Cash in dollars
int64_t debt; // Debt in dollars
/* Earnings */
double eps[QUARTERS]; // Earnings per share per quarter for past four quarters
char fiscal_period[QUARTERS][DATE_MAX_LENGTH]; /* Quarter and Year of past four quarters
(ex. Q1 2018)
*/
double eps_year_ago[QUARTERS]; // Earnings per share per quarter for the previous year
/* Chart */
double price_last_close; // Last close price
double price_7d; // Price 7 days ago
double price_30d; // Price 30 days ago
double* points; // Array of closing prices
int num_points; // Number of data points
/* News */
News** articles; // Array of News pointers
int num_articles; // Number of News pointers in array
/* Peers */
Info_Array* peers; // Related securities
/** PORTFOLIO DATA **/
double amount; // Amount of security in porfolio
double total_spent; // Total USD spent
double current_value; // Total USD value
char famount[CELL_MAX_LENGTH]; // Amount of security in porfolio
char ftotal_spent[CELL_MAX_LENGTH]; // Total USD spent
char fcurrent_value[CELL_MAX_LENGTH]; // Total USD value
/** CALCULATED DATA **/
double profit_total; // Total profit
double profit_total_percent; // Total profit %
double profit_last_close; // Profit since last close
double profit_last_close_percent; // Profit since last close %
double profit_7d; // Profit since seven days ago
double profit_7d_percent; // Profit since seven days ago %
double profit_30d; // Profit since thirty days ago
double profit_30d_percent; // Profit since thirty days ago %
/** FORMATTING DATA **/
char fprofit_total[CELL_MAX_LENGTH]; // Total profit
char fprofit_total_percent[CELL_MAX_LENGTH]; // Total profit %
char fprofit_last_close[CELL_MAX_LENGTH]; // Profit since last close
char fprofit_last_close_percent[CELL_MAX_LENGTH]; // Profit since last close %
char fprofit_7d[CELL_MAX_LENGTH]; // Profit since seven days ago
char fprofit_7d_percent[CELL_MAX_LENGTH]; // Profit since seven days ago %
char fprofit_30d[CELL_MAX_LENGTH]; // Profit since thirty days ago
char fprofit_30d_percent[CELL_MAX_LENGTH]; // Profit since thirty days ago %
} Info;
struct info_array {
Info** array;
size_t length;
Info* totals;
};
extern char* ref_cache_file_path;
extern Ref_Data* ref_cache;
extern char* crypto_cache_file_path;
extern Ref_Data* crypto_cache;
extern char* keys_file_path;
extern Key_Ring* api_keys;
extern char api_abbreviations[API_PROVIDER_MAX][SYMBOL_MAX_LENGTH];
extern char api_names[API_PROVIDER_MAX][NAME_MAX_LENGTH];
extern char api_websites[API_PROVIDER_MAX][URL_MAX_LENGTH];
extern const char usd_slug[SLUG_MAX_LENGTH];
/**
* Sets global variable keys_file_path to "$HOME/.tick_api_keys.json"
*/
void keys_file_path_init(void);
/**
* Sets global variable ref_cache_file_path to "$HOME/.tick_ref_cache.json"
*/
void ref_cache_file_path_init(void);
/**
* Sets global variable crypto_cache_file_path to "$HOME/.tick_crypto_cache.json"
*/
void crypto_cache_file_path_init(void);
/**
* Allocated a Key_Ring struct with each index of providers set to enum api_provider. Each index
* of keys will be null terminated length 0.
* @return Key_Ring*
*/
Key_Ring* key_ring_init(void);
/**
* Allocates a Ref_Data struct with length and returns a pointer to it.
* @param length
* @return Ref_Data*
*/
Ref_Data* ref_data_init_length(size_t length);
/**
* Allocates a News struct and returns a pointer to it.
* @return News*
*/
News* news_init(void);
/**
* Allocates an Info struct and returns a pointer to it. All numbers are set to EMPTY, all strings are NULL
* terminated at length 0, and all pointers are set to NULL.
* @return Info*
*/
Info* info_init(void);
/**
* Allocates an Info_Array struct with length 0.
* @return Info_Array*
*/
Info_Array* info_array_init(void);
/**
* Allocates an Info_Array struct with allocated length.
* @param length number of Info pointers to allocate
* @return Info_Array*
*/
Info_Array* info_array_init_length(size_t length);
/**
* Appends a security to an Info_Array.
* @param pInfo_Array the Info_Array to append
* @param slug the symbol of the new security
*/
void info_array_append(Info_Array* pInfo_Array, const char* slug);
/**
* writefunction for cURL HTTP GET
* stolen from a nice man on stackoverflow
*/
size_t string_writefunc(void* ptr, size_t size, size_t nmemb, String* pString);
/**
* Performs a HTTP GET. Response data is stored and returned in an allocated String*.
* @param url API url to GET
* @return NULL if error or no response from server. Otherwise, String* containing data.
*/
String* api_curl_url(const char* url);
/**
* Calls IEX's batch API to store data in pInfo_Array. slug must be a valid string in each Info
* @param pInfo_Array the Info_Array to fill
* @param data_level the level of data to store
*/
void api_iex_store_info_array(Info_Array* pInfo_Array, Data_Level data_level);
/**
* Calls IEX's batch API to store data in pInfo. symbol must be a valid string in pInfo
* @param pInfo the Info to fill
* @param data_level the level of data to store
*/
void api_iex_store_info(Info* pInfo, Data_Level data_level);
/**
* This function will only store stock and ETF data.
* Queries several of IEX's endpoints for multiple symbols at a time to store the data in
* pInfo_Array. data_level will determine the endpoints queried:
* ALL -- quote, chart, company, stats, peers, news, earnings
* CHECK -- quote, chart
* MISC -- company, stats, peers, news, earnings
* @param pInfo_Array the Info_Array
* @param data_level endpoints to query
*/
String* api_iex_get_data_string(char** slug_array, size_t len,
Data_Level data_level);
/**
* Calls CMC's API to store data in pInfo_Array. slug must be a valid string in each Info
* @param pInfo_Array the Info_Array to fill
*/
void api_cmc_store_info_array(Info_Array* pInfo_Array);
void api_cmc_store_info(Info* pInfo);
/**
* Designed for threading
*
* Queries AlpvaVantage's API and stores the data in the Info object pointed to by vpInfo. change_1d,
* change_7d, change_30d, and points are stored.
* @param vpInfo Info*
* @return vpInfo on Success, NULL on error
*/
void* api_alphavantage_store_info(void* vpInfo);
/**
* Designed for threading
*
* Queries Coinmarketcaps's API and stores the data in the Info object pointed to by vpInfo. name, symbol, price,
* change_1d, change_7d, marketcap, and volume_1d are stored.
* @param vpInfo Info*
* @return vpInfo on success, NULL on error
*/
void* api_coinmarketcap_store_info(void* vpInfo);
/**
* Queries IEX, AlphaVantage, and Coinmarketcap to store api data in pInfo_Array. data_level will
* determine the level of data stored. See iex_batch_store_data for more information on data_level.
* @param pInfo_Array the Info_Array
* @param data_level the data level to store
*/
void api_store_info_array(Info_Array* pInfo_Array, Data_Level data_level);
/**
* Queries IEX, AlphaVantage, and Coinmarketcap to store api data in pInfo. data_level will
* determine the level of data stored. See iex_batch_store_data for more information on data_level.
* @param pInfo
* @param data_level
*/
void api_store_info(Info* pInfo, Data_Level data_level);
/**
* After API data and portfolio have already been collected, uses them to populate the Info fields current_value and
* all the profit fields.
* @param pInfo Info*
*/
void info_store_portfolio_data(Info* pInfo);
/**
* Adds up values of Info array and sets values in totals.
* @param pInfo_Array
*/
void info_array_store_totals(Info_Array* pInfo_Array);
/**
* Sets global variable api_keys to a valid Key_Ring object. Reads the keys file and loads the
* data into api_keys.
*/
void keys_init(void);
/**
* Writes a Key_Ring object to the api keys file in JSON format.
* @param keys Key_Ring object to write
*/
void key_ring_write(const Key_Ring* keys);
/**
* Returns a valid Key_Ring object containing data from the api key file.
* @return Key_Ring*
*/
Key_Ring* key_ring_read(void);
/**
* Adds a key to a Key_Ring object, then writes that to the api keys file.
* @param keys Key_Ring object to modify
* @param provider Api provider that the key corresponds to
* @param new_key the key string
*/
void key_ring_add_key(Key_Ring* keys, Api_Provider provider, const char new_key[KEY_MAX_LENGTH]);
/**
*curl "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?sort=name&limit=5000
* &CMC_PRO_API_KEY=%s"
*/
/**
* Sets global variable ref_cache to a valid Ref_Data object or NULL. It will first try reading
* the cache file. If it doesn't exist or hasn't been reloaded in a week, calls the api. If it does
* exist and has been reloaded in the past week, sets ref_cache to the cache file.
*/
void api_ref_cache_init(void);
/**
* Calls IEX's ref data api endpoint and writes the data in JSON format to
* $HOME/.tick_ref_cache.json
*/
void api_ref_data_write_cache(void);
/**
* Reads the ref cache file and returns a Ref_Data* containing its data if valid or NULL if not.
* @return
*/
Ref_Data* ref_data_read_cache(void);
void api_crypto_cache_init(void);
void api_crypto_cache_write(void);
Ref_Data* crypto_cache_read(void);
/**
* Stores the data found in IEX formatted jobj in the Ref_Data.
* @param pRef_Data
* @param jobj
*/
void ref_data_store_json_iex(Ref_Data* pRef_Data, const Json* jobj);
void ref_data_store_json_cmc(Ref_Data* pRef_Data, const Json* jobj);
/**
* Stores the data found in IEX formatted jobj in the Info_Array.
* @param pInfo_Array
* @param jobj
*/
void info_array_store_endpoints_json(Info_Array* pInfo_Array, const Json* jobj);
/**
* Stores the data found in IEX formatted jsymbol in the Info_Array.
* @param pInfo
* @param jslug
*/
void info_store_endpoints_json(Info* pInfo, const Json* jslug);
/**
* IEX quote endpoint.
* intraday_time, price, price_last_close, marketcap, volume_1d, and pe_ratio is stored.
* @param pInfo
* @param jquote
*/
void info_store_quote_json(Info* pInfo, const Json* jquote);
/**
* IEX chart endpoint.
* change_7d, change_30d, and points is stored.
* @param pInfo
* @param jchart
*/
void info_store_chart_json(Info* pInfo, const Json* jchart);
/**
* IEX company endpoint.
* symbol, name, industry, website, description, ceo, issue_type, and sector is stored.
* @param pInfo
* @param jcompany
*/
void info_store_company_json(Info* pInfo, const Json* jcompany);
/**
* IEX stats endpoint.
* div_yield, revenue, gross_profit, cash, and debt is stored.
* @param pInfo
* @param jstats
*/
void info_store_stats_json(Info* pInfo, const Json* jstats);
/**
* IEX peers endpoint.
* peers is stored.
* @param pInfo
* @param jpeers
*/
void info_store_peers_json(Info* pInfo, const Json* jpeers);
/**
* IEX news endpoint.
* articles and num_articles is stored.
* @param pInfo
* @param jnews
*/
void info_store_news_json(Info* pInfo, const Json* jnews);
/**
* IEX earnings endpoint.
* eps, fiscal_period, and eps_year_ago is stored.
* @param pInfo
* @param jearnings
*/
void info_store_earnings_json(Info* pInfo, const Json* jearnings);
void info_store_quote_cmc_json(Info* pInfo, const Json* jquote);
/**
* Searches through an Info_Array and returns a ponter to the Info which has the same symbol as
* the function's argument. If not found, returns NULL.
* @param pInfo_Array the Info_Array to search
* @param slug the symbol to match
* @return valid Info* or NULL if not found
*/
Info* info_array_find_slug(const Info_Array* pInfo_Array, const char* slug);
/**
* Recursive binary search function for Ref_Data. Returns the index of the security with the given
* symbol.
* @param pRef_Data the Ref_Data to search
* @param slug symbol of the security to find
* @param left left-most index to start search
* @param right right-most index to start search
* @return index of security if found, -1 if not found
*/
int ref_data_get_index_from_slug_bsearch(const Ref_Data* pRef_Data, const char* slug,
size_t left, size_t right);
/**
* Recursive binary search function for Ref_Data. Returns the index of the security with the given
* name.
* @param pRef_Data the Ref_Data to search
* @param name name of the security to find
* @param left left-most index to start search
* @param right right-most index to start search
* @return index of security if found, -1 if not found
*/
int ref_data_get_index_from_name_bsearch(const Ref_Data* pRef_Data, const char* name,
size_t left, size_t right);
/**
* Recursively searches pInfo_Array and pInfo_Array->peers to find an Info* with the given symbol
* @param pInfo_Array the Info_Array to search
* @param slug the symbol to find
* @return Info* of found symbol or NULL if not found
*/
Info* info_array_find_slug_recursive(const Info_Array* pInfo_Array, const char* slug);
/**
* Reallocates pInfo->points with size trading days. Moves all values to end of the array and sets
* values not initialized as EMPTY.
* @param pInfo the Info to modify
* @param trading_days the size to realloc
*/
void info_chart_fill_empty(Info* pInfo, int trading_days);
void ref_data_quicksort(Ref_Data* pRef_Data, size_t start, size_t len);
/**
* Destroys Key_Ring object and frees memory. Sets the pointer of the Key_Ring to NULL
* @param phKeys
*/
void key_ring_destroy(Key_Ring** phKeys);
/**
* Destroys Ref_Data object and frees memory. Sets the pointer of the Ref_Data to NULL
* @param phRef_Data the Ref_Data to destroy
*/
void ref_data_destroy(Ref_Data** phRef_Data);
/**
* Destroys News object and frees memory. Sets the pointer of the News to NULL
* @param phNews the News to destroy
*/
void news_destroy(News** phNews);
/**
* Destroys Info object and frees memory. Sets the pointer to the Info to NULL
* @param phInfo the Info to destroy
*/
void info_destroy(Info** phInfo);
/**
* Destroys Info_Array object and frees memory. Sets the pointer to the Info to NULL
* @param phInfo_Array the Info_Array to destroy
*/
void info_array_destroy(Info_Array** phInfo_Array);
#endif