From bb2f3dc22d4ece735004a837842de3cd4c8c96d8 Mon Sep 17 00:00:00 2001 From: justinpolygon <123573436+justinpolygon@users.noreply.github.com> Date: Thu, 23 Feb 2023 14:19:17 -0800 Subject: [PATCH] Added options demo scripts --- examples/rest/options-aggregates_bars.py | 27 ++++++++++++++++ examples/rest/options-conditions.py | 13 ++++++++ examples/rest/options-contract.py | 13 ++++++++ examples/rest/options-contracts.py | 13 ++++++++ examples/rest/options-daily_open_close.py | 16 ++++++++++ examples/rest/options-exchanges.py | 27 ++++++++++++++++ examples/rest/options-last_trade.py | 14 ++++++++ examples/rest/options-market_holidays.py | 22 +++++++++++++ examples/rest/options-market_status.py | 11 +++++++ examples/rest/options-previous_close.py | 14 ++++++++ examples/rest/options-quotes.py | 13 ++++++++ .../rest/options-snapshots_option_contract.py | 13 ++++++++ .../rest/options-snapshots_options_chain.py | 13 ++++++++ .../rest/options-technical_indicators_ema.py | 14 ++++++++ .../rest/options-technical_indicators_macd.py | 19 +++++++++++ .../rest/options-technical_indicators_rsi.py | 14 ++++++++ .../rest/options-technical_indicators_sma.py | 14 ++++++++ examples/rest/options-ticker_details.py | 11 +++++++ examples/rest/options-ticker_news.py | 26 +++++++++++++++ examples/rest/options-tickers.py | 13 ++++++++ examples/rest/options-trades.py | 15 +++++++++ examples/websocket/options-ws.py | 32 +++++++++++++++++++ 22 files changed, 367 insertions(+) create mode 100644 examples/rest/options-aggregates_bars.py create mode 100644 examples/rest/options-conditions.py create mode 100644 examples/rest/options-contract.py create mode 100644 examples/rest/options-contracts.py create mode 100644 examples/rest/options-daily_open_close.py create mode 100644 examples/rest/options-exchanges.py create mode 100644 examples/rest/options-last_trade.py create mode 100644 examples/rest/options-market_holidays.py create mode 100644 examples/rest/options-market_status.py create mode 100644 examples/rest/options-previous_close.py create mode 100644 examples/rest/options-quotes.py create mode 100644 examples/rest/options-snapshots_option_contract.py create mode 100644 examples/rest/options-snapshots_options_chain.py create mode 100644 examples/rest/options-technical_indicators_ema.py create mode 100644 examples/rest/options-technical_indicators_macd.py create mode 100644 examples/rest/options-technical_indicators_rsi.py create mode 100644 examples/rest/options-technical_indicators_sma.py create mode 100644 examples/rest/options-ticker_details.py create mode 100644 examples/rest/options-ticker_news.py create mode 100644 examples/rest/options-tickers.py create mode 100644 examples/rest/options-trades.py create mode 100644 examples/websocket/options-ws.py diff --git a/examples/rest/options-aggregates_bars.py b/examples/rest/options-aggregates_bars.py new file mode 100644 index 00000000..943fae88 --- /dev/null +++ b/examples/rest/options-aggregates_bars.py @@ -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 "" <- windows +# export POLYGON_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) diff --git a/examples/rest/options-conditions.py b/examples/rest/options-conditions.py new file mode 100644 index 00000000..3d72e3e7 --- /dev/null +++ b/examples/rest/options-conditions.py @@ -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) diff --git a/examples/rest/options-contract.py b/examples/rest/options-contract.py new file mode 100644 index 00000000..f87c161e --- /dev/null +++ b/examples/rest/options-contract.py @@ -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) diff --git a/examples/rest/options-contracts.py b/examples/rest/options-contracts.py new file mode 100644 index 00000000..34d7327b --- /dev/null +++ b/examples/rest/options-contracts.py @@ -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) diff --git a/examples/rest/options-daily_open_close.py b/examples/rest/options-daily_open_close.py new file mode 100644 index 00000000..54a700ce --- /dev/null +++ b/examples/rest/options-daily_open_close.py @@ -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) diff --git a/examples/rest/options-exchanges.py b/examples/rest/options-exchanges.py new file mode 100644 index 00000000..a1affada --- /dev/null +++ b/examples/rest/options-exchanges.py @@ -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 + ) + ) diff --git a/examples/rest/options-last_trade.py b/examples/rest/options-last_trade.py new file mode 100644 index 00000000..bf3e7662 --- /dev/null +++ b/examples/rest/options-last_trade.py @@ -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) diff --git a/examples/rest/options-market_holidays.py b/examples/rest/options-market_holidays.py new file mode 100644 index 00000000..d54b8758 --- /dev/null +++ b/examples/rest/options-market_holidays.py @@ -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)) diff --git a/examples/rest/options-market_status.py b/examples/rest/options-market_status.py new file mode 100644 index 00000000..fb8e5ccd --- /dev/null +++ b/examples/rest/options-market_status.py @@ -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) diff --git a/examples/rest/options-previous_close.py b/examples/rest/options-previous_close.py new file mode 100644 index 00000000..f7b9d06b --- /dev/null +++ b/examples/rest/options-previous_close.py @@ -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) diff --git a/examples/rest/options-quotes.py b/examples/rest/options-quotes.py new file mode 100644 index 00000000..71d2577a --- /dev/null +++ b/examples/rest/options-quotes.py @@ -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) diff --git a/examples/rest/options-snapshots_option_contract.py b/examples/rest/options-snapshots_option_contract.py new file mode 100644 index 00000000..280e3315 --- /dev/null +++ b/examples/rest/options-snapshots_option_contract.py @@ -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) diff --git a/examples/rest/options-snapshots_options_chain.py b/examples/rest/options-snapshots_options_chain.py new file mode 100644 index 00000000..85a69d02 --- /dev/null +++ b/examples/rest/options-snapshots_options_chain.py @@ -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) diff --git a/examples/rest/options-technical_indicators_ema.py b/examples/rest/options-technical_indicators_ema.py new file mode 100644 index 00000000..ff04ff76 --- /dev/null +++ b/examples/rest/options-technical_indicators_ema.py @@ -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) diff --git a/examples/rest/options-technical_indicators_macd.py b/examples/rest/options-technical_indicators_macd.py new file mode 100644 index 00000000..e7961c8f --- /dev/null +++ b/examples/rest/options-technical_indicators_macd.py @@ -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) diff --git a/examples/rest/options-technical_indicators_rsi.py b/examples/rest/options-technical_indicators_rsi.py new file mode 100644 index 00000000..4bf9ebde --- /dev/null +++ b/examples/rest/options-technical_indicators_rsi.py @@ -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) diff --git a/examples/rest/options-technical_indicators_sma.py b/examples/rest/options-technical_indicators_sma.py new file mode 100644 index 00000000..ce6f3d0c --- /dev/null +++ b/examples/rest/options-technical_indicators_sma.py @@ -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) diff --git a/examples/rest/options-ticker_details.py b/examples/rest/options-ticker_details.py new file mode 100644 index 00000000..43d59156 --- /dev/null +++ b/examples/rest/options-ticker_details.py @@ -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) diff --git a/examples/rest/options-ticker_news.py b/examples/rest/options-ticker_news.py new file mode 100644 index 00000000..099ee264 --- /dev/null +++ b/examples/rest/options-ticker_news.py @@ -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 diff --git a/examples/rest/options-tickers.py b/examples/rest/options-tickers.py new file mode 100644 index 00000000..d77ef641 --- /dev/null +++ b/examples/rest/options-tickers.py @@ -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) diff --git a/examples/rest/options-trades.py b/examples/rest/options-trades.py new file mode 100644 index 00000000..210362d5 --- /dev/null +++ b/examples/rest/options-trades.py @@ -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) diff --git a/examples/websocket/options-ws.py b/examples/websocket/options-ws.py new file mode 100644 index 00000000..22fd12b1 --- /dev/null +++ b/examples/websocket/options-ws.py @@ -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)