Search code examples
python-2.7pandasgoogle-finance

How can I load historical stock indices data in my script without yahoo or google finance using pandas_datareader?


Sorry for my noobish question as I'm trying to use Python for my finance class in grad school.

I am currently stuck trying to load historical stock indices of DOW JONES, S&P 500 and NASDAQ. Sadly, Google Finance is useless with stock indices so I need help circumventing this obstacle.

Here is the the line of my code that handles the loading process:

import pandas as pd 
from pandas_datareader import data as web
import matplotlib.pyplot as plt

ticker = ['^DJI', '^INX', '^IXIC']

ind_data = pd.DataFrame()
for i in ticker:
    ind_data[i] = web.DataReader(i, data_source='google', start='2000-1-1')['Close']

Thanks in advance.


Solution

  • You can use quandl. It is a third party package you will need to install.

    pip install quandl
    

    Go to their website, make an account and get an api key. Then search the site to find the right code to type in for each query.

    You can pass the quandl.get function a variety of different parameters to get exactly what you need. The below retrieves the dow jones industrial average and outputs it as a pandas dataframe.

    quandl.get("BCB/UDJIAD1")
    

    Output

    ...
    2016-04-06  17716.05
    2016-04-07  17541.96
    2016-04-08  17576.96
    2016-04-11  17556.41
    2016-04-12  17721.25
    2016-04-13  17908.28
    2016-04-14  17926.43
    2016-04-15  17897.46