Retrieve company name with ticker symbol input, yahoo or google API
Solution 1
You need to first find a website / API which allows you to lookup stock symbols and provide information. Then you can query that API for information.
I came up with a quick and dirty solution here:
import requests
def get_symbol(symbol):
symbol_list = requests.get("http://chstocksearch.herokuapp.com/api/{}".format(symbol)).json()
for x in symbol_list:
if x['symbol'] == symbol:
return x['company']
company = get_symbol("MSFT")
print(company)
This website only provides company name. I didn't put any error checks. And you need the requests
module for it to work. Please install it using pip install requests
.
Update: Here's the code sample using Yahoo! Finance API:
import requests
def get_symbol(symbol):
url = "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query={}®ion=1&lang=en".format(symbol)
result = requests.get(url).json()
for x in result['ResultSet']['Result']:
if x['symbol'] == symbol:
return x['name']
company = get_symbol("MSFT")
print(company)
Solution 2
import yfinance as yf
msft = yf.Ticker("MSFT")
company_name = msft.info['longName']
#Output = 'Microsoft Corporation'
So this way you would be able to get the full names of companies from stock symbols
Solution 3
Using fuzzy match to get company symbol from company name or vice versa
from fuzzywuzzy import process
import requests
def getCompany(text):
r = requests.get('https://api.iextrading.com/1.0/ref-data/symbols')
stockList = r.json()
return process.extractOne(text, stockList)[0]
getCompany('GOOG')
getCompany('Alphabet')
Solution 4
Here's another Yahoo API call. @masnun's call will return all results that contain the search param, for example trying AMD (Advanced Micro Devices):
http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=amd®ion=1&lang=en
gives you AMD (Advanced Micro Devices, Inc.), AMDA (Amedica Corporation), DOX (Amdocs Limited),
etc.
If you know the ticker, you can try either of these Yahoo APIs:z
http://finance.yahoo.com/d/quotes.csv?s=amd&f=nb4t8
(well documented, this particular call asks for n=name; b4=book value; t8=1yr target price).
https://query2.finance.yahoo.com/v7/finance/options/amd
(not very well documented but new...see more info here about this API: https://stackoverflow.com/a/40243903/933972)
Forgot to include the Google API, which seems ok for stock quotes, but not reliable for full data on option chains: 'https://www.google.com/finance?q=nyse:amd&output=json'
paulz
Updated on February 06, 2022Comments
-
paulz over 2 years
Just looking for a simple api return, where I can input a ticker symbol and receive the full company name:
ticker('MSFT') will return "Microsoft"
-
prashanth manohar about 7 yearsIs it possible to return Ticker symbol given the name of the company? Which parameters should I modify in your code?
-
dborger over 4 yearsI love yfinance and use it regularly. Unfortunately it seems that the info method is somewhat flaky, depending on the stock your working with. Some of the other suggestions above seem to be from the days when the Yahoo and Google APIs were still functioning.
-
alejandro about 4 yearsActually this answer does not work appropriately for all tickers. For example, the ticker OGEN (which is Oragenics, Inc) gives the following error: IndexError: list index out of range I guess it's just that yfinance is not up-to-date. Also, if you gibe a non-existent ticker, e.g. "--", it throws a ValueError: ValueError: No tables found When, ideally it I would expect something along the lines of None.
-
Rohith Nambiar over 2 yearsLook at my answer
-
bsplosion almost 2 yearsThat API doesn't appear to be available any longer - a 403 is returned on request.