-
Notifications
You must be signed in to change notification settings - Fork 181
/
ystockquote.py
501 lines (323 loc) · 10.9 KB
/
ystockquote.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
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
#
# ystockquote : Python module - retrieve stock quote data from Yahoo Finance
#
# Copyright (c) 2007,2008,2013,2016 Corey Goldberg (cgoldberg@gmail.com)
#
# license: GNU LGPL
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# Requires: Python 2.7/3.3+
__version__ = '0.2.6dev' # NOQA
try:
# py3
from urllib.request import Request, urlopen
from urllib.parse import urlencode
except ImportError:
# py2
from urllib2 import Request, urlopen
from urllib import urlencode
def _request(symbol, stat):
url = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (symbol, stat)
req = Request(url)
resp = urlopen(req)
content = resp.read().decode().strip()
return content
def get_all(symbol):
"""
Get all available quote data for the given ticker symbol.
Returns a dictionary.
"""
ids = \
'ydb2r1b3qpoc1d1cd2c6t1k2p2c8m5c3m6gm7hm8k1m3lm4l1t8w1g1w4g3p' \
'1g4mg5m2g6kvjj1j5j3k4f6j6nk5n4ws1xj2va5b6k3t7a2t615l2el3e7v1' \
'e8v7e9s6b4j4p5p6rr2r5r6r7s7'
values = _request(symbol, ids).split(',')
return dict(
dividend_yield=values[0],
dividend_per_share=values[1],
ask_realtime=values[2],
dividend_pay_date=values[3],
bid_realtime=values[4],
ex_dividend_date=values[5],
previous_close=values[6],
today_open=values[7],
change=values[8],
last_trade_date=values[9],
change_percent_change=values[10],
trade_date=values[11],
change_realtime=values[12],
last_trade_time=values[13],
change_percent_realtime=values[14],
change_percent=values[15],
after_hours_change_realtime=values[16],
change_200_sma=values[17],
todays_low=values[18],
change_50_sma=values[19],
todays_high=values[20],
percent_change_50_sma=values[21],
last_trade_realtime_time=values[22],
fifty_sma=values[23],
last_trade_time_plus=values[24],
twohundred_sma=values[25],
last_trade_price=values[26],
one_year_target=values[27],
todays_value_change=values[28],
holdings_gain_percent=values[29],
todays_value_change_realtime=values[30],
annualized_gain=values[31],
price_paid=values[32],
holdings_gain=values[33],
todays_range=values[34],
holdings_gain_percent_realtime=values[35],
todays_range_realtime=values[36],
holdings_gain_realtime=values[37],
fiftytwo_week_high=values[38],
more_info=values[39],
fiftytwo_week_low=values[40],
market_cap=values[41],
change_from_52_week_low=values[42],
market_cap_realtime=values[43],
change_from_52_week_high=values[44],
float_shares=values[45],
percent_change_from_52_week_low=values[46],
company_name=values[47],
percent_change_from_52_week_high=values[48],
notes=values[49],
fiftytwo_week_range=values[50],
shares_owned=values[51],
stock_exchange=values[52],
shares_outstanding=values[53],
volume=values[54],
ask_size=values[55],
bid_size=values[56],
last_trade_size=values[57],
ticker_trend=values[58],
average_daily_volume=values[59],
trade_links=values[60],
order_book_realtime=values[61],
high_limit=values[62],
eps=values[63],
low_limit=values[64],
eps_estimate_current_year=values[65],
holdings_value=values[66],
eps_estimate_next_year=values[67],
holdings_value_realtime=values[68],
eps_estimate_next_quarter=values[69],
revenue=values[70],
book_value=values[71],
ebitda=values[72],
price_sales=values[73],
price_book=values[74],
pe=values[75],
pe_realtime=values[76],
peg=values[77],
price_eps_estimate_current_year=values[78],
price_eps_estimate_next_year=values[79],
short_ratio=values[80],
)
def get_dividend_yield(symbol):
return _request(symbol, 'y')
def get_dividend_per_share(symbol):
return _request(symbol, 'd')
def get_ask_realtime(symbol):
return _request(symbol, 'b2')
def get_dividend_pay_date(symbol):
return _request(symbol, 'r1')
def get_bid_realtime(symbol):
return _request(symbol, 'b3')
def get_ex_dividend_date(symbol):
return _request(symbol, 'q')
def get_previous_close(symbol):
return _request(symbol, 'p')
def get_today_open(symbol):
return _request(symbol, 'o')
def get_change(symbol):
return _request(symbol, 'c1')
def get_last_trade_date(symbol):
return _request(symbol, 'd1')
def get_change_percent_change(symbol):
return _request(symbol, 'c')
def get_trade_date(symbol):
return _request(symbol, 'd2')
def get_change_realtime(symbol):
return _request(symbol, 'c6')
def get_last_trade_time(symbol):
return _request(symbol, 't1')
def get_change_percent_realtime(symbol):
return _request(symbol, 'k2')
def get_change_percent(symbol):
return _request(symbol, 'p2')
def get_after_hours_change(symbol):
return _request(symbol, 'c8')
def get_change_200_sma(symbol):
return _request(symbol, 'm5')
def get_commission(symbol):
return _request(symbol, 'c3')
def get_percent_change_200_sma(symbol):
return _request(symbol, 'm6')
def get_todays_low(symbol):
return _request(symbol, 'g')
def get_change_50_sma(symbol):
return _request(symbol, 'm7')
def get_todays_high(symbol):
return _request(symbol, 'h')
def get_percent_change_50_sma(symbol):
return _request(symbol, 'm8')
def get_last_trade_realtime_time(symbol):
return _request(symbol, 'k1')
def get_50_sma(symbol):
return _request(symbol, 'm3')
def get_last_trade_time_plus(symbol):
return _request(symbol, 'l')
def get_200_sma(symbol):
return _request(symbol, 'm4')
def get_last_trade_price(symbol):
return _request(symbol, 'l1')
def get_1_year_target(symbol):
return _request(symbol, 't8')
def get_todays_value_change(symbol):
return _request(symbol, 'w1')
def get_holdings_gain_percent(symbol):
return _request(symbol, 'g1')
def get_todays_value_change_realtime(symbol):
return _request(symbol, 'w4')
def get_annualized_gain(symbol):
return _request(symbol, 'g3')
def get_price_paid(symbol):
return _request(symbol, 'p1')
def get_holdings_gain(symbol):
return _request(symbol, 'g4')
def get_todays_range(symbol):
return _request(symbol, 'm')
def get_holdings_gain_percent_realtime(symbol):
return _request(symbol, 'g5')
def get_todays_range_realtime(symbol):
return _request(symbol, 'm2')
def get_holdings_gain_realtime(symbol):
return _request(symbol, 'g6')
def get_52_week_high(symbol):
return _request(symbol, 'k')
def get_more_info(symbol):
return _request(symbol, 'v')
def get_52_week_low(symbol):
return _request(symbol, 'j')
def get_market_cap(symbol):
return _request(symbol, 'j1')
def get_change_from_52_week_low(symbol):
return _request(symbol, 'j5')
def get_market_cap_realtime(symbol):
return _request(symbol, 'j3')
def get_change_from_52_week_high(symbol):
return _request(symbol, 'k4')
def get_float_shares(symbol):
return _request(symbol, 'f6')
def get_percent_change_from_52_week_low(symbol):
return _request(symbol, 'j6')
def get_company_name(symbol):
return _request(symbol, 'n')
def get_percent_change_from_52_week_high(symbol):
return _request(symbol, 'k5')
def get_notes(symbol):
return _request(symbol, 'n4')
def get_52_week_range(symbol):
return _request(symbol, 'w')
def get_shares_owned(symbol):
return _request(symbol, 's1')
def get_stock_exchange(symbol):
return _request(symbol, 'x')
def get_shares_outstanding(symbol):
return _request(symbol, 'j2')
def get_volume(symbol):
return _request(symbol, 'v')
def get_ask_size(symbol):
return _request(symbol, 'a5')
def get_bid_size(symbol):
return _request(symbol, 'b6')
def get_last_trade_size(symbol):
return _request(symbol, 'k3')
def get_ticker_trend(symbol):
return _request(symbol, 't7')
def get_average_daily_volume(symbol):
return _request(symbol, 'a2')
def get_trade_links(symbol):
return _request(symbol, 't6')
def get_order_book_realtime(symbol):
return _request(symbol, 'i5')
def get_high_limit(symbol):
return _request(symbol, 'l2')
def get_eps(symbol):
return _request(symbol, 'e')
def get_low_limit(symbol):
return _request(symbol, 'l3')
def get_eps_estimate_current_year(symbol):
return _request(symbol, 'e7')
def get_holdings_value(symbol):
return _request(symbol, 'v1')
def get_eps_estimate_next_year(symbol):
return _request(symbol, 'e8')
def get_holdings_value_realtime(symbol):
return _request(symbol, 'v7')
def get_eps_estimate_next_quarter(symbol):
return _request(symbol, 'e9')
def get_revenue(symbol):
return _request(symbol, 's6')
def get_book_value(symbol):
return _request(symbol, 'b4')
def get_ebitda(symbol):
return _request(symbol, 'j4')
def get_price_sales(symbol):
return _request(symbol, 'p5')
def get_price_book(symbol):
return _request(symbol, 'p6')
def get_pe(symbol):
return _request(symbol, 'r')
def get_pe_realtime(symbol):
return _request(symbol, 'r2')
def get_peg(symbol):
return _request(symbol, 'r5')
def get_price_eps_estimate_current_year(symbol):
return _request(symbol, 'r6')
def get_price_eps_estimate_next_year(symbol):
return _request(symbol, 'r7')
def get_short_ratio(symbol):
return _request(symbol, 's7')
def get_historical_prices(symbol, start_date, end_date):
"""
Get historical prices for the given ticker symbol.
Date format is 'YYYY-MM-DD'
Returns a nested dictionary (dict of dicts).
outer dict keys are dates ('YYYY-MM-DD')
"""
params = urlencode({
's': symbol,
'a': int(start_date[5:7]) - 1,
'b': int(start_date[8:10]),
'c': int(start_date[0:4]),
'd': int(end_date[5:7]) - 1,
'e': int(end_date[8:10]),
'f': int(end_date[0:4]),
'g': 'd',
'ignore': '.csv',
})
url = 'http://real-chart.finance.yahoo.com/table.csv?%s' % params
req = Request(url)
resp = urlopen(req)
content = str(resp.read().decode('utf-8').strip())
daily_data = content.splitlines()
hist_dict = dict()
keys = daily_data[0].split(',')
for day in daily_data[1:]:
day_data = day.split(',')
date = day_data[0]
hist_dict[date] = \
{keys[1]: day_data[1],
keys[2]: day_data[2],
keys[3]: day_data[3],
keys[4]: day_data[4],
keys[5]: day_data[5],
keys[6]: day_data[6]}
return hist_dict