-
Notifications
You must be signed in to change notification settings - Fork 343
Add crypto, forex, and indices examples #415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
85c2a5d
Add crypto, forex, and indices examples
justinpolygon ccb83b4
fix linting
justinpolygon f690971
Update crypto-snapshots_all_tickers.py
justinpolygon 5aaf501
Update crypto-snapshots_all_tickers.py
justinpolygon 68b2fbd
Merge branch 'master' into jw-add-cfi
justinpolygon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 "<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( | ||
| "X:BTCUSD", | ||
| 1, | ||
| "day", | ||
| "2023-01-30", | ||
| "2023-02-03", | ||
| ) | ||
|
|
||
| print(aggs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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/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 | ||
| ) | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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/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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| ) | ||
justinpolygon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| print( | ||
| "{:<15}{:<15}{:<15}{:.2f} %".format( | ||
| item.ticker, | ||
| item.prev_day.open, | ||
| item.prev_day.close, | ||
| percent_change, | ||
| ) | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 "<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( | ||
| "C:EURUSD", | ||
| 1, | ||
| "day", | ||
| "2023-01-30", | ||
| "2023-02-03", | ||
| ) | ||
|
|
||
| print(aggs) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.