Skip to main content

A Complete Python Wrapper for Polygon.io APIs.

Project description

polygon: A Polygon.io API Wrapper

Discord Documentation Status pypi CodeFactor ff fd Licensess gh

what is polygon

polygon is a Complete Python Wrapper for Polygon.io APIs. It offers simple and elegant programmatic access over each endpoint. Functionalities include but not limited to:

  • Stocks and Options data
  • Forex and Crypto data
  • Websocket streaming (both callback and async based)
  • Market Info, News, Holidays, Schedules, Tickers, Conditions, Dividends, Splits
  • Async support for REST endpoints
  • Built In stream reconnection functionality (async streamer only, in progress for callback streamer)
  • Complete Pagination support (with internal merging of responses)
  • Better Aggregate Bars functionality (useful for historical OCHLV)

and more...

How do I use polygon

The complete description of everything you need to know is available in the Documentation which has answers to any question you might have with example uses included wherever needed. Docs is a must-read for most people.

  • For examples to get started with, see the examples
  • For an advanced use case example code involving redis queue and postgresql database, check out MarketMakerLite/polygon
  • The lib is also officially supported by the popular TA library pandas-ta as an underlying data source instead of the default yfinance. See relevant docs to know how to use

Here is a quick setup guide with a few examples

Before you do anything, you'll need to have a polygon account and get your API key. Visit your dashboard to get your API key.

Next, you'd need to install polygon

pip install polygon

and You're good to Go! A few quick examples.

Getting a stock's previous day OCHLV

import polygon

api_key = 'YOUR_KEY'

stocks_client = polygon.StocksClient(api_key)

previous_close = stocks_client.get_previous_close('AMD')

print(previous_close)

An Async example for REST endpoints - previous close

import polygon
import asyncio

async def main():
    api_key = 'YOUR_KEY'
    
    stocks_client = polygon.StocksClient(api_key, True)
    
    previous_close = await stocks_client.get_previous_close('AMD')
    await stocks_client.close()  # Recommended to close the httpx session when it's not needed. 
    print(previous_close)

if __name__ == '__main__':
    asyncio.run(main())

A streaming example (callback based)

import polygon
from polygon.enums import StreamCluster

def my_own_message_handler(ws, msg):
    print(f'msg received: {msg}')

def main():
    api_key = 'YOUR_KEY'

    stream_client = polygon.StreamClient(api_key, StreamCluster.STOCKS, on_message=my_own_message_handler)
    stream_client.start_stream_thread()
    stream_client.subscribe_stock_trades(['AMD', 'NVDA'])

if __name__ == '__main__':
    main()

An Async streaming example

import asyncio
import polygon
from polygon.enums import StreamCluster

async def stock_trades_handler(msg):   # it is possible to create one common message handler for different services.
    print(f'msg received: {msg}')
    
async def main():
    api_key = 'YOUR_KEY'
    
    stream_client = polygon.AsyncStreamClient(api_key, StreamCluster.STOCKS)
    
    await stream_client.subscribe_stock_trades(['AMD', 'NVDA'], stock_trades_handler)
    
    while 1:
        await stream_client.handle_messages()  # the lib provides auto reconnect functionality. See docs for info
if __name__ == '__main__':
    asyncio.run(main())

This only scratches the surface of the library.

See the Documentation to start using the library with its full functionalities.

what if I need help?

We have a Discord Server. Join in to ask a question, share your ideas or observations or to just chat with interesting people, maybe just for lurking :eyes:

See Getting Help or you can also start a quick discussion

Quick Links for Speed Runners

Anything else?

Bug reports, suggestions and pull requests are always welcome.

See Contributing if you wish to contribute.

Read This before raising a bug.

polygon is released under the MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

polygon-1.0.3.tar.gz (62.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

polygon-1.0.3-py2.py3-none-any.whl (69.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file polygon-1.0.3.tar.gz.

File metadata

  • Download URL: polygon-1.0.3.tar.gz
  • Upload date:
  • Size: 62.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.10

File hashes

Hashes for polygon-1.0.3.tar.gz
Algorithm Hash digest
SHA256 8e50d4d427e645f423616cae0b757dbf0a50d1920230c65e841664c862619d16
MD5 b4bbe7d5ca1427896411bcad8a3f7bda
BLAKE2b-256 a93494f92479f88e40493a9f7cc8ef7a510b7e7dda5993c70d936ffced5ac20e

See more details on using hashes here.

File details

Details for the file polygon-1.0.3-py2.py3-none-any.whl.

File metadata

  • Download URL: polygon-1.0.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 69.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.10

File hashes

Hashes for polygon-1.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5d1c48f6b06a9265c73991f5de447808a434a38cf176bb8d3357c51143246cb0
MD5 53d777398f945b57b67aba932be0b1a0
BLAKE2b-256 1a161ba577e91d9ab2e238edc3d72b99c1355c6fc3512a9bc09b664213a5e07d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page