yahoofinancials

repository·master·Indexed 21 days ago

https://github.com/jecsand/yahoofinancials

A Python module for pulling fundamental and technical financial data for stocks, crypto, and forex from Yahoo Finance. It supports single or batch ticker requests and provides raw or reformatted JSON data. Key features include retrieving financial statements, historical price data, stock recommendations, and key statistics, with support for concurrency, proxies, and flat-format financial statements.

Tokens
3.1K
Snippets
18
Records
19
Agent score
26%

What's inside yahoofinancials

  1. Configure YahooFinancials for concurrency, proxies, and country

    master

    When initializing YahooFinancials, you can pass several optional parameters to handle large batches of tickers or network restrictions:

    • concurrent=True: Enables asynchronous execution.
    • max_workers: Sets the number of workers for concurrent requests.
    • country: Specifies the country (e.g., "US").
    • proxies: A list of proxy addresses (e.g., ["mysuperproxy.com:5000"]).
    from yahoofinancials import YahooFinancials
    
    tickers = ['AAPL', 'GOOG', 'C']
    # Concurrent execution with specific workers and country
    yahoo_financials = YahooFinancials(tickers, concurrent=True, max_workers=8, country="US")
    
    # Concurrent execution with proxies
    proxy_addresses = [ "mysuperproxy.com:5000", "mysuperproxy.com:5001"]
    yahoo_financials = YahooFinancials(tickers, concurrent=True, proxies=proxy_addresses)
  2. Initialize YahooFinancials with single or multiple tickers

    master

    The YahooFinancials class constructor accepts either a single ticker string or a list of ticker strings. This allows you to create separate instances for different asset groupings (e.g., tech stocks vs. cryptocurrencies).

    from yahoofinancials import YahooFinancials
    
    # Single ticker
    yahoo_financials = YahooFinancials('AAPL')
    
    # Multiple tickers
    tech_stocks = ['AAPL', 'MSFT', 'INTC']
    yahoo_financials_tech = YahooFinancials(tech_stocks)
  3. Install yahoofinancials via pip

    master

    Install the module using pip for Linux, Mac, or Windows. For Windows users, if the python command is not recognized in your command prompt, try using python -m pip or py -m pip.

    # Linux/Mac
    $ pip install yahoofinancials
    
    # Windows
    > python -m pip install yahoofinancials
  4. Use flat_format for financial statements

    master

    As of version 1.20, you can pass flat_format=True to the YahooFinancials constructor. When enabled, financial statement data is returned as a dictionary where the keys are the reporting dates, instead of the default list format. This is useful for easier data manipulation.

    # Financial statements will return as a dict keyed by date
    yahoo_financials = YahooFinancials(tickers, flat_format=True)
  5. Get financial statements with get_financial_stmts()

    master

    Retrieve balance sheets, income statements, or cash flow statements.

    Parameters:

    • frequency: 'annual' or 'quarterly'. Note that quarterly returns the last 4 periods and annual returns the last 3.
    • statement_type: 'income', 'balance', 'cash', or a list containing these strings.
    • reformat: (Optional, default True) Set to False to receive unprocessed raw data from Yahoo Finance.
    from yahoofinancials import YahooFinancials
    
    yahoo_financials = YahooFinancials('AAPL')
    
    # Get quarterly balance sheet
    balance_sheet_data_qt = yahoo_financials.get_financial_stmts('quarterly', 'balance')
    
    # Get multiple statements at once
    all_statement_data_qt = yahoo_financials.get_financial_stmts('quarterly', ['income', 'cash', 'balance'])
  6. Retrieve financial statements (Income, Balance Sheet, Cash Flow)

    master

    Use get_financial_stmts(period, statement_type) to retrieve historical financial data.

    • period: Either 'annual' or 'quarterly'.
    • statement_type: Either 'income', 'balance', or 'cash'.

    The returned data is a dictionary containing the history of the requested statement type, keyed by the ticker symbol and then by the date.

    # Annual Income Statement
    yahoo_financials = YahooFinancials('AAPL')
    print(yahoo_financials.get_financial_stmts('annual', 'income'))
    
    # Annual Balance Sheet
    print(yahoo_financials.get_financial_stmts('annual', 'balance'))
    
    # Quarterly Cash Flow Statement
    yahoo_financials = YahooFinancials('C')
    print(yahoo_financials.get_financial_stmts('quarterly', 'cash'))
  7. Get daily dividend data

    master

    Use get_daily_dividend_data(start_date, end_date) to retrieve a history of dividend payments within a specific date range. Returns a dictionary where each symbol maps to a list of dividend events containing date, formatted_date, and amount.

    start_date = '1987-09-15'
    end_date = '1988-09-15'
    yahoo_financials = YahooFinancials(['AAPL', 'WFC'])
    print(yahoo_financials.get_daily_dividend_data(start_date, end_date))
  8. Get current pricing for multiple symbols

    master

    Use get_current_price() to retrieve the most recent price for one or more symbols provided during initialization. If multiple symbols are passed to YahooFinancials, the returned dictionary will map each symbol to its current price.

    # Get current prices for US Treasury yields
    yahoo_financials = YahooFinancials(['^TNX', '^IRX', '^TYX'])
    print(yahoo_financials.get_current_price())
  9. Get historical price data with get_historical_price_data()

    master

    Pulls historical pricing data for stocks, currencies, ETFs, mutual funds, U.S. Treasuries, cryptocurrencies, commodities, and indexes. The response includes relevant pricing event data like dividends and stock splits.

    Parameters:

    • start_date: String in 'YYYY-MM-DD' format.
    • end_date: String in 'YYYY-MM-DD' format.
    • time_interval: 'daily', 'weekly', or 'monthly'.
    from yahoofinancials import YahooFinancials
    
    yahoo_financials = YahooFinancials('AAPL')
    historical_stock_prices = yahoo_financials.get_historical_price_data('2008-09-15', '2018-09-15', 'weekly')
  10. Get key financial data

    master

    Use get_financial_data() to retrieve a condensed set of key financial ratios and metrics, such as ebitdaMargins, operatingCashflow, debtToEquity, and returnOnEquity.

    yahoo_financials = YahooFinancials("AAPL")
    print(yahoo_financials.get_financial_data())