Python wrapper for an unofficial Yahoo Finance API

dpguthrie, updated 🕥 2023-03-18 20:47:51

Python wrapper for an unofficial Yahoo Finance API

Build Status Coverage Package version Downloads


Documentation: https://yahooquery.dpguthrie.com

Interactive Demo: https://dpguthrie-yahooquery-streamlit-app-eydpjo.streamlit.app/

Source Code: https://github.com/dpguthrie/yahooquery

Blog Post: https://towardsdatascience.com/the-unofficial-yahoo-finance-api-32dcf5d53df


Overview

Yahooquery is a python interface to unofficial Yahoo Finance API endpoints. The package allows a user to retrieve nearly all the data visible via the Yahoo Finance front-end.

Some features of yahooquery:

  • Fast: Data is retrieved through API endpoints instead of web scraping. Additionally, asynchronous requests can be utilized with simple configuration
  • Simple: Data for multiple symbols can be retrieved with simple one-liners
  • User-friendly: Pandas Dataframes are utilized where appropriate
  • Premium: Yahoo Finance premium subscribers are able to retrieve data available through their subscription

Requirements

Python 2.7, 3.5+

  • Pandas - Fast, powerful, flexible and easy to use open source data analysis and manipulation tool
  • Requests - The elegant and simple HTTP library for Python, built for human beings.
  • Requests-Futures - Asynchronous Python HTTP Requests for Humans

Yahoo Finance Premium Subscribers

Selenium is only utilized to login to Yahoo, which is done when the user passes certain keyword arguments. Logging into Yahoo enables users who are subscribers to Yahoo Finance Premium to retrieve data only accessible to premium subscribers.

Installation

If you're a Yahoo Finance premium subscriber and would like to retrieve data available through your subscription, do the following:

bash pip install yahooquery[premium]

Otherwise, omit the premium argument:

bash pip install yahooquery

Example

The majority of the data available through the unofficial Yahoo Finance API is related to a company, which is represented in yahooquery as a Ticker. You can instantiate the Ticker class by passing the company's ticker symbol. For instance, to get data for Apple, Inc., pass aapl as the first argument to the Ticker class:

```python from yahooquery import Ticker

aapl = Ticker('aapl')

aapl.summary_detail ```

Multiple Symbol Example

The Ticker class also makes it easy to retrieve data for a list of symbols with the same API. Simply pass a list of symbols as the argument to the Ticker class.

```python from yahooquery import Ticker

symbols = ['fb', 'aapl', 'amzn', 'nflx', 'goog']

faang = Ticker(symbols)

faang.summary_detail ```

License

This project is licensed under the terms of the MIT license.

Issues

Converting data in a dictionary format into one in Pandas DataFrame

opened on 2023-03-25 13:03:37 by informatism

Is your feature request related to a problem? Please describe. When obtaining the data via yahooquery, there seem to be Python dictionary and Pandas DataFrame formats. I would like to be able either to convert between those two formats OR to obtain data in whichever format.

``` aapl = Ticker('aapl') aapl.institution_ownership

aapl = Ticker('aapl') aapl.get_modules('institutionOwnership') ```

The advantage is that I can just call all_modules once and get every module (e.g., institutionOwnership and others) already. Saving requests and not reaching the rate limit.

Another advantage is that depending one when/why/how, I use one method or another. It'd be easier to compare two different data from the same ticker, etc.

Describe the solution you'd like Is there a way to convert data in a dictionary format into one in Pandas DataFrame (vice verse). For example, I can take aapl.get_modules('institutionOwnership') and turn it into the same DF format as aapl.institution_ownership

aapl = Ticker('aapl') dict_to_dataframe(aapl.get_modules('institutionOwnership'))

or

aapl = Ticker('aapl') data = aapl.all_modules dict_to_dataframe(data['institutionOwnership'])

In fact, some of these seems to be already available via functions in ticker class, e.g., to_dataframe.

Or perhaps there's already a way to do this :)

Describe alternatives you've considered Alternatively, I just call aapl.institution_ownership and other modules one-by-one. It's a bad since calling all_modules is just one call, so it would save everyone many requests.

Unable to retrieve historical data due to unexpected keyword argument 'tz'

opened on 2023-03-21 21:45:21 by rinkef

Hi, when trying to get historical data using Ticker I get the following error:

fromtimestamp() got an unexpected keyword argument 'tz'

Something seems to be going wrong with the timestamp in the pandas module. Upgrading the pandas module and the yahooquery module does not help.

Hence I can't extract historical data.

I am using Python 3.9

My very simple script

from yahooquery import Ticker aapl = Ticker('AAPL') data = aapl.history() print(data.head())

What am I missing here?

Full error: Traceback (most recent call last):

File ~\Miniconda3\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec exec(code, globals, locals)

File c:\data\python\git\personal-finance\yahooquery_test.py:14 data = aapl.history()

File ~\Miniconda3\lib\site-packages\yahooquery\ticker.py:1291 in history for i in range(len(dates) - 1):

File ~\Miniconda3\lib\site-packages\yahooquery\ticker.py:1320 in _historical_data_to_dataframe df["splits"].fillna(0, inplace=True)

File ~\Miniconda3\lib\site-packages\yahooquery\utils__init__.py:198 in _history_dataframe tz = data["meta"]["exchangeTimezoneName"]

File ~\Miniconda3\lib\site-packages\yahooquery\utils__init__.py:125 in _get_daily_index last_trade = pd.Timestamp.fromtimestamp(timestamp)

File pandas_libs\tslibs\timestamps.pyx:1129 in pandas._libs.tslibs.timestamps.Timestamp.fromtimestamp

TypeError: fromtimestamp() got an unexpected keyword argument 'tz'

Requesting a few additional datapoints from the Income Statement that Yahoo Query cannot currently pull.

opened on 2023-03-19 22:48:14 by Veran04

Is your feature request related to a problem? Please describe. Hello, I was hoping that somebody could add the following 3 indicators in the response of calls to p_income_statement for a given Ticker: gross profit margin, gross income growth, and net margin growth

Describe the solution you'd like I would be very appreciative if yahooquery could be updated to include functionality for pulling those 3 data points

Describe alternatives you've considered I tried adding them myself but manipulating the source code for yahoo query is still above my head in terms of code complexity

Additional context Add any other context or screenshots about the feature request here.

Removes trailing parameter from balance_sheet

opened on 2023-03-05 05:00:36 by kushalnl7

Resolves #150

I have removed "trailing=true" parameter from balance_sheet function. Please review it. Thanks!

`calendar_events` - fetch historic, not just upcoming

opened on 2023-02-27 21:22:45 by ValueRaider

dat.calendar_events currently returns the next earnings event, which I assume is same as shown on the quote page https://finance.yahoo.com/quote/AAPL?p=AAPL&.tsrc=fin-srch

But on the Earnings Calendar page https://finance.yahoo.com/calendar/earnings?symbol=AAPL it shows past dates too - these are useful, can they be fetched?

image

Getting older data - is there a usage limit?

opened on 2023-02-20 19:17:56 by EnigmaNZ

Describe the bug I apologise if this has already been covered but I couldn't find a title that appeared to match:

I am grabbing a bit of data using the different functions listed below (in one script) and am getting up to date information on the initial one function but older info in the later functions (compared with the live values in Yahoo Finance). I've listed below the functions used and whether up to date or not: Within financial_data: currentPrice = Up to Date totalCash = Up to Date totalDebt = Up to Date Within index_trend: +5y growth estimates = Out of Date Within key_stats: priceToBook = Out of Date enterpriseValue = Out of Date Within summary_detail: trailingPE = Out of Date priceToSalesTrailing12Months = Out of Date marketCap = Out of Date Within cash_flow: FreeCashFlow = Out of Date

To Reproduce Attached a text file with the for function that's pulling the data of 15 stocks

Expected behavior Up-to-date information

Desktop (please complete the following information): - OS: Ubuntu 22.04.1 LTS - Browser: Firefox - Version: 110.0

Releases

2.3.1 2023-03-18 20:46:00

FIx

  • Fixes for history method

v2.3.0 2023-01-22 19:40:51

Added

  • dividend_history method that returns historical dividends paid for a given symbol(s)

Fixed

  • history method has been refactored pretty heavily with the help of @maread99 (Thank you!). Timezone info is now included in the date column. Also, a dataframe will always be returned regardless of bad symbols existing. Previously, a dictionary was returned with the json response for the bad symbol(s) and dataframes for successful responses.

yahoo-finance-api yahoo-finance stock-data pandas market-data python finance stock-market api api-wrapper