• Stars
    star
    801
  • Rank 56,886 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created about 5 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The official Python client library for the Polygon REST and WebSocket API.

PyPI version Docs

Polygon Python Client - WebSocket & RESTful APIs

Welcome to the official Python client library for the Polygon REST and WebSocket API. To get started, please see the Getting Started section in our documentation, view the examples directory for code snippets, or the blog post with video tutorials to learn more.

Install

Please use pip to install or update to the latest stable version.

pip install -U polygon-api-client

Requires Python >= 3.8.

Getting started

To get started, please see the Getting Started section in our docs, view the examples directory for code snippets, or view the blog post with videos to learn more.

REST API Client

Import the RESTClient.

from polygon import RESTClient

Create a new client with your API key

client = RESTClient(api_key="<API_KEY>")

Using the Client

Request data using client methods.

ticker = "AAPL"

# List Aggregates (Bars)
aggs = []
for a in client.list_aggs(ticker=ticker, multiplier=1, timespan="minute", from_="2023-01-01", to="2023-06-13", limit=50000):
    aggs.append(a)

print(aggs)

# Get Last Trade
trade = client.get_last_trade(ticker=ticker)
print(trade)

# List Trades
trades = client.list_trades(ticker=ticker, timestamp="2022-01-04")
for trade in trades:
    print(trade)

# Get Last Quote
quote = client.get_last_quote(ticker=ticker)
print(quote)

# List Quotes
quotes = client.list_quotes(ticker=ticker, timestamp="2022-01-04")
for quote in quotes:
    print(quote)

Note: For parameter argument examples check out our docs. All required arguments are annotated with red asterisks " * " and argument examples are set. Check out an example for Aggregates(client.get_aggs) here

WebSocket Client

Import classes

from polygon import WebSocketClient
from polygon.websocket.models import WebSocketMessage
from typing import List

Using the client

Create a new client with your API key and subscription options.

# Note: Multiple subscriptions can be added to the array 
# For example, if you want to subscribe to AAPL and META,
# you can do so by adding "T.META" to the subscriptions array. ["T.AAPL", "T.META"]
# If you want to subscribe to all tickers, place an asterisk in place of the symbol. ["T.*"]
ws = WebSocketClient(api_key=<API_KEY>, subscriptions=["T.AAPL"])

Create a handler function and run the WebSocket.

def handle_msg(msg: List[WebSocketMessage]):
    for m in msg:
        print(m)

ws.run(handle_msg=handle_msg)

Check out more detailed examples here.

Launchpad REST API Client

Users of the Launchpad product will need to pass in certain headers in order to make API requests using the RequestOptionBuilder. Example can be found here.

Import classes

from polygon import RESTClient
from polygon.rest.models.request import RequestOptionBuilder

Using the client

Create client and set options

# create client
c = RESTClient(api_key="API_KEY")

# create request options
options = RequestOptionBuilder().edge_headers(
    edge_id="YOUR_EDGE_ID",  # required
    edge_ip_address="IP_ADDRESS",  # required
)

Request data using client methods.

# get response
res = c.get_aggs("AAPL", 1, "day", "2022-04-04", "2022-04-04", options=options)

# do something with response

Checkout Launchpad readme for more details on RequestOptionBuilder here

Launchpad WebSocket Client

from polygon import WebSocketClient
from polygon.websocket.models import WebSocketMessage
from polygon.websocket.models.common import Feed, Market
from typing import List

ws = WebSocketClient(api_key="API_KEY",feed=Feed.Launchpad,market=Market.Stocks, subscriptions=["AM.AAPL"])

def handle_msg(msg: List[WebSocketMessage]):
    for m in msg:
        print(m)

ws.run(handle_msg=handle_msg)

Contributing

If you found a bug or have an idea for a new feature, please first discuss it with us by submitting a new issue. We will respond to issues within at most 3 weeks. We're also open to volunteers if you want to submit a PR for any open issues but please discuss it with us beforehand. PRs that aren't linked to an existing issue or discussed with us ahead of time will generally be declined. If you have more general feedback or want to discuss using this client with other users, feel free to reach out on our Slack channel.

Development

If you plan to contribute by developing new features then you will need to install certain dependencies.

Poetry

Poetry is a packaging and dependency manager for Python. Installation instructions can be found on their website. Once installed run poetry install to install the required dependencies. This step should be run after incorporating new upstream changes.

Makefile

Our Makefile has the common operations needed when developing on this repo. Running tests and linting can both be run through our Makefile. Just run make help to see the list of available commands.

If you're using pyenv to manage active Python versions then you might need to launch a Poetry shell before running Make commands in order to actually use your chosen Python version. This is because Poetry uses the system Python version by default.

poetry shell # start shell
poetry install # install deps

make test # run your make commands

Release planning

This client will attempt to follow the release cadence of our API. When endpoints are deprecated and newer versions are added, the client will maintain two methods in a backwards compatible way (e.g. list_trades and list_trades_v4(...)). When deprecated endpoints are removed from the API, we'll rename the versioned method (e.g. list_trades_v4(...) -> list_trades(...)), remove the old method, and release a new major version of the client.

The goal is to give users ample time to upgrade to newer versions of our API before we bump the major version of the client, and in general, we'll try to bundle breaking changes like this to avoid frequent major version bumps.

Exceptions to this are:

  • Methods under client.vx. These are expiremental.
  • Methods that start with _*. We use these internally.
  • Type annotations. We may modify these based on our JSON responses.
  • We may add model fields.

More Repositories

1

client-js

The official JS client library for the Polygon REST and WebSocket API.
TypeScript
183
star
2

client-go

The official Go client library for the Polygon REST and WebSocket API.
Go
127
star
3

go-app-ticker-wall

Scalable Stock Ticker Tape
Go
107
star
4

tradingview-adapter

JS Library for using Polygon.io with TradingView charting.
JavaScript
87
star
5

client-examples

Examples of how to connect to Polygon.io
85
star
6

client-jvm

The official JVM client library SDK, written in Kotlin, for accessing the Polygon REST and WebSocket API.
Kotlin
50
star
7

issues

Quickly track and report problems with polygon.io
29
star
8

client-php

The official PHP client library for the Polygon REST APIs.
PHP
28
star
9

xbrl-parser

A Go library to parse xbrl documents into their facts, contexts, and units.
Go
22
star
10

errands-server

Errands API Server. A language agnostic, HTTP based queue
Go
19
star
11

client-cs

C#
9
star
12

selenium-on-kubernetes

Kube files for deploying Selenium Hub and Nodes on Kubernetes
8
star
13

ui-swagger

Swagger UI for Polygon.io Docs.
JavaScript
7
star
14

go-multicast-dump

Dump multicast data to stdout
Go
4
star
15

errands-js

NodeJS Client for Errands
JavaScript
3
star
16

wordpress-plugins

Wordpress Plugins by Polygon.io
PHP
2
star
17

contentful-markdown-plugin

A Contentful Plugin to replace images created in the markdown editor with SEO-friendly HTML img tags that include alt, height, width, and title attributes
TypeScript
2
star
18

client-r

1
star
19

legendary-sniffle

People like forking this repo
1
star
20

errands-go

Golang client for Errands API server
Go
1
star