Skip to content

Commit

Permalink
Add option to adjust Yahoo quotes for splits and dividends.
Browse files Browse the repository at this point in the history
  • Loading branch information
kwgoodman committed Oct 11, 2010
1 parent 4572c4e commit 1da75ec
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions la/data/yahoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from la.external.matplotlib import quotes_historical_yahoo


def quotes(tickers, date1=None, date2=None, verbose=False):
def quotes(tickers, date1=None, date2=None, adjust=True, verbose=False):
"""
Given a ticker sequence, return historical Yahoo! quotes as a 3d larry.
Expand All @@ -22,19 +22,27 @@ def quotes(tickers, date1=None, date2=None, verbose=False):
The last date to grab historical quotes on. For example:
datetime.date(2010, 12, 31) or (2010, 12, 31). By default the last
date is 10 days beyond today's date.
adjust : bool, optional
Adjust (default) the open, close, high, and low prices and the volume.
The adjustment takes splits and dividends into account such that the
corresponding returns are correct. The adjustment is not correct for
volume, but at least price * volume remains unchanged after the
adjustment.
verbose : bool, optional
Print the ticker currently being loaded. By default the tickers are
not printed.
Returns
-------
lar : larry
A 3d larry is returned. In order, the axes contain: tickers, item,
and dates, where items are
A 3d larry is returned. In order, the three axes contain: tickers,
item, and dates. The elements along the item axis depend on the value
of `adjust`. When `adjust` is False, the items are
['open', 'close', 'high', 'low', 'volume', 'adjclose']
and dates are datetime.date objects.
When adjust is true (default), the adjusted close ('adjclose') is
not included. The dates are datetime.date objects.
Examples
--------
Expand All @@ -50,7 +58,6 @@ def quotes(tickers, date1=None, date2=None, verbose=False):
high
low
volume
adjclose
label_2
2010-10-01
2010-10-04
Expand All @@ -60,16 +67,14 @@ def quotes(tickers, date1=None, date2=None, verbose=False):
[ 2.82520000e+02, 2.78640000e+02, 2.88940000e+02],
[ 2.86580000e+02, 2.82900000e+02, 2.89450000e+02],
[ 2.81350000e+02, 2.77770000e+02, 2.81820000e+02],
[ 1.60051000e+07, 1.55256000e+07, 1.78743000e+07],
[ 2.82520000e+02, 2.78640000e+02, 2.88940000e+02]],
.
[ 1.60051000e+07, 1.55256000e+07, 1.78743000e+07]],
.
[[ 2.47700000e+01, 2.39600000e+01, 2.40600000e+01],
[ 2.43800000e+01, 2.39100000e+01, 2.43500000e+01],
[ 2.48200000e+01, 2.39900000e+01, 2.44500000e+01],
[ 2.43000000e+01, 2.37800000e+01, 2.39100000e+01],
[ 6.26236000e+07, 9.80868000e+07, 7.80329000e+07],
[ 2.43800000e+01, 2.39100000e+01, 2.43500000e+01]]])
[ 6.26236000e+07, 9.80868000e+07, 7.80329000e+07]]])
>>> close = lar.lix[:,['close']]
>>> close
label_0
Expand All @@ -82,7 +87,7 @@ def quotes(tickers, date1=None, date2=None, verbose=False):
x
array([[ 282.52, 278.64, 288.94],
[ 24.38, 23.91, 24.35]])
"""
if date1 is None:
date1 = datetime.date(1900, 1, 1)
Expand All @@ -103,4 +108,13 @@ def quotes(tickers, date1=None, date2=None, verbose=False):
lar = qlar
else:
lar = lar.merge(qlar)
return lar.sortaxis(-1)
lar = lar.sortaxis(-1)
if adjust:
scale = lar.x[:,-1] / lar.x[:,1]
lar.x[:,0] *= scale
lar.x[:,1] = lar.x[:,-1]
lar.x[:,2] *= scale
lar.x[:,3] *= scale
lar.x[:,4] /= scale
lar = lar[:,:-1]
return lar

0 comments on commit 1da75ec

Please sign in to comment.