Skip to content

Commit 5951003

Browse files
committed
moved from Yahoo to Google finance
1 parent da9a9f4 commit 5951003

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

docs/book_numerical.tex

+12-5
Original file line numberDiff line numberDiff line change
@@ -1742,11 +1742,11 @@ \section*{Acknowledgments}
17421742
>>> html = page.read()
17431743
\end{lstlisting}
17441744

1745-
\index{YStock}\index{Yahoo finance}
1745+
\index{YStock}\index{Yahoo/Google finance}
17461746

17471747
Usually {\ft urllib} is used to download data posted online. The challenge may be parsing the data (converting from the representation used to post it to a proper Python representation).
17481748

1749-
In the following, we create a simple helper class that can download data from Yahoo! Finance and convert each stock's historical data into a list of dictionaries. Each list element corresponds to a trading day of history of the stock, and each dictionary stores the data relative to that trading day (date, open, close, volume, adjusted close, arithmetic\_return, log\_return, etc.):
1749+
In the following, we create a simple helper class that can download data from Yahoo! Finance and Google Finance and convert each stock's historical data into a list of dictionaries. Each list element corresponds to a trading day of history of the stock, and each dictionary stores the data relative to that trading day (date, open, close, volume, adjusted close, arithmetic\_return, log\_return, etc.):
17501750

17511751
\index{class!YStock}
17521752

@@ -1763,9 +1763,12 @@ \section*{Acknowledgments}
17631763
>>> h = google.historical()
17641764
>>> last_adjusted_close = h[-1]['adjusted_close']
17651765
>>> last_log_return = h[-1]['log_return']
1766+
previous version of this code user Yahoo for historical data
1767+
but Yahoo changed API and blocked them, moving to Google finance.
17661768
"""
17671769
URL_CURRENT = 'http://finance.yahoo.com/d/quotes.csv?s=%(symbol)s&f=%(columns)s'
1768-
URL_HISTORICAL = 'http://ichart.yahoo.com/table.csv?s=%(s)s&a=%(a)s&b=%(b)s&c=%(c)s&d=%(d)s&e=%(e)s&f=%(f)s'
1770+
URL_HISTORICAL = 'https://www.google.com/finance/historical?output=csv&q=%s'
1771+
'
17691772
def __init__(self,symbol):
17701773
self.symbol = symbol.upper()
17711774

@@ -1812,14 +1815,18 @@ \section*{Acknowledgments}
18121815
d=stop.month-1,e=stop.day,f=stop.year)
18131816
# Date,Open,High,Low,Close,Volume,Adj Close
18141817
lines = urllib.urlopen(url).readlines()
1815-
raw_data = [row.split(',') for row in lines[1:] if row.count(',')==6]
1818+
if any('CAPTCHA' in line for line in lines):
1819+
print url
1820+
raise
1821+
raw_data = [row.split(',') for row in lines[1:] if 5 <= row.count(',') <= 6]
18161822
previous_adjusted_close = 0
18171823
series = []
18181824
raw_data.reverse()
18191825
for row in raw_data:
1826+
if row[1] == '-': continue
18201827
open, high, low = float(row[1]), float(row[2]), float(row[3])
18211828
close, vol = float(row[4]), float(row[5])
1822-
adjusted_close = float(row[6])
1829+
adjusted_close = float(row[5]) if len(row)>5 else close
18231830
adjustment = adjusted_close/close
18241831
if previous_adjusted_close:
18251832
arithmetic_return = adjusted_close/previous_adjusted_close-1.0

0 commit comments

Comments
 (0)