zipline icon indicating copy to clipboard operation
zipline copied to clipboard

Zipline fails to download data for GOOG

Open MarkDunne opened this issue 12 years ago • 6 comments

I cannot download data for GOOG using the load_from_yahoo function. I'm pretty sure zipline is using an outdated API and that it should migrate to a new one.

This code fails

import pytz
from datetime import datetime
from zipline.utils.factory import load_from_yahoo


if __name__ == '__main__':
    start = datetime(2013, 1, 1, 0, 0, 0, 0, pytz.utc)
    end   = datetime(2014, 1, 1, 0, 0, 0, 0, pytz.utc)     

    data = load_from_yahoo(stocks=['AAPL', 'MSFT', 'GOOG'], indexes={}, start=start, end=end)

and produces this error

AAPL
MSFT
GOOG
Traceback (most recent call last):
  File "C:\Users\Home\Desktop\temp.py", line 10, in <module>
    data = load_from_yahoo(stocks=['AAPL', 'MSFT', 'GOOG'], indexes={}, start=start, end=end)
  File "C:\Users\Home\Anaconda\lib\site-packages\zipline\data\loader.py", line 319, in load_from_yahoo
    data = _load_raw_yahoo_data(indexes, stocks, start, end)
  File "C:\Users\Home\Anaconda\lib\site-packages\zipline\data\loader.py", line 283, in _load_raw_yahoo_data
    stkd = DataReader(stock, 'yahoo', start, end).sort_index()
  File "C:\Users\Home\Anaconda\lib\site-packages\pandas\io\data.py", line 77, in DataReader
    retry_count=retry_count, pause=pause)
  File "C:\Users\Home\Anaconda\lib\site-packages\pandas\io\data.py", line 401, in get_data_yahoo
    adjust_price, ret_index, chunksize, 'yahoo')
  File "C:\Users\Home\Anaconda\lib\site-packages\pandas\io\data.py", line 347, in _get_data_from
    hist_data = src_fn(symbols, start, end, retry_count, pause)
  File "C:\Users\Home\Anaconda\lib\site-packages\pandas\io\data.py", line 200, in _get_hist_yahoo
    return _retry_read_url(url, retry_count, pause, 'Yahoo!')
  File "C:\Users\Home\Anaconda\lib\site-packages\pandas\io\data.py", line 177, in _retry_read_url
    "return a 200 for url %r" % (retry_count, name, url))
IOError: after 3 tries, Yahoo! did not return a 200 for url 'http://ichart.finance.yahoo.com/table.csv?s=GOOG&a=0&b=1&c=2013&d=0&e=1&f=2014&g=d&ignore=.csv'
[Finished in 6.4s with exit code 1]

MarkDunne avatar Sep 27 '14 10:09 MarkDunne

Can you try with load_bars_from_yahoo ?

twiecki avatar Sep 27 '14 12:09 twiecki

Same behaviour

MarkDunne avatar Sep 28 '14 18:09 MarkDunne

Tested this out a bit locally. It looks like any query for GOOG that ends on or before March 27th, 2014 will fail. Notably, that's the record date of Google's unusual stock dividend from earlier this year. (See, e.g. http://seekingalpha.com/article/2111413-googles-going-ex-dividend-a-stock-split). I'm not sure whether there's a whole lot we can do about this; @MarkDunne you mentioned that you think we're using an outdated API. Could you provide a pointer to what you think we ought to be using instead?

ssanderson avatar Sep 28 '14 19:09 ssanderson

It seems like this is something to do with Yahoo finance itself actually. When I went to investigate, I saw that Zipline loads the data via a pandas API which uses the icharts.finance.yahoo api. Regardless of whether this API is outdated or not, it seems that Yahoo has a problem displaying any data before the stock split.

If you go to http://finance.yahoo.com/q/hp?s=GOOG, you won't be able to download any data of the price before March 27th.

You can however see prices from before March 27th in Yahoo's interactive charts - http://finance.yahoo.com/echarts?s=GOOG+Interactive - where they use their own api to get the data

Interactive charts API example URL

Google finance has no problem displaying this data however. I suggest implementing a load_from_google function in loader.py. It will be very similar to load_from_yahoo, and should be a straight forward implementation

As a quick proof of concept you can change the two

DataReader(stock, 'yahoo', start, end).sort_index() lines in loader.py to DataReader(stock, 'google', start, end).sort_index()

and call

data = load_from_yahoo(stocks=['AAPL', 'MSFT', 'GOOG'], indexes={}, start=start, end=end, adjusted=False)

to get all the data perfectly

MarkDunne avatar Sep 28 '14 22:09 MarkDunne

@MarkDunne the seeking alpha article shared by @ssanderson explains that the GOOG shares that existed prior to the stock split are now traded as class A shares under the ticker GOOGL. The shares trading under GOOG don't work prior to the split because they technically didn't exist yet. You will have to replace GOOG with GOOGL if you need data prior to the split.

humdings avatar Sep 28 '14 23:09 humdings

@humdings Oh ok, I incorrectly assumed that GOOG would be constant since it was the 'original'.

Anyway, before I saw this reply, I got something working. I made a pull request here to add the option to set google as the source - https://github.com/quantopian/zipline/pull/398

MarkDunne avatar Sep 28 '14 23:09 MarkDunne