-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathiex.q
207 lines (147 loc) · 6.49 KB
/
iex.q
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
/
#########################################################################################
# Author : Himanshu Gupta
# Description: IEX is a new exchange that is slowly becoming popular. It provides a lot
# of its data for free through its API. This code is a q/kdb+ wrapper to make it easier
# to get data from IEX.
# IEX api URL: https://iextrading.com/developer/docs/#getting-started
#########################################################################################
\
main_url: "api.iextrading.com";
prefix: "HTTP/1.0\r\nhost:www.",main_url,"\r\n\r\n";
/ Function for converting epoch time
convert_epoch:{"p"$1970.01.01D+1000000j*x};
/ Function for issuing GET request and getting the data in json format
get_data:{[main_url;suffix;prefix;char_delta;identifier]
result: (`$":https://",main_url) suffix," ",prefix;
(char_delta + first result ss identifier) _ result
}
/ get last trade data for one or multiple securities
/ q)get_last_trade`aapl`ibm
/ sym price size time
/ ----------------------------------------------
/ AAPL 174.66 100 2017.11.10D20:59:58.008999936
/ IBM 149.18 300 2017.11.10D20:59:59.724999936
get_last_trade:{[syms]
/ This function can run for multiple securities.
syms:$[1<count syms;"," sv string(upper syms);string(upper syms)];
/ Construct the GET request
suffix: "GET /1.0/tops/last?symbols=",syms;
/ Parse json response and put into table
data:.j.k get_data[main_url;suffix;prefix;-3;"symbol"];
`sym xcol update symbol:`$symbol, time:"P"$string(convert_epoch time) from data
}
/ Get previous day summary for a security - high, low, open, close, vwap etc
/ q)get_prev_day_summary`aapl
get_prev_day_summary:{[sym]
sym:string(upper sym);
suffix: "GET /1.0/stock/",sym,"/previous";
/ Parse json response and put into table
data: enlist .j.k get_data[main_url;suffix;prefix;-2;"symbol"];
`sym xcol update symbol:`$symbol, date:"D"$date from data
}
/ Get bucketed data for a security for different periods
/ Available buckets are:
/ 1d - 1 day
/ 1m - 1 month
/ 3m - 3 months
/ 6m - 6 months
/ ytd - Year-to-date
/ 1y - 1 year
/ 2y - 2 years
/ 5y - 5 years
/ q)get_historical_summary[`aapl;`1m]
get_historical_summary:{[sym;period]
sym:string(upper sym);
period:string(period);
suffix: "GET /1.0/stock/",sym,"/chart/",period;
/ Remove any text from response before 'minute' if period is 1d and 'date' otherwise
txt:$[all "1d"=period;"minute";"date"];
/ Parse json response and put into table
data:.j.k "[", get_data[main_url;suffix;prefix;-2;txt];
/ data has different schema for 1d vs other buckets
$[all "1d"=period;
`sym`minute xcols update sym:`$sym,minute:"U"$minute from data;
`sym`date xcols update sym:`$sym, date:"D"$date from data]
}
/ Same as get_historical_summary but for a specific date
/ q)get_minutely_summary[`aapl;`20171103]
get_minutely_summary:{[sym;date]
sym:string(upper sym);
date:string(date);
suffix: "GET /1.0/stock/",sym,"/chart/date/",date;
/ Parse json response and put into table
data:.j.k "[",get_data[main_url;suffix;prefix;-2;"minute"];
`sym`date`minute xcols update sym:`$sym, date:"D"$date, minute:"U"$minute from data
}
/ Get information about a company such as exchange, industry, website, description, CEO etc
/ q)get_company_info`aapl
get_company_info:{[sym]
sym:string(upper sym);
suffix: "GET /1.0/stock/",sym,"/company";
/ Parse json response and put into table
data: enlist .j.k get_data[main_url;suffix;prefix;-2;"symbol"];
/ Rename symbol to sym
`sym xcol update symbol:`$symbol from data
}
/ Get key stats about a company such as market cap, beta, revenue, debt etc
/ q)get_key_stats`aapl
get_key_stats:{[sym]
sym:string(upper sym);
suffix: "GET /1.0/stock/",sym,"/stats";
/ Parse json response and put into table
data: enlist .j.k get_data[main_url;suffix;prefix;-2;"companyName"];
/ Update data types and rename symbol column to sym
`sym xcol `symbol xcols update latestEPSDate:"D"$latestEPSDate, shortDate:"D"$shortDate, exDividendDate:"D"$exDividendDate, symbol:`$symbol from data
}
/ Get news relating to a company
/ q)get_company_news`aapl
get_company_news:{[sym]
sym:string(upper sym);
suffix: "GET /1.0/stock/",sym,"/news";
/ Parse json response and put into table
data:.j.k "[",get_data[main_url;suffix;prefix;-2;"datetime"];
`sym xcols update sym:`$sym, datetime:"P"$datetime from data
}
/ Get financial information of a company such as report date, gross profit, net income etc
/ q)get_company_financials`aapl
get_company_financials:{[sym]
sym:string(upper sym);
suffix: "GET /1.0/stock/",sym,"/financials";
/ Parse json response and put into table
data:.j.k get_data[main_url;suffix;prefix;-2;"symbol"];
`sym xcols update sym:`$sym, reportDate:"D"$reportDate from data[`financials]
}
/ Get earnings information of a company such as actual EPS, consensus EPS, estimated EPS etc
/ q)get_company_earnings`aapl
get_company_earnings:{[sym]
sym:string(upper sym);
suffix: "GET /1.0/stock/",sym,"/earnings";
/ Parse json response and put into table
data:.j.k get_data[main_url;suffix;prefix;-2;"symbol"];
`sym xcols update sym:`$sym, EPSReportDate:"D"$EPSReportDate, fiscalEndDate:"D"$fiscalEndDate from data[`earnings]
}
/ Get most 'active' stocks for last trade date with additional info such as close price, open price etc
/ q)get_most_active_stocks[]
get_most_active_stocks:{
predefined_iex_lists_data[`mostactive]
}
/ Get stocks with highest gains for last trade date with additional info such as close price, open price etc
/ q)get_most_gainers_stocks[]
get_most_gainers_stocks:{
predefined_iex_lists_data[`gainers]
}
/ Get stocks with most loss for last trade date with additional info such as close price, open price etc
/ q)get_most_losers_stocks[]
get_most_losers_stocks:{
predefined_iex_lists_data[`losers]
}
/ Helper function for getting 'lists' data from IEX
/ There are three types of lists: most active, gainers and losers
predefined_iex_lists_data:{[list]
list: string(list);
suffix: "GET /1.0/stock/market/list/",list;
/ Parse json response and put into table
data:.j.k "[",get_data[main_url;suffix;prefix;-2;"symbol"];
`sym xcol update symbol:`$symbol, openTime:"P"$string(convert_epoch openTime), closeTime:"P"$string(convert_epoch closeTime), latestUpdate:"P"$string(convert_epoch latestUpdate), iexLastUpdated:"P"$string(convert_epoch iexLastUpdated), delayedPriceTime:"P"$string(convert_epoch delayedPriceTime) from data
}