Polygon.io¶
Polygon.io’s market data API is directly integrated within lightweight-charts-python, and is easy to use within the library.
- class PolygonAPI¶
This class should be accessed from the
polygonattribute, which is contained within all chart types.chart.polygon.<method>The
stock,option,index,forex, andcryptomethods ofchart.polygonhave common parameters:timeframe: The timeframe to be used ('1min','5min','H','2D','5W'etc.)start_date: The start date given in the formatYYYY-MM-DD.end_date: The end date given in the same format. By default this is'now', which uses the time now.limit: The maximum number of base aggregates to be queried to create the aggregate results.live: When set toTrue, a websocket connection will be used to update the chart or subchart in real-time.These methods will also return a boolean representing whether the request was successful.
The
websocketslibrary is required when using live data.Important
When using live data and the standard
showmethod, theblockparameter must be set toTruein order for the data to congregate on the chart (chart.show(block=True)).show_asynccan also be used with live data.For Example:
from lightweight_charts import Chart if __name__ == '__main__': chart = Chart() chart.polygon.api_key('<API-KEY>') chart.polygon.stock( symbol='AAPL', timeframe='5min', start_date='2023-06-09' ) chart.show(block=True)
- api_key(key: str)¶
Sets the API key for the chart. Subsequent
SubChartobjects will inherit the API key given to the parent chart.
- stock(symbol: str, timeframe: str, start_date: str, end_date: str, limit: int, live: bool) bool¶
Requests and displays stock data pulled from Polygon.io.
async_stockcan also be used.
- option(symbol: str, timeframe: str, expiration: str, right: 'C' | 'P', strike: NUM, end_date: str, limit: int, live: bool) bool¶
Requests and displays option data pulled from Polygon.io.
A formatted option ticker (SPY251219C00650000) can also be given to the
symbolparameter, allowing forexpiration,right, andstriketo be left blank.async_optioncan also be used.
- index(symbol: str, timeframe: str, start_date: str, end_date: str, limit: int, live: bool) bool¶
Requests and displays index data pulled from Polygon.io.
async_indexcan also be used.
- forex(fiat_pair: str, timeframe: str, start_date: str, end_date: str, limit: int, live: bool) bool¶
Requests and displays a forex pair pulled from Polygon.io.
The two currencies should be separated by a ‘-’ (
USD-CAD,GBP-JPY, etc.).async_forexcan also be used.
- crypto(crypto_pair: str, timeframe: str, start_date: str, end_date: str, limit: int, live: bool) bool¶
Requests and displays a crypto pair pulled from Polygon.io.
The two currencies should be separated by a ‘-’ (
BTC-USD,ETH-BTC, etc.).async_cryptocan also be used.
- log(info: bool)¶
If
True, informational log messages (connection, subscriptions etc.) will be displayed in the console.Data errors will always be shown in the console.
- class PolygonChart(api_key: str, num_bars: int, limit: int, end_date: str, timeframe_options: tuple, security_options: tuple, live: bool)¶
The
PolygonChartprovides an easy and complete way to use the Polygon.io API within lightweight-charts-python.This object requires the
websocketslibrary for live data.All data is requested within the chart window through searching and selectors.
As well as the parameters from the Chart object, PolygonChart also has the parameters:
api_key: The user’s Polygon.io API key.num_bars: The target number of bars to be displayed on the chartlimit: The maximum number of base aggregates to be queried to create the aggregate results.end_date: The end date of the time window.timeframe_options: The selectors to be included within the timeframe selector.security_options: The selectors to be included within the security selector.live: If True, the chart will update in real-time.
For Example:
from lightweight_charts import PolygonChart if __name__ == '__main__': chart = PolygonChart( api_key='<API-KEY>', num_bars=200, limit=5000, live=True ) chart.show(block=True)

- polygon.get_bar_data(ticker: str, timeframe: str, start_date: str, end_date: str, limit: int = 5_000) pd.DataFrame¶
Module function which returns a formatted Dataframe of the requested aggregate data.
tickershould be prefixed for the appropriate security type (eg.I:NDX)
- async polygon.subscribe(ticker: str, sec_type: SEC_TYPE, func: callable, args: tuple, precision=2)¶
Subscribes the given callable to live data from polygon. emitting a dictionary (and any given arguments) to the function.
- async polygon.unsubscribe(func: callable)¶
Unsubscribes the given function from live data.
The ticker will only be unsubscribed if there are no additional functions that are currently subscribed.