This repository has been archived by the owner on Nov 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stocks.py
416 lines (380 loc) · 13.6 KB
/
stocks.py
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
import requests
import urllib
import json
import misc
from time import sleep
from prefs import prefs, keys
from formatter import extformat
cache = {}
# we merge stocks with this so keyerrors dont happen
emptyquote = {
'AfterHoursChangeRealtime' ,
'AnnualizedGain' ,
'Ask' ,
'AskRealtime' ,
'AverageDailyVolume' ,
'Bid' ,
'BidRealtime' ,
'BookValue' ,
'Change' ,
'ChangeFromFiftydayMovingAverage' ,
'ChangeFromTwoHundreddayMovingAverage' ,
'ChangeFromYearHigh' ,
'ChangeFromYearLow' ,
'ChangePercentRealtime' ,
'ChangeRealtime' ,
'Change_PercentChange' ,
'ChangeinPercent' ,
'Commission' ,
'Currency' ,
'DaysHigh' ,
'DaysLow' ,
'DaysRange' ,
'DaysRangeRealtime' ,
'DaysValueChange' ,
'DaysValueChangeRealtime' ,
'DividendPayDate' ,
'DividendShare' ,
'DividendYield' ,
'EBITDA' ,
'EPSEstimateCurrentYear' ,
'EPSEstimateNextQuarter' ,
'EPSEstimateNextYear' ,
'EarningsShare' ,
'ErrorIndicationreturnedforsymbolchangedinvalid' ,
'ExDividendDate' ,
'FiftydayMovingAverage' ,
'HighLimit' ,
'HoldingsGain' ,
'HoldingsGainPercent' ,
'HoldingsGainPercentRealtime' ,
'HoldingsGainRealtime' ,
'HoldingsValue' ,
'HoldingsValueRealtime' ,
'LastTradeDate' ,
'LastTradePriceOnly' ,
'LastTradeRealtimeWithTime' ,
'LastTradeTime' ,
'LastTradeWithTime' ,
'LowLimit' ,
'MarketCapRealtime' ,
'MarketCapitalization' ,
'MoreInfo' ,
'Name' ,
'Notes' ,
'OneyrTargetPrice' ,
'Open' ,
'OrderBookRealtime' ,
'PEGRatio' ,
'PERatio' ,
'PERatioRealtime' ,
'PercebtChangeFromYearHigh' ,
'PercentChange' ,
'PercentChangeFromFiftydayMovingAverage' ,
'PercentChangeFromTwoHundreddayMovingAverage' ,
'PercentChangeFromYearLow' ,
'PreviousClose' ,
'PriceBook' ,
'PriceEPSEstimateCurrentYear' ,
'PriceEPSEstimateNextYear' ,
'PricePaid' ,
'PriceSales' ,
'SharesOwned' ,
'ShortRatio' ,
'StockExchange' ,
'Symbol' ,
'TickerTrend' ,
'TradeDate' ,
'TwoHundreddayMovingAverage' ,
'Volume' ,
'YearHigh' ,
'YearLow' ,
'YearRange' ,
'symbol' ,
}
# numeric key values we should convert to numbers instead of strings
numerkeys = [
'Ask',
'AverageDailyVolume',
'Bid',
'AskRealtime',
'BidRealtime',
'BookValue',
'Change',
'ChangeRealtime',
'AfterHoursChangeRealtime',
'DividendShare',
'EarningsShare',
'EPSEstimateCurrentYear',
'EPSEstimateNextYear',
'EPSEstimateNextQuarter',
'DaysLow',
'DaysHigh',
'YearLow',
'YearHigh',
'HoldingsGainPercent',
'AnnualizedGain',
'HoldingsGain',
'HoldingsGainPercentRealtime',
'HoldingsGainRealtime',
'MarketCapitalization',
'MarketCapRealtime',
'EBITDA',
'ChangeFromYearLow',
'PercentChangeFromYearLow',
'ChangePercentRealtime',
'ChangeFromYearHigh',
'PercebtChangeFromYearHigh',
'LastTradePriceOnly',
'FiftydayMovingAverage',
'TwoHundreddayMovingAverage',
'ChangeFromTwoHundreddayMovingAverage',
'PercentChangeFromTwoHundreddayMovingAverage',
'ChangeFromFiftydayMovingAverage',
'PercentChangeFromFiftydayMovingAverage',
'Open',
'PreviousClose',
'PricePaid',
'ChangeinPercent',
'PriceSales',
'PriceBook',
'PERatio',
'PERatioRealtime',
'PEGRatio',
'PriceEPSEstimateCurrentYear',
'PriceEPSEstimateNextYear',
'SharesOwned',
'ShortRatio',
'OneyrTargetPrice',
'Volume',
'HoldingsValue',
'HoldingsValueRealtime',
'DaysValueChange',
'DaysValueChangeRealtime',
'DividendYield',
'PercentChange',
]
def query(url):
"""
local wrapper around misc.request_json
performs caching
"""
global cache
ret, cache = misc.request_json(url, cache)
return ret
def yahoo_query(url):
"""
local wrapper around misc.request_json
performs caching
"""
global cache
ret, cache = misc.request_json(url, cache)
if 'query' in ret:
ret = cache[url] = ret['query']['results']['quote']
return ret
def google_query(url):
"""
local wrapper around misc.request
performs caching
"""
global cache
bad_request_str = 'httpserver.cc: Response Code 400\n'
for i in range(prefs['max_retries']):
ret, cache = misc.request(url, cache)
if isinstance(ret, str):
if ret != bad_request_str:
# google returns a `// [` before and a `]` after the data we can get rid
# of with a slice
ret = cache[url] = json.loads(ret[4:])
break
else:
ret = []
sleep(1)
return ret
def yahoo_query_url(query):
"""
takes a YQL query like
select * from yahoo.finance.quotes where symbol in ("TLSA", "^GSPC")
and returns a correctly encoded URL that can be requested and parsed as json
with query or misc.request_json
"""
# {
# 'format': 'json',
# 'diagnostics': 'true',
# 'env': 'store://datatables.org/alltableswithkeys',
# 'q': query,
# }
return ('https://query.yahooapis.com/v1/public/yql?format=json'
'&diagnostics=true'
'&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&q='
+ urllib.parse.quote(query))
def google_query_url(symbols):
# https://github.com/hongtaocai/googlefinance
symblist = ','.join(symbols)
# a deprecated but still active & correct api
# {
# 'client': 'ig',
# 'infotype': 'infoquoteall',
# 'q': symblist,
# }
return ('http://finance.google.com/finance/info?client=ig'
+ '&infotype=infoquoteall&q='
+ urllib.parse.quote(symblist))
def yahoo_stock_query(symbols):
"""
generates the yahoo finance query url for a list of symbols using
yahoo_query_url
takes ONE argument, a list/tuple of symbols; not multiple arguments
generally don't call this alone; call symbol_list to deal with possible
dict symbols (if applicable)
"""
symblist = '"' + '", "'.join(symbols) + '"'
return yahoo_query_url(
f'select * from yahoo.finance.quotes where symbol in ({symblist})')
def fill_quote(quote):
"""
fill missing fields in a quote from google with empty strings
to prevent keyerrors
"""
for k in emptyquote:
if k not in quote:
quote[k] = ''
return quote
def google_dat(symbols):
symbols = misc.listifier(symbols)
dat = google_query(google_query_url(symbols))
fullnames = {
'op' : 'Open',
'name' : 'Name',
'hi' : 'DaysHigh',
'hi52' : 'YearHigh',
'lo' : 'DaysLow',
'lo52' : 'YearLow',
'e' : 'StockExchange',
'l' : 'LastTradePriceOnly',
'lt' : 'LastTradeDateTime',
'ltt' : 'LastTradeTime',
'ltt_dts' : 'LastTradeISOTime',
'el' : 'AfterHoursLastTradePriceOnly',
'elt' : 'AfterHoursLastTradeDateTime',
'mc' : 'MarketCapitalization',
't' : 'Symbol',
'eps' : 'EarningsShare',
'c_fix' : 'Change',
'ec_fix' : 'AfterHoursChange',
'cp_fix' : 'PercentChange',
'ecp_fix' : 'AfterHoursPercentChange',
'pcls_fix' : 'PreviousClose',
'pe' : 'PERatio',
'yld' : 'DividendYield',
'div' : 'Dividend',
'inst_own' : 'InstitutionShares',
's' : 'LastTradeSize',
}
ret = []
for symbol in dat:
# convert short keys to long keys
ret.append(fill_quote(translate_keys(symbol, fullnames)))
return ret
def yahoo_dat(symbols):
"""
gets yahoo data for a list of symbols
"""
symbols = misc.listifier(symbols)
dat = yahoo_query(yahoo_stock_query(symbols))
return misc.listifier(dat)
def extfloat(s):
"""
more leniant float parsing
"""
if isinstance(s, float) or isinstance(s, int):
return s
elif s is None or len(s) == 0:
return None
else:
striplast = True
if s[-1] == 'M':
factor = 1e6
elif s[-1] == 'B':
factor = 1e9
elif s[-1] == 'T':
factor = 1e12
else:
striplast = False
factor = 1
if striplast:
s = s[:-1]
new_s = s.replace('%', '').replace(',', '')
return float(new_s) * factor
def stock_dat(symbols, source=prefs['stocks']['source']):
dat = yahoo_dat(symbols)
# if source == 'yahoo':
# dat = yahoo_dat(symbols)
# else: # google
# dat = google_dat(symbols)
ret = []
for s, orig in zip(dat, symbols):
# if (('PreviousClose' not in s or s['PreviousClose'] is None) and
# ('PercentChange' not in s or s['PercentChange'] is None) and
# ('Volume' not in s or s['Volume'] is None)):
# # yahoo finance isn't able to return data
# # e.g. in case of dow jones (^DJI)
# # default to google finance
# # see https://stackoverflow.com/a/3681992/5719760
# # replace ^ with . for eg ^DJI (yahoo) to .DJI (google)
# tmp = google_dat(orig.replace('^', '.'))
# if len(tmp) > 0:
# s = tmp[0]
# else:
# s = None
if s is not None:
for numkey in numerkeys:
if numkey in s:
s[numkey] = extfloat(s[numkey])
ret.append(fill_quote(s))
return ret
def symbol_list(symbols):
"""
prefs.json allows overriding the format on a symbol-by-symbol basis
so, prefs.stocks.symbols isnt always 100% a string list
"""
if isinstance(symbols, str):
return [symbols]
ret = []
for s in symbols:
if isinstance(s, str):
ret.append(s)
elif isinstance(s, dict):
ret.append(s['symbol'])
else:
ret.append(str(s))
return ret
def format_stock(dat, symbol):
fstr = prefs['stocks']['format']
if isinstance(symbol, dict):
if 'style' in symbol:
fstr = prefs['stocks'][symbol['style'] + '_format']
if ('source' in symbol
and symbol['source'] != prefs['stocks']['source']):
dat = stock_dat(symbol['Symbol'], symbol['source'])
if 'display_symbol' in symbol:
dat['Symbol'] = symbol['display_symbol']
if dat['Currency'] in prefs['stocks']['excluded_currencies']:
dat['Currency'] = ''
return extformat(fstr, dat,
default=lambda: extformat(prefs['stocks']['format'], dat))
def stocks(symbols=prefs['stocks']['symbols']):
"""
assembles and formats output from a list/tuple of symbols
only takes one argument! might support *args in the future, but not now
"""
symbols = misc.listifier(symbols)
dat = misc.listifier(stock_dat(symbol_list(symbols)))
ret = []
for symdat, symbol in zip(dat, symbols):
ret.append(format_stock(symdat, symbol))
return '\n'.join(ret)
def main():
print(stocks())
if __name__ == '__main__':
main()