3
# Copyright (c) 2007-2008, Corey Goldberg (corey@goldb.org)
7
# This library is free software; you can redistribute it and/or
8
# modify it under the terms of the GNU Lesser General Public
9
# License as published by the Free Software Foundation; either
10
# version 2.1 of the License, or (at your option) any later version.
17
This is the "ystockquote" module.
19
This module provides a Python API for retrieving stock data from Yahoo Finance.
22
>>> import ystockquote
23
>>> print ystockquote.get_price('GOOG')
28
def __request(symbol, stat):
29
url = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (symbol, stat)
30
return urllib.urlopen(url).read().strip().strip('"')
35
Get all available quote data for the given ticker symbol.
39
values = __request(symbol, 'l1c1va2xj1b4j4dyekjm3m4rr5p5p6s7').split(',')
41
data['price'] = values[0]
42
data['change'] = values[1]
43
data['volume'] = values[2]
44
data['avg_daily_volume'] = values[3]
45
data['stock_exchange'] = values[4]
46
data['market_cap'] = values[5]
47
data['book_value'] = values[6]
48
data['ebitda'] = values[7]
49
data['dividend_per_share'] = values[8]
50
data['dividend_yield'] = values[9]
51
data['earnings_per_share'] = values[10]
52
data['52_week_high'] = values[11]
53
data['52_week_low'] = values[12]
54
data['50day_moving_avg'] = values[13]
55
data['200day_moving_avg'] = values[14]
56
data['price_earnings_ratio'] = values[15]
57
data['price_earnings_growth_ratio'] = values[16]
58
data['price_sales_ratio'] = values[17]
59
data['price_book_ratio'] = values[18]
60
data['short_ratio'] = values[19]
64
def get_price(symbol):
65
return __request(symbol, 'l1')
68
def get_change(symbol):
69
return __request(symbol, 'c1')
72
def get_volume(symbol):
73
return __request(symbol, 'v')
76
def get_avg_daily_volume(symbol):
77
return __request(symbol, 'a2')
80
def get_stock_exchange(symbol):
81
return __request(symbol, 'x')
84
def get_market_cap(symbol):
85
return __request(symbol, 'j1')
88
def get_book_value(symbol):
89
return __request(symbol, 'b4')
92
def get_ebitda(symbol):
93
return __request(symbol, 'j4')
96
def get_dividend_per_share(symbol):
97
return __request(symbol, 'd')
100
def get_dividend_yield(symbol):
101
return __request(symbol, 'y')
104
def get_earnings_per_share(symbol):
105
return __request(symbol, 'e')
108
def get_52_week_high(symbol):
109
return __request(symbol, 'k')
112
def get_52_week_low(symbol):
113
return __request(symbol, 'j')
116
def get_50day_moving_avg(symbol):
117
return __request(symbol, 'm3')
120
def get_200day_moving_avg(symbol):
121
return __request(symbol, 'm4')
124
def get_price_earnings_ratio(symbol):
125
return __request(symbol, 'r')
128
def get_price_earnings_growth_ratio(symbol):
129
return __request(symbol, 'r5')
132
def get_price_sales_ratio(symbol):
133
return __request(symbol, 'p5')
136
def get_price_book_ratio(symbol):
137
return __request(symbol, 'p6')
140
def get_short_ratio(symbol):
141
return __request(symbol, 's7')
144
def get_historical_prices(symbol, start_date, end_date):
146
Get historical prices for the given ticker symbol.
147
Date format is 'YYYYMMDD'
149
Returns a nested list.
151
url = 'http://ichart.yahoo.com/table.csv?s=%s&' % symbol + \
152
'd=%s&' % str(int(end_date[4:6]) - 1) + \
153
'e=%s&' % str(int(end_date[6:8])) + \
154
'f=%s&' % str(int(end_date[0:4])) + \
156
'a=%s&' % str(int(start_date[4:6]) - 1) + \
157
'b=%s&' % str(int(start_date[6:8])) + \
158
'c=%s&' % str(int(start_date[0:4])) + \
160
days = urllib.urlopen(url).readlines()
161
data = [day[:-2].split(',') for day in days]