1

So I'm learning finance and matplotlib, and I want to download 20 .csv files each with some data. The thing is when I execute the code, the terminal prints a massive error.

Up to now, I've tried changing the code, trying to remove MSTAR from the .pickle file, even if MSTAR isn't even in the .pickle file, I also tried asking around on Github, still nothing, I even looked it up on stack overflow, and nothing worked

the error seems to happen when I run the following

def get_data_from_yahoo(reload_sp500=False):
    if reload_sp500:
        tickers = save_sp500_tickers()
    else:
        with open("sp500tickers.pickle", "rb") as f:
            tickers = pickle.load(f)
    if not os.path.exists('stock_dfs'):
        os.makedirs('stock_dfs')

    start = dt.datetime(2010, 1, 1)
    end = dt.datetime.now()
    for ticker in tickers[:20]:

        if not os.path.exists('stock_dfs/{}.csv'.format(ticker)):
            df = web.DataReader(ticker, 'morningstar', start, end)
            df.reset_index(inplace=True)
            df.set_index("Date", inplace=True)
            df = df.drop("Symbol", axis=1)
            df.to_csv('stock_dfs/{}.csv'.format(ticker))
        else:
            print('Already have {}'.format(ticker))

get_data_from_yahoo()

I expected a folder full of .csv files, each with their data, but I got an error telling me the following:

Traceback (most recent call last):
  File "C:\Users\chedl\Documents\Programs\Python\financing\python-for-financing.py", line 46, in <module>
    get_data_from_yahoo()
  File "C:\Users\chedl\Documents\Programs\Python\financing\python-for-financing.py", line 37, in get_data_from_yahoo
    df = web.DataReader(ticker, 'morningstar', start, end)
  File "C:\Users\chedl\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pandas_datareader\data.py", line 387, in DataReader
    session=session, interval="d").read()
  File "C:\Users\chedl\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pandas_datareader\mstar\daily.py", line 57, in __init__
    raise ImmediateDeprecationError(DEP_ERROR_MSG.format("Morningstar"))
pandas_datareader.exceptions.ImmediateDeprecationError:
Morningstar has been immediately deprecated due to large breaks in the API without the
introduction of a stable replacement. Pull Requests to re-enable these data
connectors are welcome.

If you don't understand the error, it's asking for a specific dataset even if I don't want it, it's trying to download that dataset, then it complains about some breaks in the API, even if I never wanted this file, and also, it's getting the names from a.pickle file, well, nowhere is it written morningstar, or MSTAR in that .pickle file.

1 Answers1

1

Looks like you're specifically requesting data from Morningstar in your code:

df = web.DataReader(ticker, 'morningstar', start, end)

Yahoo Finance can be rather difficult to work with at times. I find Quandl to be both easier to use and more reliable. You can sign up for free here which will allow you access to an API key. They also have a python library which you can pip install, then import to your file. From there, just replace the call to Yahoo Finance with the call to Quandl.

    if not os.path.exists('stock_dfs/{}.csv'.format(ticker)):
        quandl.ApiConfig.api_key = "YOUR API KEY"
        df = quandl.get(ticker, start_date=start, end_date=end)
        df.reset_index(inplace=True)
        df.set_index("Date", inplace=True)
        df = df.drop("Symbol", axis=1)
        df.to_csv('stock_dfs/{}.csv'.format(ticker))
    else:
        print('Already have {}'.format(ticker))

Docs for Quandl here

DH_III
  • 171
  • 2
  • 12