Search code examples
pythonpython-3.xfinancepandas-datareader

Where and how to get the equity historical data (at least covers 2008)?


I can use the following code to get historical data from Google Finance. But the oldest is from 2016-09-20. This is too short for back testing. I need historical data which covers the year of 2008.

import pandas_datareader.data as web

start = datetime.datetime(2002, 1, 1)
end = datetime.datetime(2017, 1, 27)

aapl = DataReader("AAPL", "google", start, end)
print(len(aapl))
print(aapl.head())

The outputs are as follow:

             Open   High    Low  Close    Volume
Date                                            
2016-09-20  35.02  35.06  33.91  34.44  21192908
2016-09-21  34.93  37.27  34.47  37.01  37582384
2016-09-22  38.22  38.52  37.73  38.47  22289829
2016-09-23  38.24  38.58  37.90  38.12  18705969
2016-09-26  36.69  36.94  35.63  35.97  23382130

Solution

  • Use fix-yahoo-finance and then use yahoo rather than Google as your source. It looks like Google has been locking down a lot of its data lately.

    First you'll need to pip-install fix-yahoo-finance.

    Then:

    from pandas_datareader import data
    import fix_yahoo_finance
    
    aapl = data.get_data_yahoo('AAPL', start='2000-01-01')
    
    print(aapl.head())
                   Open     High      Low    Close  Adj Close     Volume
    Date                                                                
    2000-01-03  3.74554  4.01786  3.63170  3.99777    3.59662  133949200
    2000-01-04  3.86607  3.95089  3.61384  3.66071    3.29338  128094400
    2000-01-05  3.70536  3.94866  3.67857  3.71429    3.34158  194580400
    2000-01-06  3.79018  3.82143  3.39286  3.39286    3.05240  191993200
    2000-01-07  3.44643  3.60714  3.41071  3.55357    3.19699  115183600