I'm trying to find some information on some tickers through yfinance. However, I run into an error whenever I try to search up information on a ticker that doesn't exist. For example, this is the code I had.
import yfinance as yf
with open("/2019.txt") as f:
content = f.readlines()
# content is a list with all tickers in "/2019.txt"
content = [x.strip() for x in content]
print(content)
for x in content:
y = yf.Ticker(x)
z = y.info['volume']
print(x)
print(z)
This is the error I received:
/usr/local/lib/python3.6/dist-packages/yfinance/base.py in _get_fundamentals(self, kind, proxy)
338 self._info.update(data[item])
339
--> 340 self._info['regularMarketPrice'] = self._info['regularMarketOpen']
341 self._info['logo_url'] = ""
342 try:
KeyError: 'regularMarketOpen'
To fix this I tried:
import yfinance as yf
with open("/2019.txt") as f:
content = f.readlines()
# content is a list with all tickers in "/2019.txt"
content = [x.strip() for x in content]
print(content)
for x in content:
y = yf.Ticker(x)
if(y==1):
z = y.info['volume']
print(x)
print(z)
elif(y==0):
print(y)
print( "does not exist")
But now it doesn't print anything except the tickers in the list.
Does anyone know how to approach this? Thanks!
There are increasing issues using the info
method in the yfinance
library. Most recent issues are discussed here: https://github.com/ranaroussi/yfinance/issues/1407. For those looking to get a check, an alternative approach is to simply use the history
method and check if there is recently-available data. For example, the following is output from yf.Ticker('SPY').history(period='7d', interval='1d')
:
Open High ... Stock Splits Capital Gains
Date ...
2023-02-10 00:00:00-05:00 405.859985 408.440002 ... 0.0 0.0
2023-02-13 00:00:00-05:00 408.720001 412.970001 ... 0.0 0.0
2023-02-14 00:00:00-05:00 411.239990 415.049988 ... 0.0 0.0
2023-02-15 00:00:00-05:00 410.350006 414.059998 ... 0.0 0.0
2023-02-16 00:00:00-05:00 408.790009 412.910004 ... 0.0 0.0
2023-02-17 00:00:00-05:00 406.059998 407.510010 ... 0.0 0.0
2023-02-21 00:00:00-05:00 403.059998 404.160004 ... 0.0 0.0
For a non-trading asset (FAKENAME
in this case) the following output is seen:
FAKENAME: No data found, symbol may be delisted
An approach for wrapping that into a concise function that gives a True/False
output would be as follows:
def check_available(asset: str) -> bool:
"""
Checks if an asset is available via the Yahoo Finance API.
"""
info = yf.Ticker(asset).history(
period='7d',
interval='1d')
# return == period value for more precise check but may
# need more complex handling to take into account non-
# trading days, holidays, etc.
return len(info) > 0
Essentially, this says "an asset is available if there is recently data" which may not be true in all cases so keep that in mind.
Try to get the info of the ticker and if you get an exception you probably cannot do much more with it:
import yfinance as yf
for t in ["MSFT", "FAKE"]:
ticker = yf.Ticker(t)
info = None
try:
info = ticker.info
except:
print(f"Cannot get info of {t}, it probably does not exist")
continue
# Got the info of the ticker, do more stuff with it
print(f"Info of {t}: {info}")
See a demo on this colab.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With