Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 3195
1
#
2
# ystockquote : Python module - retrieve stock quote data from Yahoo Finance
3
#
4
# Copyright (c) 2007,2008,2013 Corey Goldberg ([email protected])
5
#
6
# license: GNU LGPL
7
#
8
# This library is free software; you can redistribute it and/or
9
# modify it under the terms of the GNU Lesser General Public
10
# License as published by the Free Software Foundation; either
11
# version 2.1 of the License, or (at your option) any later version.
12
#
13
# Requires: Python 2.7/3.2+
14
15
16
__version__ = '0.2.2'
17
18
19
try:
20
# py3
21
from urllib.request import Request, urlopen
22
from urllib.parse import urlencode
23
except ImportError:
24
# py2
25
from urllib2 import Request, urlopen
26
from urllib import urlencode
27
28
29
def _request(symbol, stat):
30
url = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (symbol, stat)
31
req = Request(url)
32
resp = urlopen(req)
33
return str(resp.read().decode('utf-8').strip())
34
35
36
def get_all(symbol):
37
"""
38
Get all available quote data for the given ticker symbol.
39
40
Returns a dictionary.
41
"""
42
values = _request(symbol, 'l1c1va2xj1b4j4dyekjm3m4rr5p5p6s7').split(',')
43
return dict(
44
price=values[0],
45
change=values[1],
46
volume=values[2],
47
avg_daily_volume=values[3],
48
stock_exchange=values[4],
49
market_cap=values[5],
50
book_value=values[6],
51
ebitda=values[7],
52
dividend_per_share=values[8],
53
dividend_yield=values[9],
54
earnings_per_share=values[10],
55
fifty_two_week_high=values[11],
56
fifty_two_week_low=values[12],
57
fifty_day_moving_avg=values[13],
58
two_hundred_day_moving_avg=values[14],
59
price_earnings_ratio=values[15],
60
price_earnings_growth_ratio=values[16],
61
price_sales_ratio=values[17],
62
price_book_ratio=values[18],
63
short_ratio=values[19],
64
)
65
66
67
def get_price(symbol):
68
return _request(symbol, 'l1')
69
70
71
def get_change(symbol):
72
return _request(symbol, 'c1')
73
74
75
def get_volume(symbol):
76
return _request(symbol, 'v')
77
78
79
def get_avg_daily_volume(symbol):
80
return _request(symbol, 'a2')
81
82
83
def get_stock_exchange(symbol):
84
return _request(symbol, 'x')
85
86
87
def get_market_cap(symbol):
88
return _request(symbol, 'j1')
89
90
91
def get_book_value(symbol):
92
return _request(symbol, 'b4')
93
94
95
def get_ebitda(symbol):
96
return _request(symbol, 'j4')
97
98
99
def get_dividend_per_share(symbol):
100
return _request(symbol, 'd')
101
102
103
def get_dividend_yield(symbol):
104
return _request(symbol, 'y')
105
106
107
def get_earnings_per_share(symbol):
108
return _request(symbol, 'e')
109
110
111
def get_52_week_high(symbol):
112
return _request(symbol, 'k')
113
114
115
def get_52_week_low(symbol):
116
return _request(symbol, 'j')
117
118
119
def get_50day_moving_avg(symbol):
120
return _request(symbol, 'm3')
121
122
123
def get_200day_moving_avg(symbol):
124
return _request(symbol, 'm4')
125
126
127
def get_price_earnings_ratio(symbol):
128
return _request(symbol, 'r')
129
130
131
def get_price_earnings_growth_ratio(symbol):
132
return _request(symbol, 'r5')
133
134
135
def get_price_sales_ratio(symbol):
136
return _request(symbol, 'p5')
137
138
139
def get_price_book_ratio(symbol):
140
return _request(symbol, 'p6')
141
142
143
def get_short_ratio(symbol):
144
return _request(symbol, 's7')
145
146
147
def get_historical_prices(symbol, start_date, end_date):
148
"""
149
Get historical prices for the given ticker symbol.
150
Date format is 'YYYY-MM-DD'
151
152
Returns a nested list (first item is list of column headers).
153
"""
154
params = urlencode({
155
's': symbol,
156
'a': int(start_date[5:7]) - 1,
157
'b': int(start_date[8:10]),
158
'c': int(start_date[0:4]),
159
'd': int(end_date[5:7]) - 1,
160
'e': int(end_date[8:10]),
161
'f': int(end_date[0:4]),
162
'g': 'd',
163
'ignore': '.csv',
164
})
165
url = 'http://ichart.yahoo.com/table.csv?%s' % params
166
req = Request(url)
167
resp = urlopen(req)
168
content = str(resp.read().decode('utf-8').strip())
169
days = content.splitlines()
170
return [day.split(',') for day in days]
171