aboutsummarylogtreecommitdiffstats
path: root/api.h
blob: a8ac36745fe3a41a3cdbfa1669ab753f1f893c39 (plain)
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
/**
 * API data is taken from IEX Trading, Morningstar, Coinmarketcap.
 * https://iextrading.com/developer/docs/
 * http://www.morningstar.com/
 * https://coinmarketcap.com/api/
 */

#ifndef API_H
#define API_H

#define QUARTERS 4
#define DATE_MAX_LENGTH 16
#define SYMBOL_MAX_LENGTH 32
#define URL_MAX_LENGTH 2048
#define INFO_TEXT_MAX 2048
#define EMPTY (-999)

#include <stddef.h>
#include <curl/curl.h>
#include <json-c/json_tokener.h>
#include <pthread.h>
#include "string-tick.h"

typedef struct info {
    /* Company */
    char symbol[SYMBOL_MAX_LENGTH]; // Symbol of security (ex. AAPL)
    char name[INFO_TEXT_MAX]; // Name of security (ex. Apple Inc.)
    char industry[INFO_TEXT_MAX];
    char website[URL_MAX_LENGTH];
    char description[INFO_TEXT_MAX]; // Description of company
    char ceo[INFO_TEXT_MAX];
    char issue_type[3];
    char sector[INFO_TEXT_MAX];

    /* Quote */
    int64_t intraday_time; // unix time
    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;

    /* Stats */
    double div_yield; // Percent dividend yield (ex. 1.46)
    int64_t revenue;
    int64_t gross_profit;
    int64_t cash;
    int64_t debt;

    /* Earnings */
    double eps[QUARTERS];
    char fiscal_period[QUARTERS][DATE_MAX_LENGTH];
    double eps_year_ago[QUARTERS];

    /* Chart */
    double change_1d, change_7d, change_30d; // Percent change in the past x days (ex. -7.49)
    double* points; // 5y
} Info;

/**
 * Creates and returns an Info object. name and symbol are allocated 64 bytes. the doubles and long are set to EMPTY
 * @return Info object
 */
Info* api_info_init(void);

/**
 * writefunction for cURL HTTP GET/POST
 * stolen from a nice man on stackoverflow
 */
size_t api_string_writefunc(void* ptr, size_t size, size_t nmemb, String* pString);

/**
 * If post_field is not NULL, performs a HTTP POST with the given parameters. Otherwise, performs a HTTP GET. Response
 * data is stored and returned in a String.
 * @param url API url to GET/POST
 * @return NULL if no response from server. Otherwise, String containing data.
 */
String* api_curl_data(const char* url);

void* iex_store_company(void* vpInfo);

void* iex_store_quote(void* vpInfo);

void* iex_store_stats(void* vpInfo);

void* iex_store_earnings(void* vpInfo);

void* iex_store_chart(void* vpInfo);

void* morningstar_store_info(void* vpInfo);

void* coinmarketcap_store_info(void* vpInfo);

/**
 * Returns a pointer to an Info object containing info pertaining
 * to the given symbol with data from IEX.
 * @param symbol stock/etf symbol
 * @return Info object
 */
Info* iex_get_info(const char* symbol);

/**
 * Returns a pointer to an Info object containing info pertaining
 * to the given symbol with data from Morningstar
 * @param symbol mutf/otc symbol
 * @return Info object
 */
Info* morningstar_get_info(const char* symbol);

/**
 * Returns a pointer to an Info object containing info pertaining to the given cryptocurrency from Coinmarketcap.
 * @param symbol the crypto's name
 * @return Info object
 */
Info* coinmarketcap_get_info(const char* symbol);

/**
 * Returns a double* containing the current price and yesterday's price of a stock or cryptocurrency.
 * [0] = current intraday price
 * [1] = last close price
 * [2] = close price 5 trading days previously
 * The function will first query IEX. If no response, it will query Morningstar. If no response, it will query
 * Coinmarketcap. If Coinmarketcap returns NULL, the function returns NULL.
 * 1. IEX -- NASDAQ/NYSE/NYSEARCA
 * 2. Morningstar -- MUTF/OTCMKTS
 * 3. Coinmarketcap -- CRYPTO
 * @param symbol security symbol or name to get prices of
 * @return price data or NULL if invalid symbol
 */
Info* api_get_check_info(const char* symbol);

Info* api_get_info(const char* symbol);

/**
 * Destroys Info object and frees memory. Sets the pointer to the Info to NULL
 * @param phInfo the Info to destroy
 */
void api_info_destroy(Info** phInfo);

#endif