Skip to content

Commit 336b313

Browse files
authored
Crypto api (#14)
* Added Crypto API endpoints
1 parent 36210ae commit 336b313

File tree

6 files changed

+324
-6
lines changed

6 files changed

+324
-6
lines changed

src/PolygonIO.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,22 @@ include("streaming_socket.jl")
1919

2020

2121
######### Global export of user API ################
22+
# Reference API
2223
export PolyOpts, tickers, ticker_types, ticker_details, ticker_details_vX,
2324
ticker_news, markets, locales, stock_splits, stock_dividends,
2425
stock_financials, market_holidays, market_status, stock_exchanges,
2526
condition_mappings, crypto_exchanges,
26-
27+
# Stock API
2728
trades, quotes_nbbo, last_trade_symbol, last_quote_symbol, daily_open_close,
2829
grouped_daily_bars, previous_close, aggregates_bars, snapshot_all_tickers,
29-
snapshot_ticker, snapshot_gainers_losers
30+
snapshot_ticker, snapshot_gainers_losers,
31+
# Crypto API
32+
last_trade_crypto_pair, crypto_daily_open_close, historic_crypto_trades,
33+
crypto_grouped_daily_bars, crypto_previous_close, crypto_aggregates_bars,
34+
crypto_snapshot_all_tickers, crypto_snapshot_ticker, crypto_snapshot_ticker_full_book,
35+
crypto_snapshot_gainers_losers
36+
# Forex API
37+
3038

3139

3240
end

src/crypto_api.jl

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
############ Last Trade For A Crypto Pair ####################
2+
"""
3+
"""
4+
function last_trade_crypto_pair(opts::PolyOpts, from="BTC", to="USD")
5+
params = Dict(
6+
"apiKey" => opts.api_key,
7+
"from" => from,
8+
"to" => to
9+
)
10+
last_trade_crypto_pair_url = "$crypto_last_trade_pair_base_url/$from/$to"
11+
12+
return generate_output_from_url(YesSinkYesLast(), last_trade_crypto_pair_url, params, opts.sink)
13+
end
14+
15+
16+
############################ Crypto Daily Open Close ####################
17+
"""
18+
"""
19+
function crypto_daily_open_close(opts::PolyOpts, from="BTC", to="USD", date="2020-10-14"; adjusted=true)
20+
params = Dict(
21+
"apiKey" => opts.api_key,
22+
"from" => from,
23+
"to" => to,
24+
"date" => date,
25+
"adjusted" => adjusted
26+
)
27+
crypto_daily_open_close_url = "$crypto_daily_open_close_base_url/$from/$to/$date"
28+
29+
return generate_output_from_url(NoSinkNoResults(), crypto_daily_open_close_url, params, opts.sink)
30+
31+
end
32+
33+
34+
############################ Historic Crypto Trades ####################
35+
"""
36+
"""
37+
function historic_crypto_trades(opts::PolyOpts, from="BTC", to="USD", date="2020-10-14"; limit=100, kwargs...)
38+
params = Dict(
39+
"apiKey" => opts.api_key,
40+
"from" => from,
41+
"to" => to,
42+
"date" => date,
43+
"limit" => limit
44+
)
45+
46+
merge!(params, Dict(kwargs))
47+
historic_crypto_trades_url = "$crypto_historic_trades_base_url/$from/$to/$date"
48+
return generate_output_from_url(NoSinkNoResults(), historic_crypto_trades_url, params, opts.sink)
49+
end
50+
51+
52+
############################ Crypto Grouped Daily Bars ####################
53+
"""
54+
"""
55+
function crypto_grouped_daily_bars(opts::PolyOpts, date="2020-10-14"; adjusted=true)
56+
params = Dict(
57+
"apiKey" => opts.api_key,
58+
"adjusted" => adjusted
59+
)
60+
61+
crypto_grouped_daily_bars_url = "$crypto_grouped_daily_bars_base_url/$date"
62+
return generate_output_from_url(YesSinkYesResults(), crypto_grouped_daily_bars_url, params, opts.sink)
63+
end
64+
65+
66+
############################ Crypto Previous Close ####################
67+
"""
68+
"""
69+
function crypto_previous_close(opts::PolyOpts, cryptoTicker="X:BTCUSD"; adjusted=true)
70+
params = Dict(
71+
"apiKey" => opts.api_key,
72+
"adjusted" => adjusted
73+
)
74+
75+
crypto_previous_close_url = "$crypto_previous_close_base_url/$cryptoTicker/prev"
76+
return generate_output_from_url(YesSinkYesResults(), crypto_previous_close_url, params, opts.sink)
77+
end
78+
79+
80+
############################ Crypto Aggregate Bars ####################
81+
"""
82+
"""
83+
function crypto_aggregates_bars(opts::PolyOpts,
84+
cryptoTicker="X:BTCUSD",
85+
multiplier=1,
86+
timespan="day",
87+
from="2020-10-14",
88+
to="2020-10-14";
89+
adjusted=true,
90+
sort="asc",
91+
limit=120,
92+
kwargs...)
93+
params = Dict(
94+
"apiKey" => opts.api_key,
95+
"adjusted" => adjusted,
96+
"from" => from,
97+
"to" => to,
98+
"sort" => sort,
99+
"limit" => limit
100+
)
101+
merge!(params, Dict(kwargs))
102+
crypto_aggregate_bars_url = "$crypto_aggregates_bars_base_url/$cryptoTicker/range/$multiplier/$timespan/$from/$to"
103+
return generate_output_from_url(YesSinkYesResults(), crypto_aggregate_bars_url, params, opts.sink)
104+
end
105+
106+
107+
############################ Crypto Snapshot All Tickers ####################
108+
"""
109+
"""
110+
function crypto_snapshot_all_tickers(opts::PolyOpts; kwargs...)
111+
params = Dict(
112+
"apiKey" => opts.api_key
113+
)
114+
merge!(params, Dict(kwargs))
115+
crypto_snapshot_all_tickers_url = "$crypto_snapshot_all_tickers_base_url"
116+
return generate_output_from_url(YesSinkYesTickers(), crypto_snapshot_all_tickers_url, params, opts.sink)
117+
end
118+
119+
120+
############################ Crypto Snapshot Ticker ####################
121+
"""
122+
"""
123+
function crypto_snapshot_ticker(opts::PolyOpts, ticker="X:BTCUSD")
124+
params = Dict(
125+
"apiKey" => opts.api_key
126+
)
127+
128+
crypto_snapshot_ticker_url = "$crypto_snapshot_ticker_base_url/$ticker"
129+
return generate_output_from_url(YesSinkYesTicker(), crypto_snapshot_ticker_url, params, opts.sink)
130+
end
131+
132+
############################ Crypto Snapshot Ticker Full Book ####################
133+
"""
134+
"""
135+
function crypto_snapshot_ticker_full_book(opts::PolyOpts, ticker="X:BTCUSD")
136+
params = Dict(
137+
"apiKey" => opts.api_key,
138+
)
139+
crypto_snapshot_ticker_full_book_url = "$crypto_snapshot_ticker_full_book_base_url/$ticker/book"
140+
return generate_output_from_url(YesSinkYesData(), crypto_snapshot_ticker_full_book_url, params, opts.sink)
141+
end
142+
143+
144+
############################ Crypto Snapshot Gainers/Losers ####################
145+
"""
146+
"""
147+
function crypto_snapshot_gainers_losers(opts::PolyOpts, direction="gainers")
148+
params = Dict(
149+
"apiKey" => opts.api_key
150+
)
151+
crypto_snapshot_gainers_losers_url = "$crypto_snapshot_gainers_losers_base_url/$direction"
152+
return generate_output_from_url(YesSinkYesTickers(), crypto_snapshot_gainers_losers_url, params, opts.sink)
153+
end

src/custom_structs.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,19 @@ struct PolyOpts <: AbstractPolyOptions
2424
sink::Any
2525
end
2626

27-
27+
# No Sink Structs
2828
struct NoSinkNoResults <: AbstractPolyChoice end
2929
struct NoSinkYesResults <: AbstractPolyChoice end
30+
struct NoSinkYesTickers <: AbstractPolyChoice end
31+
struct NoSinkYesTicker <: AbstractPolyChoice end
32+
struct NoSinkYesLast <: AbstractPolyChoice end
33+
struct NoSinkYesData <: AbstractPolyChoice end
34+
35+
36+
# Yes Sink Structs
37+
struct YesSinkYesLast <: AbstractPolyChoice end
38+
struct YesSinkYesTickers <: AbstractPolyChoice end
39+
struct YesSinkYesTicker <: AbstractPolyChoice end
3040
struct YesSinkNoResults <: AbstractPolyChoice end
3141
struct YesSinkYesResults <: AbstractPolyChoice end
32-
struct NoSinkYesTickers <: AbstractPolyChoice end
33-
struct NoSinkYesTicker <: AbstractPolyChoice end
42+
struct YesSinkYesData <: AbstractPolyChoice end

src/urls.jl

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,27 @@ snapshot_all_tickers_base_url = "https://api.polygon.io/v2/snapshot/locale/us/ma
4949

5050
snapshot_ticker_base_url = "https://api.polygon.io/v2/snapshot/locale/us/markets/stocks/tickers"
5151

52-
snapshot_gainers_losers_base_url = "https://api.polygon.io/v2/snapshot/locale/us/markets/stocks"
52+
snapshot_gainers_losers_base_url = "https://api.polygon.io/v2/snapshot/locale/us/markets/stocks"
53+
54+
55+
## Crypto API ##
56+
crypto_last_trade_pair_base_url = "https://api.polygon.io/v1/last/crypto"
57+
58+
crypto_daily_open_close_base_url = "https://api.polygon.io/v1/open-close/crypto"
59+
60+
crypto_historic_trades_base_url = "https://api.polygon.io/v1/historic/crypto"
61+
62+
crypto_grouped_daily_bars_base_url = "https://api.polygon.io/v2/aggs/grouped/locale/global/market/crypto"
63+
64+
crypto_previous_close_base_url = "https://api.polygon.io/v2/aggs/ticker"
65+
66+
crypto_aggregates_bars_base_url = "https://api.polygon.io/v2/aggs/ticker"
67+
68+
crypto_snapshot_all_tickers_base_url = "https://api.polygon.io/v2/snapshot/locale/global/markets/crypto/tickers"
69+
70+
crypto_snapshot_ticker_base_url = "https://api.polygon.io/v2/snapshot/locale/global/markets/crypto/tickers"
71+
72+
crypto_snapshot_ticker_full_book_base_url = "https://api.polygon.io/v2/snapshot/locale/global/markets/crypto/tickers"
73+
74+
crypto_snapshot_gainers_losers_base_url = "https://api.polygon.io/v2/snapshot/locale/global/markets/crypto"
75+

src/utils.jl

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,90 @@ function apply_choice(::NoSinkYesTickers, x, sink)
6464
end
6565

6666

67+
"""
68+
"""
69+
function apply_choice(::YesSinkYesTickers, x, sink)
70+
try
71+
return x.tickers |> jsontable |> sink
72+
catch
73+
return x.tickers |> x -> sink([x])
74+
end
75+
end
76+
77+
78+
"""
79+
"""
80+
function apply_choice(::YesSinkYesTickers, x, sink::Nothing)
81+
return apply_choice(NoSinkYesTickers(), x, sink)
82+
end
83+
84+
6785
"""
6886
"""
6987
function apply_choice(::NoSinkYesTicker, x, sink)
7088
return x.ticker
89+
end
90+
91+
92+
"""
93+
"""
94+
function apply_choice(::YesSinkYesLast, x, sink)
95+
return x.last |> x -> sink([x])
96+
end
97+
98+
99+
"""
100+
"""
101+
function apply_choice(::NoSinkYesLast, x, sink)
102+
return x.last
103+
end
104+
105+
106+
"""
107+
"""
108+
function apply_choice(::YesSinkYesLast, x, sink::Nothing)
109+
return apply_choice(NoSinkYesLast(), x, sink)
110+
end
111+
112+
113+
"""
114+
"""
115+
function apply_choice(::YesSinkYesTicker, x, sink)
116+
try
117+
return x.ticker |> jsontable |> sink
118+
catch
119+
return x.ticker |> x -> sink([x])
120+
end
121+
end
122+
123+
124+
"""
125+
"""
126+
function apply_choice(::YesSinkYesTicker, x, sink::Nothing)
127+
return x.ticker
128+
end
129+
130+
131+
"""
132+
"""
133+
function apply_choice(::NoSinkYesData, x, sink)
134+
return x.data
135+
end
136+
137+
138+
"""
139+
"""
140+
function apply_choice(::YesSinkYesData, x, sink)
141+
try
142+
return x.data |> jsontable |> sink
143+
catch
144+
return x.data |> x -> sink([x])
145+
end
146+
end
147+
148+
149+
"""
150+
"""
151+
function apply_choice(::YesSinkYesData, x, sink::Nothing)
152+
return apply_choice(NoSinkYesData(), x, sink)
71153
end

test/runtests.jl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,47 @@ end
118118
# snapshot_gainers_losers test
119119
@test snapshot_gainers_losers(tabular_opts, "losers") |> length >= 20
120120
@test snapshot_gainers_losers(regular_opts, "gainers") |> length >= 20
121+
end
122+
123+
124+
@testset "Crypto API" begin
125+
# last_trade_crypto_pair test
126+
@test last_trade_crypto_pair(regular_opts) |> length >= 5
127+
@test last_trade_crypto_pair(tabular_opts) |> length >= 1
128+
129+
# crypto_daily_open_close test
130+
@test crypto_daily_open_close(regular_opts, "BTC", "USD", "2020-10-14"; adjusted=true) |> length >= 7
131+
@test crypto_daily_open_close(tabular_opts, "BTC", "USD", "2020-10-14"; adjusted=false) |> length >= 7
132+
133+
# historic_crypto_trades test
134+
@test historic_crypto_trades(regular_opts, "BTC", "USD", "2020-10-14"; limit=100) |> length >= 7
135+
@test historic_crypto_trades(tabular_opts, "BTC", "USD", "2020-10-14"; limit=100) |> length >= 7
136+
137+
# crypto_grouped_daily_bars test
138+
@test crypto_grouped_daily_bars(regular_opts, "2020-10-14"; adjusted=true) |> length >= 1
139+
@test crypto_grouped_daily_bars(tabular_opts, "2020-10-14"; adjusted=false) |> length >= 1
140+
141+
# crypto_previous_close test
142+
@test crypto_previous_close(regular_opts, "X:BTCUSD"; adjusted=true) |> length == 1
143+
@test crypto_previous_close(tabular_opts, "X:BTCUSD"; adjusted=false) |> length == 1
144+
145+
# crypto_aggregates_bars test
146+
@test crypto_aggregates_bars(regular_opts, "X:BTCUSD", 1, "day", "2020-10-14", "2020-10-14"; adjusted=true) |> length == 1
147+
@test crypto_aggregates_bars(tabular_opts, "X:BTCUSD", 1, "day", "2020-10-14", "2020-10-14"; adjusted=false) |> length == 1
148+
149+
# crypto_snapshot_all_tickers test
150+
@test crypto_snapshot_all_tickers(regular_opts) |> length >= 1
151+
@test crypto_snapshot_all_tickers(tabular_opts) |> length >= 1
152+
153+
# crypto_snapshot_ticker test
154+
@test crypto_snapshot_ticker(regular_opts, "X:BTCUSD") |> length >= 1
155+
@test crypto_snapshot_ticker(tabular_opts, "X:BTCUSD") |> length >= 1
156+
157+
# crypto_snapshot_ticker_full_book
158+
@test crypto_snapshot_ticker_full_book(regular_opts, "X:BTCUSD") |> length >= 1
159+
@test crypto_snapshot_ticker_full_book(tabular_opts, "X:BTCUSD") |> length >= 1
160+
161+
# crypto_snapshot_gainers_losers test
162+
@test crypto_snapshot_gainers_losers(regular_opts, "gainers") |> length >= 1
163+
@test crypto_snapshot_gainers_losers(tabular_opts, "losers") |> length >= 1
121164
end

0 commit comments

Comments
 (0)