Skip to content

Commit 0ed7def

Browse files
Added options demo scripts (#390)
1 parent c23cf2d commit 0ed7def

22 files changed

+367
-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/options/get_v2_aggs_ticker__optionsticker__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+
"O:SPY251219C00650000",
21+
1,
22+
"day",
23+
"2023-01-30",
24+
"2023-02-03",
25+
)
26+
27+
print(aggs)

examples/rest/options-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/options/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("options", limit=1000):
12+
conditions.append(c)
13+
print(conditions)

examples/rest/options-contract.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/options/get_v3_reference_options_contracts__options_ticker
5+
# https://polygon-api-client.readthedocs.io/en/latest/Contracts.html
6+
7+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
8+
client = RESTClient() # POLYGON_API_KEY environment variable is used
9+
10+
contract = client.get_options_contract("O:EVRI240119C00002500")
11+
12+
# print raw values
13+
print(contract)

examples/rest/options-contracts.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/options/get_v3_reference_options_contracts
5+
# https://polygon-api-client.readthedocs.io/en/latest/Contracts.html#list-options-contracts
6+
7+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
8+
client = RESTClient() # POLYGON_API_KEY environment variable is used
9+
10+
contracts = []
11+
for c in client.list_options_contracts("HCP"):
12+
contracts.append(c)
13+
print(contracts)
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/options/get_v1_open-close__optionsticker___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+
"O:SPY251219C00650000",
13+
"2023-02-22",
14+
)
15+
16+
print(request)

examples/rest/options-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/options/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("options")
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+
)

examples/rest/options-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/options/get_v2_last_trade__optionsticker
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+
"O:TSLA210903C00700000",
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/options/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))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from polygon import RESTClient
2+
3+
# docs
4+
# https://polygon.io/docs/options/get_v1_marketstatus_now
5+
# https://polygon-api-client.readthedocs.io/en/latest/Reference.html#get-market-status
6+
7+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
8+
client = RESTClient() # POLYGON_API_KEY environment variable is used
9+
10+
result = client.get_market_status()
11+
print(result)
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/options/get_v2_aggs_ticker__optionsticker__prev
5+
# https://polygon-api-client.readthedocs.io/en/latest/Aggs.html#get-previous-close-agg
6+
7+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
8+
client = RESTClient() # POLYGON_API_KEY environment variable is used
9+
10+
aggs = client.get_previous_close_agg(
11+
"O:SPY251219C00650000",
12+
)
13+
14+
print(aggs)

0 commit comments

Comments
 (0)