The tickers used in pandas morningstar API

独自空忆成欢 提交于 2019-12-12 17:04:37

问题


Here is what I done for obtaining the price of the stock "TSLA" using the DataReader with morningstar API

import pandas_datareader.data as web
df = web.DataReader('TSLA', 'morningstar', start, end)

However, if I input a wrong ticker for the first parameter of the creator of DataReader, Python just keep running the line.

How to check whether a ticker is input correctly for the first parameter?


回答1:


When I ran your code with an invalid ticker symbol, I experienced the same problem with the Python Interpreter just hanging. I didn't find an entirely neat solution, but based on a somewhat related issue report I read on github (link here), I've come up with an answer that will stop the python interpreter from hanging if you enter an invalid ticker symbol.

The key seems to be setting the retry_count parameter of pandas_datareader.data.DataReader() equal to zero.

I have a fully reproducible snippet below:

import pandas_datareader
import datetime

start = datetime.datetime(2018, 5, 1)
end = datetime.datetime(2018, 5, 30)

def get_data(ticker):
    try:
        df = pandas_datareader.data.DataReader('%s' % (ticker), 'morningstar', start, end, retry_count=0)
        print(df.tail(5))
    except ValueError:
        print('Ticker Symbol %s is not available!' % (ticker))

get_data('TSLA') #valid Symbol
get_data('yyfy') #not a valid Symbol
get_data('AAPL') #valid Symbol
get_data('QQQQ') #not a valid Symbol

With Expected Output:

Symbol Date        Close     High     Low      Open   Volume
TSLA   2018-05-24  277.85  281.110  274.89  278.4000  4190598
       2018-05-25  278.85  279.640  275.61  277.6252  3875082
       2018-05-28  278.85  278.850  278.85  278.8500        0
       2018-05-29  283.76  286.500  276.15  278.5100  5666640
       2018-05-30  291.72  295.005  281.60  283.2900  7428352

Ticker Symbol yyfy is not available!

Symbol Date        Close    High     Low    Open    Volume
AAPL   2018-05-24  188.15  188.84  186.21  188.77  20330134
       2018-05-25  188.58  189.65  187.65  188.23  17460963
       2018-05-28  188.58  188.58  188.58  188.58         0
       2018-05-29  187.90  188.75  186.87  187.60  22514075
       2018-05-30  187.50  188.00  186.78  187.72  18430891

Ticker Symbol QQQQ is not available!


来源:https://stackoverflow.com/questions/50478715/the-tickers-used-in-pandas-morningstar-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!