Yet another unofficial Yahoo Finance API library but with concurrent requests.
Index
Pyhoo requires Python >= 3.8
pip install pyhooPyhoo is simple:
import pyhoo
tickers = ['FB', 'AAPL', 'AMZN', 'GOOGL']
start = '2020-02-01'
end = '2020-11-02'
stock_prices = pyhoo.get('chart', tickers, start=start, end=end, granularity="1d")
financial_reports = pyhoo.get('fundamentals', tickers, start=start, end=end)
options = pyhoo.get('options', tickers, strikeMax=400.0)The result of pyhoo.get is a formatted pandas.DataFrame (here for stock prices):
| timestamp | high | low | volume | open | close | adjclose | currency | symbol | exchangeName | instrumentType | regularMarketPrice | ... | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1580481000 | 208.69 | 201.06 | 31359900 | 208.43 | 201.91 | 201.91 | USD | FB | NMS | EQUITY | 286.95 | ... |
| 1 | 1580740200 | 205.14 | 202.5 | 15510500 | 203.44 | 204.19 | 204.19 | USD | FB | NMS | EQUITY | 286.95 | ... |
| 2 | 1580826600 | 210.6 | 205.2 | 19628900 | 206.62 | 209.83 | 209.83 | USD | FB | NMS | EQUITY | 286.95 | ... |
| 3 | 1580913000 | 212.73 | 208.71 | 12538200 | 212.51 | 210.11 | 210.11 | USD | FB | NMS | EQUITY | 286.95 | ... |
| 4 | 1580999400 | 211.19 | 209.34 | 10567500 | 210.47 | 210.85 | 210.85 | USD | FB | NMS | EQUITY | 286.95 | ... |
| 5 | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
Pyhoo is fast, it uses concurrency to fire multiple requests at the same time. You can request all the tickers of the S&P500 in one shot.
Pyhoo is still in development, feel free to add more endpoints thanks to the Config object !
Currently, it supports three endpoints:
chart, for OHLC data, basically stock pricesfundamentals, for financial data about the firm, see the list of available reportsoptions, for detailed information on each call and put at each strike on specific tickers
For each endpoint, you must specify a ticker or a list of tickers. Every ticker displayed in Yahoo Finance is valid.
The available parameters for the chart enpoint are :
start[str] (required): date ('%Y-%m-%d') from which to get the stock pricesend[str] (required): maximum date ('%Y-%m-%d') for the stock pricesgranularity[str] (optional, defaults to'1d'): data granularity, must be one of['1m', '2m', '5m', '15m', '30m', '1h', '1d', '5d', '1w', '1mo', '3mo'](ex:'1m'gives minute by minute data)
The available parameters for the fundamentals enpoint are :
start[str] (required): date ('%Y-%m-%d') from which to get the reportsend[str] (required): maximum date ('%Y-%m-%d') for the reportstype[list[str]] (defaults to all types): name of the financial report to get, see the list of available reports for the full list. Each name must be prefixed with the specific frequency (annual,monthly,quarterly). Ex: to get annual diluted EPS and querterly gros profit -> [annualDilutedEPS,quarterlyGrossProfit].
The available parameters for the options enpoint are :
start[str] (optional): date ('%Y-%m-%d') of the option emissionend[str] (optional): date ('%Y-%m-%d') of the option expirationstrikeMax[float] (optional): filter options with strike price abovestrikeMaxstrikeMin[float] (optional): filter options with strike price belowstrikeMin
By default, it retrieves every current option, regarding of its strike or expiration date.
If running from a Jupyter Notebook, you may encounter the following error:
RuntimeError: asyncio.run() cannot be called from a running event loopThis is because Jupyter Notebooks are running themselves in an event loop, and it is a known issue with asyncio.run.
There is a workaround, a bit hacky but gets the job done, using nest_asyncio.
pip install nest_asyncioThen in the Notebook, before calling pyhoo.get:
import nest_asyncio
nest_asyncio.apply()And you should be ok !
Contributions are welcome !
Pyhoo uses poetry as package manager. You can find the installation instructions here.
It is recommended to install the virtual environment in the project folder if you use VSCode, to help the linter resolve imports:
poetry config virtualenvs.path --unset
poetry config virtualenvs.in-project trueOnce Poetry is configured, you can install required dependencies with:
poetry installThe CI enforces strict typing, linting and coverage.