pythonyahoo-financeyfinance

Get historic market cap from yfinance


I am using yfinance in order to automatically generate some financial overviews of different company stocks. The problem I am having is that the module does not seem to provide the market cap over time.

Is it possible to get historic data of the market cap of a stock using yfinance?

I have figured out how to get the current market cap

import yfinance as yf

ticker = yf.Ticker('AAPL')
current_market_cap = ticker.info['marketCap']

but not how to get it for e.g. the last 4 years. One can easily get the last 4 years of cash flow or financial data with

ticker.financials
ticker.cashflow

Alternatively, is there a better module other than yfinance to access this data from a python script?


Solution

  • You cannot query yfinance for this data, but you can calculate it from data that they provide, so long as you can stick to quarterly calculations.

    You can gather the day that their quarterly statements are made by:

    ticker = yf.Ticker('AAPL')
    all_dates = ticker.quarterly_income_stmt.columns
    

    And you could use those those dates to query for total shares (date will require reformatting that I did not want to take the time to do; manually its intuitive, just 'YYYY-MM-DD'):

    total_shares = ticker.quarterly_income_stmt[date]['Basic Average Shares']
    

    Then take the total shares, gather the stock price, and calculate for market cap by doing total shares * stock price:

    stock_price = ticker.history(start='2023-09-26', end='2023-09-26')
    market_cap = stock_price * total_shares
    

    EDIT: just noticed your question about other modules. There are other modules, but you likely need to pay (and often have to pay quite a lot) for anything better than yfinance.