diff --git a/examples/rest/crypto-aggregates_bars.py b/examples/rest/crypto-aggregates_bars.py new file mode 100644 index 00000000..b75ea762 --- /dev/null +++ b/examples/rest/crypto-aggregates_bars.py @@ -0,0 +1,27 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/crypto/get_v2_aggs_ticker__cryptoticker__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( + "X:BTCUSD", + 1, + "day", + "2023-01-30", + "2023-02-03", +) + +print(aggs) diff --git a/examples/rest/crypto-conditions.py b/examples/rest/crypto-conditions.py new file mode 100644 index 00000000..93e3feda --- /dev/null +++ b/examples/rest/crypto-conditions.py @@ -0,0 +1,13 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/crypto/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("crypto", limit=1000): + conditions.append(c) +print(conditions) diff --git a/examples/rest/crypto-daily_open_close.py b/examples/rest/crypto-daily_open_close.py new file mode 100644 index 00000000..8ff3ad41 --- /dev/null +++ b/examples/rest/crypto-daily_open_close.py @@ -0,0 +1,16 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/crypto/get_v1_open-close_crypto__from___to___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( + "X:BTCUSD", + "2023-01-09", +) + +print(request) diff --git a/examples/rest/crypto-exchanges.py b/examples/rest/crypto-exchanges.py new file mode 100644 index 00000000..c5c2cf79 --- /dev/null +++ b/examples/rest/crypto-exchanges.py @@ -0,0 +1,27 @@ +from polygon import RESTClient +from polygon.rest.models import ( + Exchange, +) + +# docs +# https://polygon.io/docs/crypto/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("crypto") +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/crypto-grouped_daily_bars.py b/examples/rest/crypto-grouped_daily_bars.py new file mode 100644 index 00000000..ec1bdb81 --- /dev/null +++ b/examples/rest/crypto-grouped_daily_bars.py @@ -0,0 +1,20 @@ +from polygon import RESTClient +import pprint + +# docs +# https://polygon.io/docs/crypto/get_v2_aggs_grouped_locale_global_market_crypto__date +# https://polygon-api-client.readthedocs.io/en/latest/Aggs.html#get-grouped-daily-aggs + +# client = RESTClient("XXXXXX") # hardcoded api_key is used +client = RESTClient() # POLYGON_API_KEY environment variable is used + +grouped = client.get_grouped_daily_aggs( + "2023-01-09", locale="global", market_type="crypto" +) + +# print(grouped) + +# pprint (short for "pretty-print") is a module that provides a more human- +# readable output format for data structures. +pp = pprint.PrettyPrinter(indent=2) +pp.pprint(grouped) diff --git a/examples/rest/crypto-last_trade_for_a_crypto_pair.py b/examples/rest/crypto-last_trade_for_a_crypto_pair.py new file mode 100644 index 00000000..850bd98a --- /dev/null +++ b/examples/rest/crypto-last_trade_for_a_crypto_pair.py @@ -0,0 +1,12 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/crypto/get_v1_last_crypto__from___to +# https://polygon-api-client.readthedocs.io/en/latest/Trades.html#get-last-crypto-trade + +# client = RESTClient("XXXXXX") # hardcoded api_key is used +client = RESTClient() # POLYGON_API_KEY environment variable is used + +trade = client.get_last_crypto_trade("BTC", "USD") + +print(trade) diff --git a/examples/rest/crypto-market_holidays.py b/examples/rest/crypto-market_holidays.py new file mode 100644 index 00000000..13113917 --- /dev/null +++ b/examples/rest/crypto-market_holidays.py @@ -0,0 +1,22 @@ +from polygon import RESTClient +from polygon.rest.models import ( + MarketHoliday, +) + +# docs +# https://polygon.io/docs/crypto/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/crypto-market_status.py b/examples/rest/crypto-market_status.py new file mode 100644 index 00000000..4265997c --- /dev/null +++ b/examples/rest/crypto-market_status.py @@ -0,0 +1,11 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/crypto/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/crypto-previous_close.py b/examples/rest/crypto-previous_close.py new file mode 100644 index 00000000..bf309fed --- /dev/null +++ b/examples/rest/crypto-previous_close.py @@ -0,0 +1,14 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/crypto/get_v2_aggs_ticker__cryptoticker__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( + "X:BTCUSD", +) + +print(aggs) diff --git a/examples/rest/crypto-snapshots_all_tickers.py b/examples/rest/crypto-snapshots_all_tickers.py new file mode 100644 index 00000000..91441aae --- /dev/null +++ b/examples/rest/crypto-snapshots_all_tickers.py @@ -0,0 +1,45 @@ +from polygon import RESTClient +from polygon.rest.models import ( + TickerSnapshot, + Agg, +) + +# docs +# https://polygon.io/docs/crypto/get_v2_snapshot_locale_global_markets_crypto_tickers +# https://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 + +snapshot = client.get_snapshot_all("crypto") # all tickers + +# print raw values +print(snapshot) + +# crunch some numbers +for item in snapshot: + + # verify this is an TickerSnapshot + if isinstance(item, TickerSnapshot): + + # verify this is an Agg + if isinstance(item.prev_day, Agg): + + # verify this is a float + if isinstance(item.prev_day.open, float) and isinstance( + item.prev_day.close, float + ): + + percent_change = ( + (item.prev_day.close - item.prev_day.open) + / item.prev_day.open + * 100 + ) + print( + "{:<15}{:<15}{:<15}{:.2f} %".format( + item.ticker, + item.prev_day.open, + item.prev_day.close, + percent_change, + ) + ) diff --git a/examples/rest/crypto-snapshots_gainers_losers.py b/examples/rest/crypto-snapshots_gainers_losers.py new file mode 100644 index 00000000..61a51fa1 --- /dev/null +++ b/examples/rest/crypto-snapshots_gainers_losers.py @@ -0,0 +1,43 @@ +from polygon import RESTClient +from polygon.rest.models import ( + TickerSnapshot, +) + +# docs +# https://polygon.io/docs/crypto/get_v2_snapshot_locale_global_markets_crypto__direction +# https://polygon-api-client.readthedocs.io/en/latest/Snapshot.html#get-gainers-losers-snapshot + +# client = RESTClient("XXXXXX") # hardcoded api_key is used +client = RESTClient() # POLYGON_API_KEY environment variable is used + +# get gainers +gainers = client.get_snapshot_direction("crypto", "gainers") +# print(gainers) + +# print ticker with % change +for gainer in gainers: + + # verify this is a TickerSnapshot + if isinstance(gainer, TickerSnapshot): + + # verify this is a float + if isinstance(gainer.todays_change_percent, float): + + print("{:<15}{:.2f} %".format(gainer.ticker, gainer.todays_change_percent)) + +print() + +# get losers +losers = client.get_snapshot_direction("crypto", "losers") +# print(losers) + +# print ticker with % change +for loser in losers: + + # verify this is a TickerSnapshot + if isinstance(loser, TickerSnapshot): + + # verify this is a float + if isinstance(loser.todays_change_percent, float): + + print("{:<15}{:.2f} %".format(loser.ticker, loser.todays_change_percent)) diff --git a/examples/rest/crypto-snapshots_ticker.py b/examples/rest/crypto-snapshots_ticker.py new file mode 100644 index 00000000..31a48ebd --- /dev/null +++ b/examples/rest/crypto-snapshots_ticker.py @@ -0,0 +1,11 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/crypto/get_v2_snapshot_locale_global_markets_crypto_tickers__ticker +# https://polygon-api-client.readthedocs.io/en/latest/Snapshot.html#get-ticker-snapshot + +# client = RESTClient("XXXXXX") # hardcoded api_key is used +client = RESTClient() # POLYGON_API_KEY environment variable is used + +ticker = client.get_snapshot_ticker("crypto", "X:BTCUSD") +print(ticker) diff --git a/examples/rest/crypto-snapshots_ticker_full_book_l2.py b/examples/rest/crypto-snapshots_ticker_full_book_l2.py new file mode 100644 index 00000000..38d239cf --- /dev/null +++ b/examples/rest/crypto-snapshots_ticker_full_book_l2.py @@ -0,0 +1,13 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/crypto/get_v2_snapshot_locale_global_markets_crypto_tickers__ticker__book +# https://polygon-api-client.readthedocs.io/en/latest/Snapshot.html#get-crypto-l2-book-snapshot + +# client = RESTClient("XXXXXX") # hardcoded api_key is used +client = RESTClient() # POLYGON_API_KEY environment variable is used + +snapshot = client.get_snapshot_crypto_book("X:BTCUSD") + +# print raw values +print(snapshot) diff --git a/examples/rest/crypto-technical_indicators_ema.py b/examples/rest/crypto-technical_indicators_ema.py new file mode 100644 index 00000000..edd45dee --- /dev/null +++ b/examples/rest/crypto-technical_indicators_ema.py @@ -0,0 +1,11 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/crypto/get_v1_indicators_ema__cryptoticker +# 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("X:BTCUSD") +print(ema) diff --git a/examples/rest/crypto-technical_indicators_macd.py b/examples/rest/crypto-technical_indicators_macd.py new file mode 100644 index 00000000..d1f60337 --- /dev/null +++ b/examples/rest/crypto-technical_indicators_macd.py @@ -0,0 +1,11 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/crypto/get_v1_indicators_macd__cryptoticker +# 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("X:BTCUSD") +print(macd) diff --git a/examples/rest/crypto-technical_indicators_rsi.py b/examples/rest/crypto-technical_indicators_rsi.py new file mode 100644 index 00000000..38423590 --- /dev/null +++ b/examples/rest/crypto-technical_indicators_rsi.py @@ -0,0 +1,11 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/crypto/get_v1_indicators_rsi__cryptoticker +# 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("X:BTCUSD") +print(rsi) diff --git a/examples/rest/crypto-technical_indicators_sma.py b/examples/rest/crypto-technical_indicators_sma.py new file mode 100644 index 00000000..f343ff7b --- /dev/null +++ b/examples/rest/crypto-technical_indicators_sma.py @@ -0,0 +1,11 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/crypto/get_v1_indicators_sma__cryptoticker +# 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("X:BTCUSD") +print(sma) diff --git a/examples/rest/crypto-tickers.py b/examples/rest/crypto-tickers.py new file mode 100644 index 00000000..8eb07170 --- /dev/null +++ b/examples/rest/crypto-tickers.py @@ -0,0 +1,13 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/crypto/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(market="crypto", limit=1000): + tickers.append(t) +print(tickers) diff --git a/examples/rest/crypto-trades.py b/examples/rest/crypto-trades.py new file mode 100644 index 00000000..7cafd989 --- /dev/null +++ b/examples/rest/crypto-trades.py @@ -0,0 +1,15 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/crypto/get_v3_trades__cryptoticker +# 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("X:BTC-USD", "2023-02-01", limit=50000): + trades.append(t) + +# prints each trade that took place +print(trades) diff --git a/examples/rest/forex-aggregates_bars.py b/examples/rest/forex-aggregates_bars.py new file mode 100644 index 00000000..56f50aa6 --- /dev/null +++ b/examples/rest/forex-aggregates_bars.py @@ -0,0 +1,27 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/forex/get_v2_aggs_ticker__forexticker__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( + "C:EURUSD", + 1, + "day", + "2023-01-30", + "2023-02-03", +) + +print(aggs) diff --git a/examples/rest/forex-conditions.py b/examples/rest/forex-conditions.py new file mode 100644 index 00000000..fca1e887 --- /dev/null +++ b/examples/rest/forex-conditions.py @@ -0,0 +1,13 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/forex/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("fx", limit=1000): + conditions.append(c) +print(conditions) diff --git a/examples/rest/forex-exchanges.py b/examples/rest/forex-exchanges.py new file mode 100644 index 00000000..a573a19b --- /dev/null +++ b/examples/rest/forex-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("fx") +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/forex-grouped_daily_bars.py b/examples/rest/forex-grouped_daily_bars.py new file mode 100644 index 00000000..f8130877 --- /dev/null +++ b/examples/rest/forex-grouped_daily_bars.py @@ -0,0 +1,22 @@ +from polygon import RESTClient +import pprint + +# docs +# https://polygon.io/docs/forex/get_v2_aggs_grouped_locale_global_market_fx__date +# https://polygon-api-client.readthedocs.io/en/latest/Aggs.html#get-grouped-daily-aggs + +# client = RESTClient("XXXXXX") # hardcoded api_key is used +client = RESTClient() # POLYGON_API_KEY environment variable is used + +grouped = client.get_grouped_daily_aggs( + "2023-03-27", + locale="global", + market_type="fx", +) + +# print(grouped) + +# pprint (short for "pretty-print") is a module that provides a more human- +# readable output format for data structures. +pp = pprint.PrettyPrinter(indent=2) +pp.pprint(grouped) diff --git a/examples/rest/forex-last_quote_for_a_currency_pair.py b/examples/rest/forex-last_quote_for_a_currency_pair.py new file mode 100644 index 00000000..8b3c78b1 --- /dev/null +++ b/examples/rest/forex-last_quote_for_a_currency_pair.py @@ -0,0 +1,15 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/forex/get_v1_last_quote_currencies__from___to +# https://polygon-api-client.readthedocs.io/en/latest/Quotes.html#get-last-forex-quote + +# client = RESTClient("XXXXXX") # hardcoded api_key is used +client = RESTClient() # POLYGON_API_KEY environment variable is used + +quote = client.get_last_forex_quote( + "AUD", + "USD", +) + +print(quote) diff --git a/examples/rest/forex-market_holidays.py b/examples/rest/forex-market_holidays.py new file mode 100644 index 00000000..85489844 --- /dev/null +++ b/examples/rest/forex-market_holidays.py @@ -0,0 +1,22 @@ +from polygon import RESTClient +from polygon.rest.models import ( + MarketHoliday, +) + +# docs +# https://polygon.io/docs/forex/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/forex-market_status.py b/examples/rest/forex-market_status.py new file mode 100644 index 00000000..06f544cc --- /dev/null +++ b/examples/rest/forex-market_status.py @@ -0,0 +1,11 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/forex/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/forex-previous_close.py b/examples/rest/forex-previous_close.py new file mode 100644 index 00000000..0de11709 --- /dev/null +++ b/examples/rest/forex-previous_close.py @@ -0,0 +1,14 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/forex/get_v2_aggs_ticker__forexticker__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( + "C:EURUSD", +) + +print(aggs) diff --git a/examples/rest/forex-quotes.py b/examples/rest/forex-quotes.py new file mode 100644 index 00000000..880ebca8 --- /dev/null +++ b/examples/rest/forex-quotes.py @@ -0,0 +1,13 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/forex/get_v3_quotes__fxticker +# 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("C:EUR-USD", "2023-02-01", limit=50000): + quotes.append(t) +print(quotes) diff --git a/examples/rest/forex-real-time_currency_conversion.py b/examples/rest/forex-real-time_currency_conversion.py new file mode 100644 index 00000000..b50099c9 --- /dev/null +++ b/examples/rest/forex-real-time_currency_conversion.py @@ -0,0 +1,15 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/forex/get_v1_conversion__from___to +# https://polygon-api-client.readthedocs.io/en/latest/Quotes.html#get-real-time-currency-conversion + +# client = RESTClient("XXXXXX") # hardcoded api_key is used +client = RESTClient() # POLYGON_API_KEY environment variable is used + +rate = client.get_real_time_currency_conversion( + "AUD", + "USD", +) + +print(rate) diff --git a/examples/rest/forex-snapshots_all_tickers.py b/examples/rest/forex-snapshots_all_tickers.py new file mode 100644 index 00000000..0650bbc6 --- /dev/null +++ b/examples/rest/forex-snapshots_all_tickers.py @@ -0,0 +1,45 @@ +from polygon import RESTClient +from polygon.rest.models import ( + TickerSnapshot, + Agg, +) + +# docs +# https://polygon.io/docs/forex/get_v2_snapshot_locale_global_markets_forex_tickers +# https://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 + +snapshot = client.get_snapshot_all("forex") # all tickers + +# print raw values +print(snapshot) + +# crunch some numbers +for item in snapshot: + + # verify this is an TickerSnapshot + if isinstance(item, TickerSnapshot): + + # verify this is an Agg + if isinstance(item.prev_day, Agg): + + # verify this is a float + if isinstance(item.prev_day.open, float) and isinstance( + item.prev_day.close, float + ): + + percent_change = ( + (item.prev_day.close - item.prev_day.open) + / item.prev_day.open + * 100 + ) + print( + "{:<15}{:<15}{:<15}{:.2f} %".format( + item.ticker, + item.prev_day.open, + item.prev_day.close, + percent_change, + ) + ) diff --git a/examples/rest/forex-snapshots_gainers_losers.py b/examples/rest/forex-snapshots_gainers_losers.py new file mode 100644 index 00000000..16a59149 --- /dev/null +++ b/examples/rest/forex-snapshots_gainers_losers.py @@ -0,0 +1,43 @@ +from polygon import RESTClient +from polygon.rest.models import ( + TickerSnapshot, +) + +# docs +# https://polygon.io/docs/forex/get_v2_snapshot_locale_global_markets_forex__direction +# https://polygon-api-client.readthedocs.io/en/latest/Snapshot.html#get-gainers-losers-snapshot + +# client = RESTClient("XXXXXX") # hardcoded api_key is used +client = RESTClient() # POLYGON_API_KEY environment variable is used + +# get gainers +gainers = client.get_snapshot_direction("forex", "gainers") +# print(gainers) + +# print ticker with % change +for gainer in gainers: + + # verify this is a TickerSnapshot + if isinstance(gainer, TickerSnapshot): + + # verify this is a float + if isinstance(gainer.todays_change_percent, float): + + print("{:<15}{:.2f} %".format(gainer.ticker, gainer.todays_change_percent)) + +print() + +# get losers +losers = client.get_snapshot_direction("forex", "losers") +# print(losers) + +# print ticker with % change +for loser in losers: + + # verify this is a TickerSnapshot + if isinstance(loser, TickerSnapshot): + + # verify this is a float + if isinstance(loser.todays_change_percent, float): + + print("{:<15}{:.2f} %".format(loser.ticker, loser.todays_change_percent)) diff --git a/examples/rest/forex-snapshots_ticker.py b/examples/rest/forex-snapshots_ticker.py new file mode 100644 index 00000000..9dbfc0ff --- /dev/null +++ b/examples/rest/forex-snapshots_ticker.py @@ -0,0 +1,11 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/forex/get_v2_snapshot_locale_global_markets_forex_tickers__ticker +# https://polygon-api-client.readthedocs.io/en/latest/Snapshot.html#get-ticker-snapshot + +# client = RESTClient("XXXXXX") # hardcoded api_key is used +client = RESTClient() # POLYGON_API_KEY environment variable is used + +ticker = client.get_snapshot_ticker("forex", "C:EURUSD") +print(ticker) diff --git a/examples/rest/forex-technical_indicators_ema.py b/examples/rest/forex-technical_indicators_ema.py new file mode 100644 index 00000000..ab927dcb --- /dev/null +++ b/examples/rest/forex-technical_indicators_ema.py @@ -0,0 +1,11 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/forex/get_v1_indicators_ema__fxticker +# 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("C:EURUSD") +print(ema) diff --git a/examples/rest/forex-technical_indicators_macd.py b/examples/rest/forex-technical_indicators_macd.py new file mode 100644 index 00000000..2613f61a --- /dev/null +++ b/examples/rest/forex-technical_indicators_macd.py @@ -0,0 +1,11 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/forex/get_v1_indicators_macd__fxticker +# 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("C:EURUSD") +print(macd) diff --git a/examples/rest/forex-technical_indicators_rsi.py b/examples/rest/forex-technical_indicators_rsi.py new file mode 100644 index 00000000..0b3001ac --- /dev/null +++ b/examples/rest/forex-technical_indicators_rsi.py @@ -0,0 +1,11 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/forex/get_v1_indicators_rsi__fxticker +# 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("C:EURUSD") +print(rsi) diff --git a/examples/rest/forex-technical_indicators_sma.py b/examples/rest/forex-technical_indicators_sma.py new file mode 100644 index 00000000..22389b63 --- /dev/null +++ b/examples/rest/forex-technical_indicators_sma.py @@ -0,0 +1,11 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/forex/get_v1_indicators_sma__fxticker +# 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("C:EURUSD") +print(sma) diff --git a/examples/rest/forex-tickers.py b/examples/rest/forex-tickers.py new file mode 100644 index 00000000..c1736721 --- /dev/null +++ b/examples/rest/forex-tickers.py @@ -0,0 +1,13 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/forex/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(market="fx", limit=1000): + tickers.append(t) +print(tickers) diff --git a/examples/rest/indices-aggregates_bars.py b/examples/rest/indices-aggregates_bars.py new file mode 100644 index 00000000..b5305648 --- /dev/null +++ b/examples/rest/indices-aggregates_bars.py @@ -0,0 +1,27 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/indices/get_v2_aggs_ticker__indicesticker__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( + "I:SPX", + 1, + "day", + "2023-03-10", + "2023-03-10", +) + +print(aggs) diff --git a/examples/rest/indices-daily_open_close.py b/examples/rest/indices-daily_open_close.py new file mode 100644 index 00000000..f84a51ab --- /dev/null +++ b/examples/rest/indices-daily_open_close.py @@ -0,0 +1,16 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/indices/get_v1_open-close__indicesticker___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( + "I:SPX", + "2023-03-28", +) + +print(request) diff --git a/examples/rest/indices-market_holidays.py b/examples/rest/indices-market_holidays.py new file mode 100644 index 00000000..c1e94932 --- /dev/null +++ b/examples/rest/indices-market_holidays.py @@ -0,0 +1,22 @@ +from polygon import RESTClient +from polygon.rest.models import ( + MarketHoliday, +) + +# docs +# https://polygon.io/docs/indices/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/indices-market_status.py b/examples/rest/indices-market_status.py new file mode 100644 index 00000000..6c74dee3 --- /dev/null +++ b/examples/rest/indices-market_status.py @@ -0,0 +1,11 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/indices/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/indices-previous_close.py b/examples/rest/indices-previous_close.py new file mode 100644 index 00000000..8774bd6e --- /dev/null +++ b/examples/rest/indices-previous_close.py @@ -0,0 +1,14 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/indices/get_v2_aggs_ticker__indicesticker__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( + "I:SPX", +) + +print(aggs) diff --git a/examples/rest/indices-snapshots.py b/examples/rest/indices-snapshots.py new file mode 100644 index 00000000..ed1560cc --- /dev/null +++ b/examples/rest/indices-snapshots.py @@ -0,0 +1,13 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/indices/get_v3_snapshot_indices +# https://github.com/polygon-io/client-python/blob/master/polygon/rest/snapshot.py# + +# client = RESTClient("XXXXXX") # hardcoded api_key is used +client = RESTClient() # POLYGON_API_KEY environment variable is used + +snapshot = client.get_snapshot_indices("I:SPX") + +# print raw values +print(snapshot) diff --git a/examples/rest/indices-technical_indicators_ema.py b/examples/rest/indices-technical_indicators_ema.py new file mode 100644 index 00000000..bbf9bc39 --- /dev/null +++ b/examples/rest/indices-technical_indicators_ema.py @@ -0,0 +1,11 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/indices/get_v1_indicators_ema__indicesticker +# 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("I:SPX") +print(ema) diff --git a/examples/rest/indices-technical_indicators_macd.py b/examples/rest/indices-technical_indicators_macd.py new file mode 100644 index 00000000..751258aa --- /dev/null +++ b/examples/rest/indices-technical_indicators_macd.py @@ -0,0 +1,11 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/indices/get_v1_indicators_macd__indicesticker +# 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("I:SPX") +print(macd) diff --git a/examples/rest/indices-technical_indicators_rsi.py b/examples/rest/indices-technical_indicators_rsi.py new file mode 100644 index 00000000..93f1a16b --- /dev/null +++ b/examples/rest/indices-technical_indicators_rsi.py @@ -0,0 +1,11 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/indices/get_v1_indicators_rsi__indicesticker +# 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("I:SPX") +print(rsi) diff --git a/examples/rest/indices-technical_indicators_sma.py b/examples/rest/indices-technical_indicators_sma.py new file mode 100644 index 00000000..5343e54d --- /dev/null +++ b/examples/rest/indices-technical_indicators_sma.py @@ -0,0 +1,11 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/indices/get_v1_indicators_sma__indicesticker +# 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("I:SPX") +print(sma) diff --git a/examples/rest/indices-ticker_types.py b/examples/rest/indices-ticker_types.py new file mode 100644 index 00000000..ec3277e9 --- /dev/null +++ b/examples/rest/indices-ticker_types.py @@ -0,0 +1,27 @@ +from typing import Optional, Union, List +from urllib3 import HTTPResponse +from polygon import RESTClient +from polygon.rest.models import ( + TickerTypes, +) + +# docs +# https://polygon.io/docs/indices/get_v3_reference_tickers_types +# https://polygon-api-client.readthedocs.io/en/latest/Reference.html#get-ticker-types + +# client = RESTClient("XXXXXX") # hardcoded api_key is used +client = RESTClient() # POLYGON_API_KEY environment variable is used + +types: Optional[Union[List[TickerTypes], HTTPResponse]] = None + +try: + types = client.get_ticker_types("indices") +except TypeError as e: + if "not NoneType" in str(e): + print("None found") + types = None + else: + raise + +if types is not None: + print(types) diff --git a/examples/rest/indices-tickers.py b/examples/rest/indices-tickers.py new file mode 100644 index 00000000..a0786f19 --- /dev/null +++ b/examples/rest/indices-tickers.py @@ -0,0 +1,13 @@ +from polygon import RESTClient + +# docs +# https://polygon.io/docs/indices/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(market="indices", limit=1000): + tickers.append(t) +print(tickers)