Skip to content

Commit 4d8ab1c

Browse files
Add stocks demo scripts (#377)
* added demo scripts
1 parent e1ae814 commit 4d8ab1c

31 files changed

+700
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from polygon import RESTClient
2+
3+
# docs
4+
# https://polygon.io/docs/stocks/get_v2_aggs_ticker__stocksticker__range__multiplier___timespan___from___to
5+
# https://polygon-api-client.readthedocs.io/en/latest/Aggs.html#polygon.RESTClient.get_aggs
6+
7+
# API key injected below for easy use. If not provided, the script will attempt
8+
# to use the environment variable "POLYGON_API_KEY".
9+
#
10+
# setx POLYGON_API_KEY "<your_api_key>" <- windows
11+
# export POLYGON_API_KEY="<your_api_key>" <- mac/linux
12+
#
13+
# Note: To persist the environment variable you need to add the above command
14+
# to the shell startup script (e.g. .bashrc or .bash_profile.
15+
#
16+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
17+
client = RESTClient() # POLYGON_API_KEY environment variable is used
18+
19+
aggs = client.get_aggs(
20+
"AAPL",
21+
1,
22+
"day",
23+
"2023-01-30",
24+
"2023-02-03",
25+
)
26+
27+
print(aggs)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# This code retrieves stock market data for a specific stock using the
2+
# Polygon REST API and writes it to a CSV file. It uses the "polygon"
3+
# library to communicate with the API and the "csv" library to write
4+
# the data to a CSV file. The script retrieves data for the stock "AAPL"
5+
# for the dates "2023-01-30" to "2023-02-03" in 1 hour intervals. The
6+
# resulting data includes the open, high, low, close, volume, vwap,
7+
# timestamp, transactions, and otc values for each hour. The output is
8+
# then printed to the console.
9+
from polygon import RESTClient
10+
from polygon.rest.models import (
11+
Agg,
12+
)
13+
import csv
14+
import datetime
15+
import io
16+
17+
# docs
18+
# https://polygon.io/docs/stocks/get_v2_aggs_ticker__stocksticker__range__multiplier___timespan___from___to
19+
# https://polygon-api-client.readthedocs.io/en/latest/Aggs.html#polygon.RESTClient.get_aggs
20+
21+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
22+
client = RESTClient() # POLYGON_API_KEY environment variable is used
23+
24+
aggs = client.get_aggs(
25+
"AAPL",
26+
1,
27+
"hour",
28+
"2023-01-30",
29+
"2023-02-03",
30+
)
31+
32+
print(aggs)
33+
34+
# headers
35+
headers = [
36+
"timestamp",
37+
"open",
38+
"high",
39+
"low",
40+
"close",
41+
"volume",
42+
"vwap",
43+
"transactions",
44+
"otc",
45+
]
46+
47+
# creating the csv string
48+
csv_string = io.StringIO()
49+
writer = csv.DictWriter(csv_string, fieldnames=headers)
50+
51+
# writing headers
52+
writer.writeheader()
53+
54+
# writing data
55+
for agg in aggs:
56+
57+
# verify this is an agg
58+
if isinstance(agg, Agg):
59+
60+
# verify this is an int
61+
if isinstance(agg.timestamp, int):
62+
63+
writer.writerow(
64+
{
65+
"timestamp": datetime.datetime.fromtimestamp(agg.timestamp / 1000),
66+
"open": agg.open,
67+
"high": agg.high,
68+
"low": agg.low,
69+
"close": agg.close,
70+
"volume": agg.volume,
71+
"vwap": agg.vwap,
72+
"transactions": agg.transactions,
73+
"otc": agg.otc,
74+
}
75+
)
76+
77+
# printing the csv string
78+
print(csv_string.getvalue())

examples/rest/stocks-conditions.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from polygon import RESTClient
2+
3+
# docs
4+
# https://polygon.io/docs/stocks/get_v3_reference_conditions
5+
# https://polygon-api-client.readthedocs.io/en/latest/Reference.html#list-conditions
6+
7+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
8+
client = RESTClient() # POLYGON_API_KEY environment variable is used
9+
10+
conditions = []
11+
for c in client.list_conditions(limit=1000):
12+
conditions.append(c)
13+
print(conditions)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from polygon import RESTClient
2+
3+
# docs
4+
# https://polygon.io/docs/stocks/get_v1_open-close__stocksticker___date
5+
# https://polygon-api-client.readthedocs.io/en/latest/Aggs.html#get-daily-open-close-agg
6+
7+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
8+
client = RESTClient() # POLYGON_API_KEY environment variable is used
9+
10+
# make request
11+
request = client.get_daily_open_close_agg(
12+
"AAPL",
13+
"2023-02-07",
14+
)
15+
16+
print(request)

examples/rest/stocks-dividends.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from polygon import RESTClient
2+
3+
# docs
4+
# https://polygon.io/docs/stocks/get_v3_reference_dividends
5+
# https://polygon-api-client.readthedocs.io/en/latest/Reference.html#list-dividends
6+
7+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
8+
client = RESTClient() # POLYGON_API_KEY environment variable is used
9+
10+
dividends = []
11+
for d in client.list_dividends("MSFT", limit=1000):
12+
dividends.append(d)
13+
print(dividends)

examples/rest/stocks-exchanges.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from polygon import RESTClient
2+
from polygon.rest.models import (
3+
Exchange,
4+
)
5+
6+
# docs
7+
# https://polygon.io/docs/stocks/get_v3_reference_exchanges
8+
# https://polygon-api-client.readthedocs.io/en/latest/Reference.html#get-exchanges
9+
10+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
11+
client = RESTClient() # POLYGON_API_KEY environment variable is used
12+
13+
exchanges = client.get_exchanges()
14+
print(exchanges)
15+
16+
# loop over exchanges
17+
for exchange in exchanges:
18+
19+
# verify this is an exchange
20+
if isinstance(exchange, Exchange):
21+
22+
# print exchange info
23+
print(
24+
"{:<15}{} ({})".format(
25+
exchange.asset_class, exchange.name, exchange.operating_mic
26+
)
27+
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from polygon import RESTClient
2+
import pprint
3+
4+
# docs
5+
# https://polygon.io/docs/stocks/get_v2_aggs_grouped_locale_us_market_stocks__date
6+
# https://polygon-api-client.readthedocs.io/en/latest/Aggs.html#get-grouped-daily-aggs
7+
8+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
9+
client = RESTClient() # POLYGON_API_KEY environment variable is used
10+
11+
grouped = client.get_grouped_daily_aggs(
12+
"2023-02-07",
13+
)
14+
15+
# print(grouped)
16+
17+
# pprint (short for "pretty-print") is a module that provides a more human-
18+
# readable output format for data structures.
19+
pp = pprint.PrettyPrinter(indent=2)
20+
pp.pprint(grouped)

examples/rest/stocks-last_quote.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from polygon import RESTClient
2+
3+
# docs
4+
# https://polygon.io/docs/stocks/get_v2_last_nbbo__stocksticker
5+
# https://polygon-api-client.readthedocs.io/en/latest/Quotes.html#get-last-quote
6+
7+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
8+
client = RESTClient() # POLYGON_API_KEY environment variable is used
9+
10+
quote = client.get_last_quote(
11+
"AAPL",
12+
)
13+
14+
print(quote)

examples/rest/stocks-last_trade.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from polygon import RESTClient
2+
3+
# docs
4+
# https://polygon.io/docs/stocks/get_v2_last_trade__stocksticker
5+
# https://polygon-api-client.readthedocs.io/en/latest/Trades.html#get-last-trade
6+
7+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
8+
client = RESTClient() # POLYGON_API_KEY environment variable is used
9+
10+
trade = client.get_last_trade(
11+
"AAPL",
12+
)
13+
14+
print(trade)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from polygon import RESTClient
2+
from polygon.rest.models import (
3+
MarketHoliday,
4+
)
5+
6+
# docs
7+
# https://polygon.io/docs/stocks/get_v1_marketstatus_upcoming
8+
# https://polygon-api-client.readthedocs.io/en/latest/Reference.html#get-market-holidays
9+
10+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
11+
client = RESTClient() # POLYGON_API_KEY environment variable is used
12+
13+
holidays = client.get_market_holidays()
14+
# print(holidays)
15+
16+
# print date, name, and exchange
17+
for holiday in holidays:
18+
19+
# verify this is an exchange
20+
if isinstance(holiday, MarketHoliday):
21+
22+
print("{:<15}{:<15} ({})".format(holiday.date, holiday.name, holiday.exchange))

0 commit comments

Comments
 (0)