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
defget_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
defget_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'
Solution 5:
I use Quandl for prices, so when I had a similar issue I decided to check there. If you go to https://www.quandl.com/data/EOD-End-of-Day-US-Stock-Prices/documentation about a quarter of the way down under Available Tickers there is a link to download a csv file containing names and tickers. I then use the following code to create a dictionary with ticker as key and name a value.
defcompanyNames():
`` cnames = pd.read_csv('ticker_list.csv') cnames_dict = pd.Series(cnames.Name.values, index=cnames.Ticker).to_dict()
return cnames_dict
Post a Comment for "Retrieve Company Name With Ticker Symbol Input, Yahoo Or Google Api"