Search code examples
pythonpandasyahoo-financealgorithmic-tradingquantitative-finance

Getting marketcap from yahoo finance


I have a very long list of stocks for which I'd like to get the market cap

say I have the following stocks stored as a list


test = ['A', 'AA', 'AA-W', 'AAAB', 'AAAG', 'AAAGY', 'AAAIF', 'AAALF', 'AAALY', 'AAAP', 'AAARF', 'AABA', 'AABB', 'AABC',
        'AABNF', 'AABVF', 'AAC', 'AAC', 'AAC-U', 'AAC-WS', 'AAC1', 'AACAF', 'AACAY', 'AACB', 'AACC', 'AACE', 'AACEU',
        'AACEY', 'AACH', 'AACIQ', 'AACMF', 'AACMZ', 'AACOU', 'AACPF', 'AACPU', 'AACQ', 'AACQU', 'AACQW']

How can I get the market cap with yahoo finance panda?

The code I am using now is the following, but it prints "Error with: ..." for each and every one of the stocks:

import pandas as pd
from pandas_datareader import data as pdr
import yfinance as yf

test = ['A', 'AA', 'AA-W', 'AAAB', 'AAAG', 'AAAGY', 'AAAIF', 'AAALF', 'AAALY', 'AAAP', 'AAARF', 'AABA', 'AABB', 'AABC',
        'AABNF', 'AABVF', 'AAC', 'AAC', 'AAC-U', 'AAC-WS', 'AAC1', 'AACAF', 'AACAY', 'AACB', 'AACC', 'AACE', 'AACEU',
        'AACEY', 'AACH', 'AACIQ', 'AACMF', 'AACMZ', 'AACOU', 'AACPF', 'AACPU', 'AACQ', 'AACQU', 'AACQW']
    
for ticker in test:
    try:
        marketCap = pdr.get_quote_yahoo(ticker)['marketCap']
        print(marketCap)
    except:
        print('Error with: ', ticker)
  

I tried pdr.get_quote_yahoo wrapper for a single stock and it works well, but not with the list that I'm using. What went wrong with my code?

The following works:

marketCap = pdr.get_quote_yahoo('A')['marketCap'])
print(marketCap)

Solution

  • Actually, your code works, albeit with errors. Simple reason - many tickers don't correspond to any known stocks. My result for your code is as follows:

        A    44845838336
        Name: marketCap, dtype: int64
        AA    6901355520
        Name: marketCap, dtype: int64
        Error with:  AA-W
        Error with:  AAAB
        Error with:  AAAG
        Error with:  AAAGY
        Error with:  AAAIF
        AAALF    1551833984
        Name: marketCap, dtype: int64
        AAALY    1662833024
        Name: marketCap, dtype: int64
        Error with:  AAAP
        Error with:  AAARF
        Error with:  AABA
        AABB    46516112
        Name: marketCap, dtype: int64
        Error with:  AABC
        Error with:  AABNF
        AABVF    31826214
        Name: marketCap, dtype: int64
        AAC    1218749952
        Name: marketCap, dtype: int64
        AAC    1218749952
        Name: marketCap, dtype: int64
        Error with:  AAC-U
        Error with:  AAC-WS
        Error with:  AAC1
        AACAF    9106046976
        Name: marketCap, dtype: int64
        AACAY    9020131328
        Name: marketCap, dtype: int64
        Error with:  AACB
        Error with:  AACC
        Error with:  AACE
        Error with:  AACEU
        Error with:  AACEY
        Error with:  AACH
        Error with:  AACIQ
        Error with:  AACMF
        Error with:  AACMZ
        Error with:  AACOU
        Error with:  AACPF
        Error with:  AACPU
        AACQ    904719296
        Name: marketCap, dtype: int64
        Error with:  AACQU
        Error with:  AACQW
    

    As you can see, to some you have values, and to most others you have errors. When I checked for a couple of tickers, in finance.yahoo.com, such as AA-W, AAAB, I don't see any stocks corresponding to that ticker symbol. For example, AACQW doesn't correspond to any known stocks, while AACQ (Equity NCM) and AACQU (Equity NMS) are. In fact, both AACQ and AACQU are for Origin Materials Inc.

    There are some stocks for which yahoo APIs doesn't fetch results, but the web application of finance.yahoo.com fetches results. For example, the market cap of 904.719M for AACQ, you have the web result as follows:

    Web Results for AACQ

    For such stocks, you may want to use some web screen scraping apps to get results, but they may not be accurate (they might report the result such as 1.29T). Here is the link for one such app.