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.
15
This is the "ystockquote" module.
17
This module provides a Python API for retrieving stock data from Yahoo Finance.
20
>>> import ystockquote
21
>>> print ystockquote.get_price('GOOG')
25
def __request(symbol, stat):
26
url = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (symbol, stat)
27
return urllib.urlopen(url).read().strip().strip('"')
31
Get all available quote data for the given ticker symbol.
35
values = __request(symbol, 'l1c1va2xj1b4j4dyekjm3m4rr5p5p6s7').split(',')
37
data['price'] = values[0]
38
data['change'] = values[1]
39
data['volume'] = values[2]
40
data['avg_daily_volume'] = values[3]
41
data['stock_exchange'] = values[4]
42
data['market_cap'] = values[5]
43
data['book_value'] = values[6]
44
data['ebitda'] = values[7]
45
data['dividend_per_share'] = values[8]
46
data['dividend_yield'] = values[9]
47
data['earnings_per_share'] = values[10]
48
data['52_week_high'] = values[11]
49
data['52_week_low'] = values[12]
50
data['50day_moving_avg'] = values[13]
51
data['200day_moving_avg'] = values[14]
52
data['price_earnings_ratio'] = values[15]
53
data['price_earnings_growth_ratio'] = values[16]
54
data['price_sales_ratio'] = values[17]
55
data['price_book_ratio'] = values[18]
56
data['short_ratio'] = values[19]
59
def get_price(symbol):
60
return __request(symbol, 'l1')
62
def get_change(symbol):
63
return __request(symbol, 'c1')
65
def get_volume(symbol):
66
return __request(symbol, 'v')
68
def get_avg_daily_volume(symbol):
69
return __request(symbol, 'a2')
71
def get_stock_exchange(symbol):
72
return __request(symbol, 'x')
74
def get_market_cap(symbol):
75
return __request(symbol, 'j1')
77
def get_book_value(symbol):
78
return __request(symbol, 'b4')
80
def get_ebitda(symbol):
81
return __request(symbol, 'j4')
83
def get_dividend_per_share(symbol):
84
return __request(symbol, 'd')
86
def get_dividend_yield(symbol):
87
return __request(symbol, 'y')
89
def get_earnings_per_share(symbol):
90
return __request(symbol, 'e')
92
def get_52_week_high(symbol):
93
return __request(symbol, 'k')
95
def get_52_week_low(symbol):
96
return __request(symbol, 'j')
98
def get_50day_moving_avg(symbol):
99
return __request(symbol, 'm3')
101
def get_200day_moving_avg(symbol):
102
return __request(symbol, 'm4')
104
def get_price_earnings_ratio(symbol):
105
return __request(symbol, 'r')
107
def get_price_earnings_growth_ratio(symbol):
108
return __request(symbol, 'r5')
110
def get_price_sales_ratio(symbol):
111
return __request(symbol, 'p5')
113
def get_price_book_ratio(symbol):
114
return __request(symbol, 'p6')
116
def get_short_ratio(symbol):
117
return __request(symbol, 's7')
119
def get_historical_prices(symbol, start_date, end_date):
121
Get historical prices for the given ticker symbol.
122
Date format is 'YYYYMMDD'
124
Returns a nested list.
126
url = 'http://ichart.yahoo.com/table.csv?s=%s&' % symbol + \
127
'd=%s&' % str(int(end_date[4:6]) - 1) + \
128
'e=%s&' % str(int(end_date[6:8])) + \
129
'f=%s&' % str(int(end_date[0:4])) + \
131
'a=%s&' % str(int(start_date[4:6]) - 1) + \
132
'b=%s&' % str(int(start_date[6:8])) + \
133
'c=%s&' % str(int(start_date[0:4])) + \
136
days = urllib.urlopen(url).readlines()
137
data = [day[:-2].split(',') for day in days]