8000 Add crypto, forex, and indices examples (#415) · polygon-io/client-python@5d8560c · GitHub
[go: up one dir, main page]

Skip to content

Commit 5d8560c

Browse files
Add crypto, forex, and indices examples (#415)
1 parent a09584a commit 5d8560c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+868
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from polygon import RESTClient
2+
3+
# docs
4+
# https://polygon.io/docs/crypto/get_v2_aggs_ticker__cryptoticker__range__multiplier___timespan___from___to
5+
# https://polygon-api-client.readthedocs.io/en/latest/Aggs.html#polygon.RESTClient.get_aggs
6+
7+
# API key injected below for easy use. If not provided, the script will attempt
8+
# to use the environment variable "POLYGON_API_KEY".
9+
#
10+
# setx POLYGON_API_KEY "<your_api_key>" <- windows
11+
# export POLYGON_API_KEY="<your_api_key>" <- mac/linux
12+
#
13+
# Note: To persist the environment variable you need to add the above command
14+
# to the shell startup script (e.g. .bashrc or .bash_profile.
15+
#
16+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
17+
client = RESTClient() # POLYGON_API_KEY environment variable is used
18+
19+
aggs = client.get_aggs(
20+
"X:BTCUSD",
21+
1,
22+
"day",
23+
"2023-01-30",
24+
"2023-02-03",
25+
)
26+
27+
print(aggs)

examples/rest/crypto-conditions.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from polygon import RESTClient
2+
3+
# docs
4+
# https://polygon.io/docs/crypto/get_v3_reference_conditions
5+
# https://polygon-api-client.readthedocs.io/en/latest/Reference.html#list-conditions
6+
7+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
8+
client = RESTClient() # POLYGON_API_KEY environment variable is used
9+
10+
conditions = []
11+
for c in client.list_conditions("crypto", limit=1000):
12+
conditions.append(c)
13+
print(conditions)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from polygon import RESTClient
2+
3+
# docs
4+
# https://polygon.io/docs/crypto/get_v1_open-close_crypto__from___to___date
5+
# https://polygon-api-client.readthedocs.io/en/latest/Aggs.html#get-daily-open-close-agg
6+
7+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
8+
client = RESTClient() # POLYGON_API_KEY environment variable is used
9+
10+
# make request
11+
request = client.get_daily_open_close_agg(
12+
"X:BTCUSD",
13+
"2023-01-09",
14+
)
15+
16+
print(request)

examples/rest/crypto-exchanges.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from polygon import RESTClient
2+
from polygon.rest.models import (
3+
Exchange,
4+
)
5+
6+
# docs
7+
# https://polygon.io/docs/crypto/get_v3_reference_exchanges
8+
# https://polygon-api-client.readthedocs.io/en/latest/Reference.html#get-exchanges
9+
10+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
11+
client = RESTClient() # POLYGON_API_KEY environment variable is used
12+
13+
exchanges = client.get_exchanges("crypto")
14+
print(exchanges)
15+
16+
# loop over exchanges
17+
for exchange in exchanges:
18+
19+
# verify this is an exchange
20+
if isinstance(exchange, Exchange):
21+
22+
# print exchange info
23+
print(
24+
"{:<15}{} ({})".format(
25+
exchange.asset_class, exchange.name, exchange.operating_mic
26+
)
27+
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from polygon import RESTClient
2+
import pprint
3+
4+
# docs
5+
# https://polygon.io/docs/crypto/get_v2_aggs_grouped_locale_global_market_crypto__date
6+
# https://polygon-api-client.readthedocs.io/en/latest/Aggs.html#get-grouped-daily-aggs
7+
8+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
9+
client = RESTClient() # POLYGON_API_KEY environment variable is used
10+
11+
grouped = client.get_grouped_daily_aggs(
12+
"2023-01-09", locale="global", market_type="crypto"
13+
)
14+
15+
# print(grouped)
16+
17+
# pprint (short for "pretty-print") is a module that provides a more human-
18+
# readable output format for data structures.
19+
pp = pprint.PrettyPrinter(indent=2)
20+
pp.pprint(grouped)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from polygon import RESTClient
2+
3+
# docs
4+
# https://polygon.io/docs/crypto/get_v1_last_crypto__from___to
5+
# https://polygon-api-client.readthedocs.io/en/latest/Trades.html#get-last-crypto-trade
6+
7+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
8+
client = RESTClient() # POLYGON_API_KEY environment variable is used
9+
10+
trade = client.get_last_crypto_trade("BTC", "USD")
11+
12+
print(trade)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from polygon import RESTClient
2+
from polygon.rest.models import (
3+
MarketHoliday,
4+
)
5+
6+
# doc 10000 s
7+
# https://polygon.io/docs/crypto/get_v1_marketstatus_upcoming
8+
# https://polygon-api-client.readthedocs.io/en/latest/Reference.html#get-market-holidays
9+
10+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
11+
client = RESTClient() # POLYGON_API_KEY environment variable is used
12+
13+
holidays = client.get_market_holidays()
14+
# print(holidays)
15+
16+
# print date, name, and exchange
17+
for holiday in holidays:
18+
19+
# verify this is an exchange
20+
if isinstance(holiday, MarketHoliday):
21+
22+
print("{:<15}{:<15} ({})".format(holiday.date, holiday.name, holiday.exchange))

examples/rest/crypto-market_status.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from polygon import RESTClient
2+
3+
# docs
4+
# https://polygon.io/docs/crypto/get_v1_marketstatus_now
5+
# https://polygon-api-client.readthedocs.io/en/latest/Reference.html#get-market-status
6+
7+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
8+
client = RESTClient() # POLYGON_API_KEY environment variable is used
9+
10+
result = client.get_market_status()
11+
print(result)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from polygon import RESTClient
2+
3+
# docs
4+
# https://polygon.io/docs/crypto/get_v2_aggs_ticker__cryptoticker__prev
5+
# https://polygon-api-client.readthedocs.io/en/latest/Aggs.html#get-previous-close-agg
6+
7+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
8+
client = RESTClient() # POLYGON_API_KEY environment variable is used
9+
10+
aggs = client.get_previous_close_agg(
11+
"X:BTCUSD",
12+
)
13+
14+
print(aggs)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from polygon import RESTClient
2+
from polygon.rest.models import (
3+
TickerSnapshot,
4+
Agg,
5+
)
6+
7+
# docs
8+
# https://polygon.io/docs/crypto/get_v2_snapshot_locale_global_markets_crypto_tickers
9+
# https://polygon-api-client.readthedocs.io/en/latest/Snapshot.html#get-all-snapshots
10+
11+
# client = RESTClient("XXXXXX") # hardcoded api_key is used
12+
client = RESTClient() # POLYGON_API_KEY environment variable is used
13+
14+
snapshot = client.get_snapshot_all("crypto") # all tickers
15+
16+
# print raw values
17+
print(snapshot)
18+
19+
# crunch some numbers
20+
for item in snapshot:
21+
22+
# verify this is an TickerSnapshot
23+
if isinstance(item, TickerSnapshot):
24+
25+
# verify this is an Agg
26+
if isinstance(item.prev_day, Agg):
27+
28+
# verify this is a float
29+
if isinstance(item.prev_day.open, float) and isinstance(
30+
item.prev_day.close, float
31+
):
32+
33+
percent_change = (
34+
(item.prev_day.close - item.prev_day.open)
35+
/ item.prev_day.open
36+
* 100
37+
)
38+
print(
39+
"{:<15}{:<15}{:<15}{:.2f} %".format(
40+
item.ticker,
41+
item.prev_day.open,
42+
item.prev_day.close,
43+
percent_change,
44+
)
45+
)

0 commit comments

Comments
 (0)
0