Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
a363476
copy rfc over (#114)
Apr 25, 2022
6b4ee83
Add CI (#115)
Apr 25, 2022
406549d
Add unittest (#116)
Apr 26, 2022
19926a9
add better types (#118)
Apr 26, 2022
c2c40be
Quotes (#117)
Darcy-Linde Apr 26, 2022
b724ad3
Darcy quotes (#119)
Darcy-Linde Apr 26, 2022
9ef7862
add httpResponse to get_last_quote (#120)
Darcy-Linde Apr 26, 2022
2a65a1a
Markets (#121)
Darcy-Linde Apr 26, 2022
6c05495
Tickers (#122)
Darcy-Linde Apr 27, 2022
6c4e7dc
Splits (#124)
Darcy-Linde Apr 27, 2022
c28320c
allow datetime or int for aggs, lint tests (#123)
Apr 27, 2022
2e48c25
Dividends (#125)
Darcy-Linde Apr 27, 2022
02d15ea
add sphinx docs (#126)
Apr 28, 2022
b98052c
Conditions And Exchanges (#127)
Darcy-Linde Apr 28, 2022
a6411de
try fix readthedocs build
clickingbuttons Apr 28, 2022
bbe5451
try fix readthedocs build (2)
clickingbuttons Apr 28, 2022
71a5e4c
try fix readthedocs build (3)
clickingbuttons Apr 28, 2022
601ac5e
try fix readthedocs build (4)
clickingbuttons Apr 28, 2022
a473264
Rst update and quick fixes (#129)
Darcy-Linde Apr 28, 2022
1b42f26
try fix readthedocs build (5)
clickingbuttons Apr 28, 2022
6aaa58e
aggs endpoints (#128)
mcdayoub Apr 28, 2022
db03e5b
update readme
clickingbuttons Apr 28, 2022
11661eb
remove build badge
clickingbuttons Apr 28, 2022
0aa8bc5
clean readme
clickingbuttons Apr 28, 2022
e4065d7
readme raw response example
clickingbuttons Apr 28, 2022
300cc99
fit on half 1440p screen
clickingbuttons Apr 28, 2022
e047ea9
use setupclass (#130)
Apr 28, 2022
82dfa1d
remove module index
clickingbuttons Apr 28, 2022
94881d6
set docs env var in conf.py
clickingbuttons Apr 28, 2022
53ab30e
Date param support (#131)
Apr 28, 2022
eecd477
Tickers tests (#133)
Darcy-Linde Apr 28, 2022
75b0aa2
Trade endpoints (#134)
mcdayoub Apr 28, 2022
4e0f66e
Market tests (#135)
Darcy-Linde Apr 29, 2022
0b6b715
add user agent string (#137)
Apr 29, 2022
0e0d09b
Trades tests (#136)
mcdayoub Apr 29, 2022
06334a3
add missing automethod docs (#138)
Apr 29, 2022
49962fb
Add tests for splits, dividends, conditions, exchanges (#139)
Darcy-Linde Apr 29, 2022
c1ed004
Fix pagination and tests (#140)
Darcy-Linde Apr 29, 2022
d92f678
add snapshot (#141)
Apr 29, 2022
bb303aa
Snapshot tests (#143)
Darcy-Linde Apr 29, 2022
b4d81cd
Snapshot rst (#144)
Darcy-Linde Apr 29, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 0 additions & 38 deletions .drone.yml

This file was deleted.

33 changes: 33 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: lint
on:
push:
tags:
- v*
branches:
- v1
pull_request:
permissions:
contents: read
jobs:
lint:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10']
name: Lint ${{ matrix.python-version }}
steps:
- uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Setup Poetry
uses: abatilo/actions-poetry@v2.0.0
- name: Install pypi deps
run: poetry install
- name: Style lint
run: make style
- name: Static lint
run: make static
if: always()
30 changes: 30 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: unittest
on:
push:
tags:
- v*
branches:
- v1
pull_request:
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10']
name: Lint ${{ matrix.python-version }}
steps:
- uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Setup Poetry
uses: abatilo/actions-poetry@v2.0.0
- name: Install pypi deps
run: poetry install
- name: Unit tests
run: make test
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ venv/
*.log

# Sphinx documentation
docs/_build/
docs/build/

# PyBuilder
target/
Expand Down
11 changes: 11 additions & 0 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# https://docs.readthedocs.io/en/latest/config-file/index.html#configuration-file
version: 2

# Build documentation in the docs/ directory with Sphinx
sphinx:
configuration: docs/source/conf.py

python:
install:
- requirements: docs/requirements.txt

37 changes: 37 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
.DEFAULT_GOAL := help
TARGET_MAX_CHAR_NUM := 20

GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
RESET := $(shell tput -Txterm sgr0)

.PHONY: help lint style static test

## Show help
help:
@awk '/^[a-zA-Z\-_0-9]+:/ { \
helpMessage = match(lastLine, /^## (.*)/); \
if (helpMessage) { \
helpCommand = substr($$1, 0, index($$1, ":")-1); \
helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \
printf " ${YELLOW}%-$(TARGET_MAX_CHAR_NUM)s${RESET} ${GREEN}%s${RESET}\n", helpCommand, helpMessage; \
} \
} \
{ lastLine = $$0 }' $(MAKEFILE_LIST)

## Check code style
style:
poetry run black $(if $(CI),--check,) polygon tests

## Check static types
static:
poetry run mypy polygon tests

## Check code style and static types
lint: style static

## Run unit tests
test:
poetry run python -m unittest discover -s tests

110 changes: 19 additions & 91 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,115 +1,43 @@
[![Build Status](https://drone.polygon.io/api/badges/polygon-io/client-python/status.svg)](https://drone.polygon.io/polygon-io/client-python)
[![PyPI version](https://badge.fury.io/py/polygon-api-client.svg)](https://badge.fury.io/py/polygon-api-client)
[![Docs](https://readthedocs.org/projects/polygon-api-client/badge/?version=latest)](https://polygon-api-client.readthedocs.io/en/latest/)

# Polygon Python Client - WebSocket & RESTful APIs

Python client for the Polygon.io [Stocks API](https://polygon.io)

## Getting Started

For a basic product overview, check out our [setup and use documentation](https://polygon.io/sockets)
Python client for the [Polygon.io API](https://polygon.io).

## Getting started
### Install

`pip install polygon-api-client`

`polygon-api-client` supports python version >= 3.6
Requires python version >= 3.7

## Simple WebSocket Demo
### Getting aggs
```python
import time

from polygon import WebSocketClient, STOCKS_CLUSTER


def my_custom_process_message(message):
print("this is my custom message processing", message)


def my_custom_error_handler(ws, error):
print("this is my custom error handler", error)


def my_custom_close_handler(ws):
print("this is my custom close handler")


def main():
key = 'your api key'
my_client = WebSocketClient(STOCKS_CLUSTER, key, my_custom_process_message)
my_client.run_async()

my_client.subscribe("T.MSFT", "T.AAPL", "T.AMD", "T.NVDA")
time.sleep(1)

my_client.close_connection()

from polygon import RESTClient

if __name__ == "__main__":
main()
client = RESTClient() # Uses POLYGON_API_KEY env var. Can optionally supply your key.
aggs = client.get_aggs("AAPL", 1, "day", "2005-04-01", "2005-04-04")
```

## Simple REST Demo
### Getting trades
```python
from polygon import RESTClient
from polygon.rest.models import Sort

client = RESTClient() # Uses POLYGON_API_KEY env var. Can optionally supply your key.

def main():
key = "your api key"

# RESTClient can be used as a context manager to facilitate closing the underlying http session
# https://requests.readthedocs.io/en/master/user/advanced/#session-objects
with RESTClient(key) as client:
resp = client.stocks_equities_daily_open_close("AAPL", "2021-06-11")
print(f"On: {resp.from_} Apple opened at {resp.open} and closed at {resp.close}")


if __name__ == '__main__':
main()

trades = []
for t in client.list_trades("AAA", timestamp="2022-04-20", limit=5, sort=Sort.ASC):
trades.append(t)
```

### Query parameters for REST calls

Every function call under our RESTClient has the `query_params` kwargs. These kwargs are passed along and mapped 1:1
as query parameters to the underling HTTP call. For more information on the different query parameters please reference
our [API Docs](https://polygon.io/docs/).

#### Example with query parameters
### Getting raw response
To handle the raw [urllib3 response](https://urllib3.readthedocs.io/en/stable/reference/urllib3.response.html?highlight=response#response) yourself, pass `raw=True`:

```python
import datetime

from polygon import RESTClient


def ts_to_datetime(ts) -> str:
return datetime.datetime.fromtimestamp(ts / 1000.0).strftime('%Y-%m-%d %H:%M')


def main():
key = "your api key"

# RESTClient can be used as a context manager to facilitate closing the underlying http session
# https://requests.readthedocs.io/en/master/user/advanced/#session-objects
with RESTClient(key) as client:
from_ = "2021-01-01"
to = "2021-02-01"
resp = client.stocks_equities_aggregates("AAPL", 1, "minute", from_, to, unadjusted=False)

print(f"Minute aggregates for {resp.ticker} between {from_} and {to}.")

for result in resp.results:
dt = ts_to_datetime(result["t"])
print(f"{dt}\n\tO: {result['o']}\n\tH: {result['h']}\n\tL: {result['l']}\n\tC: {result['c']} ")


if __name__ == '__main__':
main()
```

## Notes about the REST Client

We use swagger as our API spec and we used this swagger to generate most of the code that defines the REST client.
We made this decision due to the size of our API, many endpoints and object definitions, and to accommodate future changes.

client = RESTClient() # Uses POLYGON_API_KEY env var. Can optionally supply your key.
response = client.get_aggs("AAPL", 1, "day", "2005-04-01", "2005-04-04", raw=True)
```
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = source
BUILDDIR = build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sphinx-autodoc-typehints~=1.18.1
25 changes: 25 additions & 0 deletions docs/source/Aggs.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.. _aggs_header:

Aggs
==========

===========
Get aggs
===========
.. automethod:: polygon.RESTClient.get_aggs

============================
Get grouped daily aggs
============================
.. automethod:: polygon.RESTClient.get_grouped_daily_aggs

============================
Get daily open close agg
============================
.. automethod:: polygon.RESTClient.get_daily_open_close_agg

============================
Get previous close agg
============================
.. automethod:: polygon.RESTClient.get_previous_close_agg

Loading