Code Monkey home page Code Monkey logo

pyhoo's Introduction

Pyhoo

Code style: black Checked with mypy Build Status codecov

Yet another unofficial Yahoo Finance API library but with concurrent requests.

Index

  1. Installation
  2. Usage
    1. Demo
    2. Parameters
  3. Troubleshooting
  4. Contributing

Installation

Pyhoo requires Python >= 3.8

pip install pyhoo

Usage

Demo

Pyhoo 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 !

Parameters

Currently, it supports three endpoints:

  1. chart, for OHLC data, basically stock prices
  2. fundamentals, for financial data about the firm, see the list of available reports
  3. options, 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 prices
  • end [str] (required): maximum date ('%Y-%m-%d') for the stock prices
  • granularity [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 reports
  • end [str] (required): maximum date ('%Y-%m-%d') for the reports
  • type [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 emission
  • end [str] (optional): date ('%Y-%m-%d') of the option expiration
  • strikeMax [float] (optional): filter options with strike price above strikeMax
  • strikeMin [float] (optional): filter options with strike price below strikeMin

By default, it retrieves every current option, regarding of its strike or expiration date.

Troubleshooting

If running from a Jupyter Notebook, you may encounter the following error:

RuntimeError: asyncio.run() cannot be called from a running event loop

This 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_asyncio

Then in the Notebook, before calling pyhoo.get:

import nest_asyncio
nest_asyncio.apply()

And you should be ok !

Contributing

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 true

Once Poetry is configured, you can install required dependencies with:

poetry install

The CI enforces strict typing, linting and coverage.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.