Skip to content

Commit ded17f0

Browse files
germanzelentsovgryumovyshadrinnuteaterartememelin
authored
Add endpoints to Binance Futures API (#20)
Co-authored-by: Stas Gryumov <33372067+gryumov@users.noreply.github.com> Co-authored-by: Yuri Shadrin <53979055+yshadrin@users.noreply.github.com> Co-authored-by: nuteater <60854537+nuteater@users.noreply.github.com> Co-authored-by: Artem Emelin <153018291+artememelin@users.noreply.github.com> Co-authored-by: Gleb <33556795+sirion34@users.noreply.github.com>
1 parent 38858b6 commit ded17f0

File tree

13 files changed

+741
-3
lines changed

13 files changed

+741
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "CryptoAPIs"
22
uuid = "5e3d4798-c815-4641-85e1-deed530626d3"
3-
version = "0.9.0"
3+
version = "0.10.0"
44

55
[deps]
66
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

docs/src/pages/Binance.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,17 @@ CryptoAPIs.Binance.USDMFutures.public_client
3131

3232
```@docs
3333
CryptoAPIs.Binance.USDMFutures.candle
34+
CryptoAPIs.Binance.USDMFutures.continuous_candle
3435
CryptoAPIs.Binance.USDMFutures.exchange_info
3536
CryptoAPIs.Binance.USDMFutures.funding_rate
37+
CryptoAPIs.Binance.USDMFutures.long_short_ratio
38+
CryptoAPIs.Binance.USDMFutures.open_interest_hist
3639
CryptoAPIs.Binance.USDMFutures.order_book
40+
CryptoAPIs.Binance.USDMFutures.premium_index
41+
CryptoAPIs.Binance.USDMFutures.taker_long_short_ratio
3742
CryptoAPIs.Binance.USDMFutures.ticker
43+
CryptoAPIs.Binance.USDMFutures.top_long_short_account_ratio
44+
CryptoAPIs.Binance.USDMFutures.top_long_short_position_ratio
3845
```
3946

4047
## CoinMFutures

examples/Binance/CoinMFutures.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ Binance.CoinMFutures.income_log(
5252
startTime = DateTime("2023-06-22"),
5353
endTime = DateTime("2023-07-23"),
5454
limit = 1000,
55-
)
55+
)

examples/Binance/USDMFutures.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,44 @@ Binance.USDMFutures.candle(;
1818
limit = 5,
1919
)
2020

21+
Binance.USDMFutures.continuous_candle(;
22+
pair = "BTCUSDT",
23+
contractType = Binance.USDMFutures.ContinuousCandle.PERPETUAL,
24+
interval = Binance.USDMFutures.ContinuousCandle.M1,
25+
)
26+
2127
Binance.USDMFutures.exchange_info()
2228

2329
Binance.USDMFutures.funding_rate(; symbol = "BTCUSDT")
2430

31+
Binance.USDMFutures.long_short_ratio(;
32+
symbol = "BTCUSDT",
33+
period = Binance.USDMFutures.LongShortRatio.h1,
34+
)
35+
36+
Binance.USDMFutures.open_interest_hist(;
37+
symbol = "BTCUSDT",
38+
period = Binance.USDMFutures.OpenInterestHist.h1,
39+
)
40+
2541
Binance.USDMFutures.order_book(; symbol = "BTCUSDT")
2642
Binance.USDMFutures.order_book(; symbol = "BTCUSDT", limit = 10)
2743

44+
Binance.USDMFutures.premium_index(; symbol = "BTCUSDT")
45+
46+
Binance.USDMFutures.taker_long_short_ratio(;
47+
symbol = "BTCUSDT",
48+
period = Binance.USDMFutures.TakerLongShortRatio.h1,
49+
)
50+
2851
Binance.USDMFutures.ticker(; symbol = "BTCUSDT")
52+
53+
Binance.USDMFutures.top_long_short_account_ratio(;
54+
symbol = "BTCUSDT",
55+
period = Binance.USDMFutures.TopLongShortAccountRatio.h1,
56+
)
57+
58+
Binance.USDMFutures.top_long_short_position_ratio(;
59+
symbol = "BTCUSDT",
60+
period = Binance.USDMFutures.TopLongShortPositionRatio.h1,
61+
)
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
module ContinuousCandle
2+
3+
export ContinuousCandleQuery,
4+
ContinuousCandleData,
5+
continuous_candle
6+
7+
using Serde
8+
using Dates, NanoDates, TimeZones
9+
10+
using CryptoAPIs.Binance
11+
using CryptoAPIs: Maybe, APIsRequest
12+
13+
@enum TimeInterval m1 m3 m5 m15 m30 h1 h2 h4 h6 h8 h12 d1 d3 w1 M1
14+
15+
@enum ContractType PERPETUAL CURRENT_QUARTER NEXT_QUARTER
16+
17+
Base.@kwdef struct ContinuousCandleQuery <: BinancePublicQuery
18+
pair::String
19+
contractType::ContractType
20+
interval::TimeInterval
21+
limit::Maybe{Int64} = nothing
22+
endTime::Maybe{DateTime} = nothing
23+
startTime::Maybe{DateTime} = nothing
24+
end
25+
26+
function Serde.ser_type(::Type{<:ContinuousCandleQuery}, x::TimeInterval)::String
27+
x == m1 && return "1m"
28+
x == m3 && return "3m"
29+
x == m5 && return "5m"
30+
x == m15 && return "15m"
31+
x == m30 && return "30m"
32+
x == h1 && return "1h"
33+
x == h2 && return "2h"
34+
x == h4 && return "4h"
35+
x == h6 && return "6h"
36+
x == h8 && return "8h"
37+
x == h12 && return "12h"
38+
x == d1 && return "1d"
39+
x == d3 && return "3d"
40+
x == w1 && return "1w"
41+
x == M1 && return "1M"
42+
end
43+
44+
struct ContinuousCandleData <: BinanceData
45+
openTime::NanoDate
46+
openPrice::Maybe{Float64}
47+
highPrice::Maybe{Float64}
48+
lowPrice::Maybe{Float64}
49+
closePrice::Maybe{Float64}
50+
volume::Maybe{Float64}
51+
closeTime::NanoDate
52+
quoteAssetVolume::Maybe{Float64}
53+
tradesNumber::Maybe{Int64}
54+
takerBuyVolume::Maybe{Float64}
55+
takerBuyQuoteAssetVolume::Maybe{Float64}
56+
end
57+
58+
"""
59+
continuous_candle(client::BinanceClient, query::ContinuousCandleQuery)
60+
continuous_candle(client::BinanceClient = Binance.USDMFutures.public_client; kw...)
61+
62+
Kline/candlestick bars for a specific contract type.
63+
64+
[`GET fapi/v1/continuousKlines`](https://binance-docs.github.io/apidocs/futures/en/#continuous-contract-kline-candlestick-data)
65+
66+
## Parameters:
67+
68+
| Parameter | Type | Required | Description |
69+
|:-------------|:---------------|:---------|:--------------------------------------------------------------|
70+
| pair | String | true | |
71+
| contractType | ContractType | true | PERPETUAL, CURRENT\\_QUARTER, NEXT\\_QUARTER |
72+
| interval | TimeInterval | true | m1, m3, m5, m15, m30, h1, h2, h4, h6, h8, h12, d1, d3, w1, M1 |
73+
| endTime | DateTime | false | |
74+
| limit | Int64 | false | |
75+
| startTime | DateTime | false |
76+
77+
## Code samples:
78+
79+
```julia
80+
using Serde
81+
using CryptoAPIs.Binance
82+
83+
result = Binance.USDMFutures.continuous_candle(;
84+
pair = "BTCUSDT",
85+
contractType = Binance.USDMFutures.ContinuousCandle.PERPETUAL,
86+
interval = Binance.USDMFutures.ContinuousCandle.M1,
87+
)
88+
89+
to_pretty_json(result.result)
90+
```
91+
92+
## Result:
93+
94+
```json
95+
[
96+
{
97+
"openTime":"2019-09-01T00:00:00",
98+
"openPrice":8042.08,
99+
"highPrice":10475.54,
100+
"lowPrice":7700.67,
101+
"closePrice":8041.96,
102+
"volume":608742.1109999999,
103+
"closeTime":"2019-09-30T23:59:59.999000064",
104+
"quoteAssetVolume":5.61187924896223e9,
105+
"tradesNumber":998055,
106+
"takerBuyVolume":298326.244,
107+
"takerBuyQuoteAssetVolume":2.7368038906708302e9
108+
},
109+
...
110+
]
111+
```
112+
"""
113+
function continuous_candle(client::BinanceClient, query::ContinuousCandleQuery)
114+
return APIsRequest{Vector{ContinuousCandleData}}("GET", "fapi/v1/continuousKlines", query)(client)
115+
end
116+
117+
function continuous_candle(client::BinanceClient = Binance.USDMFutures.public_client; kw...)
118+
return continuous_candle(client, ContinuousCandleQuery(; kw...))
119+
end
120+
121+
end

src/Binance/USDMFutures/API/FundingRate.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,4 @@ function funding_rate(client::BinanceClient = Binance.USDMFutures.public_client;
8080
return funding_rate(client, FundingRateQuery(; kw...))
8181
end
8282

83-
end
83+
end
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
module LongShortRatio
2+
3+
export LongShortRatioQuery,
4+
LongShortRatioData,
5+
long_short_ratio
6+
7+
using Serde
8+
using Dates, NanoDates, TimeZones
9+
10+
using CryptoAPIs.Binance
11+
using CryptoAPIs: Maybe, APIsRequest
12+
13+
@enum TimeInterval m5 m15 m30 h1 h2 h4 h6 h12 d1
14+
15+
Base.@kwdef struct LongShortRatioQuery <: BinancePublicQuery
16+
symbol::String
17+
period::TimeInterval
18+
limit::Maybe{Int64} = nothing
19+
endTime::Maybe{DateTime} = nothing
20+
startTime::Maybe{DateTime} = nothing
21+
end
22+
23+
function Serde.ser_type(::Type{<:LongShortRatioQuery}, x::TimeInterval)::String
24+
x == m5 && return "5m"
25+
x == m15 && return "15m"
26+
x == m30 && return "30m"
27+
x == h1 && return "1h"
28+
x == h2 && return "2h"
29+
x == h4 && return "4h"
30+
x == h6 && return "6h"
31+
x == h12 && return "12h"
32+
x == d1 && return "1d"
33+
end
34+
35+
struct LongShortRatioData <: BinanceData
36+
symbol::String
37+
longShortRatio::Maybe{Float64}
38+
longAccount::Maybe{Float64}
39+
shortAccount::Maybe{Float64}
40+
timestamp::NanoDate
41+
end
42+
43+
"""
44+
long_short_ratio(client::BinanceClient, query::LongShortRatioQuery)
45+
long_short_ratio(client::BinanceClient = Binance.USDMFutures.public_client; kw...)
46+
47+
[`GET futures/data/globalLongShortAccountRatio`](https://binance-docs.github.io/apidocs/futures/en/#long-short-ratio)
48+
49+
## Parameters:
50+
51+
| Parameter | Type | Required | Description |
52+
|:-------------|:---------------|:---------|:-----------------------------------------------|
53+
| symbol | String | true | |
54+
| period | TimeInterval | true | m5, m15, m30, h1, h2, h4, h6, h12, d1 |
55+
| endTime | DateTime | false | |
56+
| limit | Int64 | false | default 30, max 500 |
57+
| startTime | DateTime | false | |
58+
59+
## Code samples:
60+
61+
```julia
62+
using Serde
63+
using CryptoAPIs.Binance
64+
65+
result = Binance.USDMFutures.long_short_ratio(;
66+
symbol = "BTCUSDT",
67+
period = Binance.USDMFutures.LongShortRatio.h1,
68+
)
69+
70+
to_pretty_json(result.result)
71+
```
72+
73+
## Result:
74+
75+
```json
76+
[
77+
{
78+
"symbol":"BTCUSDT",
79+
"longShortRatio":1.3305,
80+
"longAccount":0.5709,
81+
"shortAccount":0.4291,
82+
"timestamp":"2024-03-29T12:00:00"
83+
},
84+
...
85+
]
86+
```
87+
"""
88+
function long_short_ratio(client::BinanceClient, query::LongShortRatioQuery)
89+
return APIsRequest{Vector{LongShortRatioData}}("GET", "futures/data/globalLongShortAccountRatio", query)(client)
90+
end
91+
92+
function long_short_ratio(client::BinanceClient = Binance.USDMFutures.public_client; kw...)
93+
return long_short_ratio(client, LongShortRatioQuery(; kw...))
94+
end
95+
96+
end

0 commit comments

Comments
 (0)