npm 中文文档 npm 中文文档
指南
npmjs.com (opens new window)
指南
npmjs.com (opens new window)
  • 快速入门

    • npm 是什么?
    • npm 安装和更新
    • npm 防止权限错误
    • npm package.json 文件
    • npm 安装包
    • npm 更新包
    • npm 卸载包
    • npm 创建 Node.js 模块
    • npm 发布和更新包
    • npm 使用语义化版本
    • npm 使用 Dist-tags 标记包
    • npm 包和模块的了解
  • 命令行
  • 配置 npm

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


  1. ``` sh
  2. pip install polygon-api-client

  3. ```

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.

  1. ``` python
  2. from polygon import RESTClient
  3. ```

Create a new client with your API key

  1. ``` python
  2. client = RESTClient(api_key="<API_KEY>")
  3. ```

Using the Client


Request data using client methods.

  1. ``` python
  2. ticker = "AAPL"

  3. # List Aggregates (Bars)
  4. bars = client.get_aggs(ticker=ticker, multiplier=1, timespan="day", from_="2023-01-09", to="2023-01-10")
  5. for bar in bars:
  6.     print(bar)

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

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

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

  17. # List Quotes
  18. quotes = client.list_quotes(ticker=ticker, timestamp="2022-01-04")
  19. for quote in quotes:
  20.     print(quote)
  21. ```

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

  1. ``` python
  2. from polygon import WebSocketClient
  3. from polygon.websocket.models import WebSocketMessage
  4. from typing import List
  5. ```

Using the client


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

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

Create a handler function and run the WebSocket.

  1. ``` python
  2. def handle_msg(msg: List[WebSocketMessage]):
  3.     for m in msg:
  4.         print(m)

  5. ws.run(handle_msg=handle_msg)
  6. ```

Check out more detailed examples here.

Launchpad


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

  1. ``` python
  2. from polygon import RESTClient
  3. from polygon.rest.models.request import RequestOptionBuilder
  4. ```

Using the client


Create client and set options

  1. ``` python
  2. # create client
  3. c = RESTClient(api_key="API_KEY")

  4. # create request options
  5. options = RequestOptionBuilder().edge_headers(
  6.     edge_id="YOUR_EDGE_ID",  # required
  7.     edge_ip_address="IP_ADDRESS",  # required
  8. )
  9. ```

Request data using client methods.

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

  4. # do something with response
  5. ```

Checkout Launchpad readme for more details on RequestOptionBuilder here

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.

  1. ``` shell
  2. poetry shell # start shell
  3. poetry install # install deps

  4. make test # run your make commands
  5. ```

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 beforewe 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.
Last Updated: 2023-07-11 08:35:04