Skip to content

Commit 06e2ac1

Browse files
committed
Merge pull request matplotlib#469 from evandavey/patch-1
fetch_historical_yahoo - adds dividend support
2 parents 135c749 + 51f461e commit 06e2ac1

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

lib/matplotlib/finance.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def parse_yahoo_historical(fh, adjusted=True, asobject=False):
132132
return d.view(np.recarray) # Close enough to former Bunch return
133133

134134

135-
def fetch_historical_yahoo(ticker, date1, date2, cachename=None):
135+
def fetch_historical_yahoo(ticker, date1, date2, cachename=None,dividends=False):
136136
"""
137137
Fetch historical data for ticker between date1 and date2. date1 and
138138
date2 are date or datetime instances, or (year, month, day) sequences.
@@ -143,6 +143,9 @@ def fetch_historical_yahoo(ticker, date1, date2, cachename=None):
143143
cachename is the name of the local file cache. If None, will
144144
default to the md5 hash or the url (which incorporates the ticker
145145
and date range)
146+
147+
set dividends=True to return dividends instead of price data. With
148+
this option set, parse functions will not work
146149
147150
a file handle is returned
148151
"""
@@ -160,11 +163,17 @@ def fetch_historical_yahoo(ticker, date1, date2, cachename=None):
160163
d2 = (date2.month-1, date2.day, date2.year)
161164

162165

163-
urlFmt = 'http://table.finance.yahoo.com/table.csv?a=%d&b=%d&c=%d&d=%d&e=%d&f=%d&s=%s&y=0&g=d&ignore=.csv'
166+
if dividends:
167+
g='v'
168+
verbose.report('Retrieving dividends instead of prices')
169+
else:
170+
g='d'
171+
172+
urlFmt = 'http://table.finance.yahoo.com/table.csv?a=%d&b=%d&c=%d&d=%d&e=%d&f=%d&s=%s&y=0&g=%s&ignore=.csv'
164173

165174

166175
url = urlFmt % (d1[0], d1[1], d1[2],
167-
d2[0], d2[1], d2[2], ticker)
176+
d2[0], d2[1], d2[2], ticker, g)
168177

169178

170179
if cachename is None:

0 commit comments

Comments
 (0)