Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 27 additions & 0 deletions examples/rest/options-aggregates_bars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from polygon import RESTClient

# docs
# https://polygon.io/docs/options/get_v2_aggs_ticker__optionsticker__range__multiplier___timespan___from___to
# https://polygon-api-client.readthedocs.io/en/latest/Aggs.html#polygon.RESTClient.get_aggs

# API key injected below for easy use. If not provided, the script will attempt
# to use the environment variable "POLYGON_API_KEY".
#
# setx POLYGON_API_KEY "<your_api_key>" <- windows
# export POLYGON_API_KEY="<your_api_key>" <- mac/linux
#
# Note: To persist the environment variable you need to add the above command
# to the shell startup script (e.g. .bashrc or .bash_profile.
#
# client = RESTClient("XXXXXX") # hardcoded api_key is used
client = RESTClient() # POLYGON_API_KEY environment variable is used

aggs = client.get_aggs(
"O:SPY251219C00650000",
1,
"day",
"2023-01-30",
"2023-02-03",
)

print(aggs)
13 changes: 13 additions & 0 deletions examples/rest/options-conditions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from polygon import RESTClient

# docs
# https://polygon.io/docs/options/get_v3_reference_conditions
# https://polygon-api-client.readthedocs.io/en/latest/Reference.html#list-conditions

# client = RESTClient("XXXXXX") # hardcoded api_key is used
client = RESTClient() # POLYGON_API_KEY environment variable is used

conditions = []
for c in client.list_conditions("options", limit=1000):
conditions.append(c)
print(conditions)
13 changes: 13 additions & 0 deletions examples/rest/options-contract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from polygon import RESTClient

# docs
# https://polygon.io/docs/options/get_v3_reference_options_contracts__options_ticker
# https://polygon-api-client.readthedocs.io/en/latest/Contracts.html

# client = RESTClient("XXXXXX") # hardcoded api_key is used
client = RESTClient() # POLYGON_API_KEY environment variable is used

contract = client.get_options_contract("O:EVRI240119C00002500")

# print raw values
print(contract)
13 changes: 13 additions & 0 deletions examples/rest/options-contracts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from polygon import RESTClient

# docs
# https://polygon.io/docs/options/get_v3_reference_options_contracts
# https://polygon-api-client.readthedocs.io/en/latest/Contracts.html#list-options-contracts

# client = RESTClient("XXXXXX") # hardcoded api_key is used
client = RESTClient() # POLYGON_API_KEY environment variable is used

contracts = []
for c in client.list_options_contracts("HCP"):
contracts.append(c)
print(contracts)
16 changes: 16 additions & 0 deletions examples/rest/options-daily_open_close.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from polygon import RESTClient

# docs
# https://polygon.io/docs/options/get_v1_open-close__optionsticker___date
# https://polygon-api-client.readthedocs.io/en/latest/Aggs.html#get-daily-open-close-agg

# client = RESTClient("XXXXXX") # hardcoded api_key is used
client = RESTClient() # POLYGON_API_KEY environment variable is used

# make request
request = client.get_daily_open_close_agg(
"O:SPY251219C00650000",
"2023-02-22",
)

print(request)
27 changes: 27 additions & 0 deletions examples/rest/options-exchanges.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from polygon import RESTClient
from polygon.rest.models import (
Exchange,
)

# docs
# https://polygon.io/docs/options/get_v3_reference_exchanges
# https://polygon-api-client.readthedocs.io/en/latest/Reference.html#get-exchanges

# client = RESTClient("XXXXXX") # hardcoded api_key is used
client = RESTClient() # POLYGON_API_KEY environment variable is used

exchanges = client.get_exchanges("options")
print(exchanges)

# loop over exchanges
for exchange in exchanges:

# verify this is an exchange
if isinstance(exchange, Exchange):

# print exchange info
print(
"{:<15}{} ({})".format(
exchange.asset_class, exchange.name, exchange.operating_mic
)
)
14 changes: 14 additions & 0 deletions examples/rest/options-last_trade.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from polygon import RESTClient

# docs
# https://polygon.io/docs/options/get_v2_last_trade__optionsticker
# https://polygon-api-client.readthedocs.io/en/latest/Trades.html#get-last-trade

# client = RESTClient("XXXXXX") # hardcoded api_key is used
client = RESTClient() # POLYGON_API_KEY environment variable is used

trade = client.get_last_trade(
"O:TSLA210903C00700000",
)

print(trade)
22 changes: 22 additions & 0 deletions examples/rest/options-market_holidays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from polygon import RESTClient
from polygon.rest.models import (
MarketHoliday,
)

# docs
# https://polygon.io/docs/options/get_v1_marketstatus_upcoming
# https://polygon-api-client.readthedocs.io/en/latest/Reference.html#get-market-holidays

# client = RESTClient("XXXXXX") # hardcoded api_key is used
client = RESTClient() # POLYGON_API_KEY environment variable is used

holidays = client.get_market_holidays()
# print(holidays)

# print date, name, and exchange
for holiday in holidays:

# verify this is an exchange
if isinstance(holiday, MarketHoliday):

print("{:<15}{:<15} ({})".format(holiday.date, holiday.name, holiday.exchange))
11 changes: 11 additions & 0 deletions examples/rest/options-market_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from polygon import RESTClient

# docs
# https://polygon.io/docs/options/get_v1_marketstatus_now
# https://polygon-api-client.readthedocs.io/en/latest/Reference.html#get-market-status

# client = RESTClient("XXXXXX") # hardcoded api_key is used
client = RESTClient() # POLYGON_API_KEY environment variable is used

result = client.get_market_status()
print(result)
14 changes: 14 additions & 0 deletions examples/rest/options-previous_close.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from polygon import RESTClient

# docs
# https://polygon.io/docs/options/get_v2_aggs_ticker__optionsticker__prev
# https://polygon-api-client.readthedocs.io/en/latest/Aggs.html#get-previous-close-agg

# client = RESTClient("XXXXXX") # hardcoded api_key is used
client = RESTClient() # POLYGON_API_KEY environment variable is used

aggs = client.get_previous_close_agg(
"O:SPY251219C00650000",
)

print(aggs)
13 changes: 13 additions & 0 deletions examples/rest/options-quotes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from polygon import RESTClient

# docs
# https://polygon.io/docs/options/get_v3_quotes__optionsticker
# https://polygon-api-client.readthedocs.io/en/latest/Quotes.html#list-quotes

# client = RESTClient("XXXXXX") # hardcoded api_key is used
client = RESTClient() # POLYGON_API_KEY environment variable is used

quotes = []
for t in client.list_quotes("O:SPY241220P00720000", limit=50000):
quotes.append(t)
print(quotes)
13 changes: 13 additions & 0 deletions examples/rest/options-snapshots_option_contract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from polygon import RESTClient

# docs
# https://polygon.io/docs/options/get_v3_snapshot_options__underlyingasset___optioncontract
# https://polygon-api-client.readthedocs.io/en/latest/Snapshot.html

# client = RESTClient("XXXXXX") # hardcoded api_key is used
client = RESTClient() # POLYGON_API_KEY environment variable is used

snapshot = client.get_snapshot_option("AAPL", "O:AAPL230616C00150000")

# print raw values
print(snapshot)
13 changes: 13 additions & 0 deletions examples/rest/options-snapshots_options_chain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from polygon import RESTClient

# docs
# https://polygon.io/docs/options/get_v3_snapshot_options__underlyingasset
# ttps://polygon-api-client.readthedocs.io/en/latest/Snapshot.html#get-all-snapshots

# client = RESTClient("XXXXXX") # hardcoded api_key is used
client = RESTClient() # POLYGON_API_KEY environment variable is used

options_chain = []
for o in client.list_snapshot_options_chain("HCP"):
options_chain.append(o)
print(options_chain)
14 changes: 14 additions & 0 deletions examples/rest/options-technical_indicators_ema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from polygon import RESTClient

# docs
# https://polygon.io/docs/options/get_v1_indicators_ema__optionsticker
# https://github.com/polygon-io/client-python/blob/master/polygon/rest/indicators.py

# client = RESTClient("XXXXXX") # hardcoded api_key is used
client = RESTClient() # POLYGON_API_KEY environment variable is used

ema = client.get_ema(
ticker="O:SPY241220P00720000", timespan="day", window=50, series_type="close"
)

print(ema)
19 changes: 19 additions & 0 deletions examples/rest/options-technical_indicators_macd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from polygon import RESTClient

# docs
# https://polygon.io/docs/options/get_v1_indicators_macd__optionsticker
# https://github.com/polygon-io/client-python/blob/master/polygon/rest/indicators.py

# client = RESTClient("XXXXXX") # hardcoded api_key is used
client = RESTClient() # POLYGON_API_KEY environment variable is used

macd = client.get_macd(
ticker="O:SPY241220P00720000",
timespan="day",
short_window=12,
long_window=26,
signal_window=9,
series_type="close",
)

print(macd)
14 changes: 14 additions & 0 deletions examples/rest/options-technical_indicators_rsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from polygon import RESTClient

# docs
# https://polygon.io/docs/options/get_v1_indicators_rsi__optionsticker
# https://github.com/polygon-io/client-python/blob/master/polygon/rest/indicators.py

# client = RESTClient("XXXXXX") # hardcoded api_key is used
client = RESTClient() # POLYGON_API_KEY environment variable is used

rsi = client.get_rsi(
ticker="O:SPY241220P00720000", timespan="day", window=14, series_type="close"
)

print(rsi)
14 changes: 14 additions & 0 deletions examples/rest/options-technical_indicators_sma.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from polygon import RESTClient

# docs
# https://polygon.io/docs/options/get_v1_indicators_sma__optionsticker
# https://github.com/polygon-io/client-python/blob/master/polygon/rest/indicators.py

# client = RESTClient("XXXXXX") # hardcoded api_key is used
client = RESTClient() # POLYGON_API_KEY environment variable is used

sma = client.get_sma(
ticker="O:SPY241220P00720000", timespan="day", window=50, series_type="close"
)

print(sma)
11 changes: 11 additions & 0 deletions examples/rest/options-ticker_details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from polygon import RESTClient

# docs
# https://polygon.io/docs/options/get_v3_reference_tickers__ticker
# https://polygon-api-client.readthedocs.io/en/latest/Reference.html#get-ticker-details

# client = RESTClient("XXXXXX") # hardcoded api_key is used
client = RESTClient() # POLYGON_API_KEY environment variable is used

details = client.get_ticker_details("TSLA")
print(details)
26 changes: 26 additions & 0 deletions examples/rest/options-ticker_news.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from polygon import RESTClient
from polygon.rest.models import (
TickerNews,
)

# docs
# https://polygon.io/docs/options/get_v2_reference_news
# https://polygon-api-client.readthedocs.io/en/latest/Reference.html#list-ticker-news

# client = RESTClient("XXXXXX") # hardcoded api_key is used
client = RESTClient() # POLYGON_API_KEY environment variable is used

news = []
for n in client.list_ticker_news("AAPL", order="desc", limit=1000):
news.append(n)

# print date + title
for index, item in enumerate(news):

# verify this is an agg
if isinstance(item, TickerNews):

print("{:<25}{:<15}".format(item.published_utc, item.title))

if index == 20:
break
13 changes: 13 additions & 0 deletions examples/rest/options-tickers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from polygon import RESTClient

# docs
# https://polygon.io/docs/options/get_v3_reference_tickers
# https://polygon-api-client.readthedocs.io/en/latest/Reference.html#list-tickers

# client = RESTClient("XXXXXX") # hardcoded api_key is used
client = RESTClient() # POLYGON_API_KEY environment variable is used

tickers = []
for t in client.list_tickers(limit=1000):
tickers.append(t)
print(tickers)
15 changes: 15 additions & 0 deletions examples/rest/options-trades.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from polygon import RESTClient

# docs
# https://polygon.io/docs/options/get_v3_trades__optionsticker
# https://polygon-api-client.readthedocs.io/en/latest/Trades.html#polygon.RESTClient.list_trades

# client = RESTClient("XXXXXX") # hardcoded api_key is used
client = RESTClient() # POLYGON_API_KEY environment variable is used

trades = []
for t in client.list_trades("O:TSLA210903C00700000", limit=50000):
trades.append(t)

# prints each trade that took place
print(trades)
32 changes: 32 additions & 0 deletions examples/websocket/options-ws.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from polygon import WebSocketClient
from polygon.websocket.models import WebSocketMessage, Market
from typing import List

# docs
# https://polygon.io/docs/options/ws_getting-started
# https://polygon-api-client.readthedocs.io/en/latest/WebSocket.html

# client = WebSocketClient("XXXXXX") # hardcoded api_key is used
client = WebSocketClient(
market=Market.Options
) # POLYGON_API_KEY environment variable is used

# aggregates
# client.subscribe("AM.*") # aggregates (per minute)
client.subscribe("A.*") # aggregates (per second)

# trades
# client.subscribe("T.*") # all trades
# client.subscribe("T.O:SPY241220P00720000", "T.O:SPY251219C00650000") # limited trades

# quotes (1,000 option contracts per connection)
# client.subscribe("Q.O:SPY241220P00720000", "Q.O:SPY251219C00650000") # limited quotes


def handle_msg(msgs: List[WebSocketMessage]):
for m in msgs:
print(m)


# print messages
client.run(handle_msg)