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.
Before installing the Polygon Python client, ensure your environment has Python 3.8 or higher.
Please use pip to install or update to the latest stable version.
pip install -U polygon-api-client
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.
The free tier of our API comes with usage limitations, potentially leading to rate limit errors if these are exceeded. For uninterrupted access and to support larger data requirements, we recommend reviewing our subscription plans, which are tailored for a wide range of needs from development to high-demand applications. Refer to our pricing page for detailed information on limits and features to ensure a seamless experience, especially for real-time data processing.
Import the RESTClient.
from polygon import RESTClient
Create a new client with your API key
client = RESTClient(api_key="<API_KEY>")
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)
Many of the APIs in this client library support the use of additional filter parameters to refine your queries. Please refer to the specific API documentation for details on which filter parameters are supported for each endpoint. These filters can be applied using the following operators:
.gt
: greater than.gte
: greater than or equal to.lt
: less than.lte
: less than or equal to
Here's a sample code snippet that demonstrates how to use these filter parameters when requesting an Options Chain using the list_snapshot_options_chain
method. In this example, the filter parameters ensure that the returned options chain data will only include options with an expiration date that is greater than or equal to "2024-03-16" and a strike price that falls between 29 and 30 (inclusive).
options_chain = []
for o in client.list_snapshot_options_chain(
"HCP",
params={
"expiration_date.gte": "2024-03-16",
"strike_price.gte": 29,
"strike_price.lte": 30,
},
):
options_chain.append(o)
print(options_chain)
print(len(options_chain))
Also, please refer to the API documentation to get a full understanding of how the API works and the supported arguments. All required arguments are annotated with red asterisks " * " and argument examples are set.