0
votes

I have a list of tickers that I want to retrieve the prices for by running the following:

from yahoo_fin import stock_info as si
for x in watchlist:
    print(si.get_live_price(x))

When I run this I get the following error:

File "", line 1, in runfile('C:/Users/User/OneDrive/Documents/Stuff/fluff 2.py', wdir='C:/Users/User/OneDrive/Documents/Stuff')

File "D:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile execfile(filename, namespace)

File "D:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/User/OneDrive/Documents/Stuff/fluff 2.py", line 46, in print(si.get_live_price(x))

File "D:\Anaconda3\lib\site-packages\yahoo_fin\stock_info.py", line 338, in get_live_price df = get_data(ticker, end_date = pd.Timestamp.today() + pd.DateOffset(10))

File "D:\Anaconda3\lib\site-packages\yahoo_fin\stock_info.py", line 68, in get_data temp = loads(needed)

ValueError: Expected object or value

However, when I refer to a ticker directly, it runs normally:

print(si.get_live_price('tsla'))

348.8399963378906

What could be causing this issue? Is it due to me using a different html parser than that used with yahoo_fin in an earlier part of the code?

2
So, watchlist is a list of strings, and all of them work when you test them directly? Are you sure about that? - sammy
Now it's not working at all, the TSLA example I provided resulted in the same error message instead of the price value it showed before. Perhaps I need a new source of price data. - standard

2 Answers

0
votes

Try this out, It gives you complete dataframe for last 6 months data

import yfinance as yf
for x in ['TSLA','AAPL']:
    data = yf.download( tickers = x)
    print(data['Close'][-1])

Output :

348.8399963378906
268.4800109863281

If you want last 6 month data then you can store individual dataframe. In above case I have printed only last index as you wanted LTP.

0
votes

This issue should be fixed now in the latest version of yahoo_fin (0.8.4). It was due to a change in Yahoo Finance's structure. See here for news about recent updates: http://theautomatic.net/2019/12/16/updates-to-yahoo_fin-package/