Skip to content

Commit 6189b9c

Browse files
authored
Merge pull request #596 from Croomburg/feature/futures-history-data
implement futures orderbook history
2 parents d45dcb3 + 5dd8ab3 commit 6189b9c

File tree

3 files changed

+134
-5
lines changed

3 files changed

+134
-5
lines changed

v2/client.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ type UserUniversalTransferType string
109109
// UserUniversalTransferStatus define the user universal transfer status
110110
type UserUniversalTransferStatusType string
111111

112+
// FuturesOrderBookHistoryDataType define the futures order book history data types
113+
type FuturesOrderBookHistoryDataType string
114+
112115
// Endpoints
113116
var (
114117
BaseAPIMainURL = "https://api.binance.com"
@@ -180,8 +183,10 @@ const (
180183
MarginTransferTypeToMargin MarginTransferType = 1
181184
MarginTransferTypeToMain MarginTransferType = 2
182185

183-
FuturesTransferTypeToFutures FuturesTransferType = 1
184-
FuturesTransferTypeToMain FuturesTransferType = 2
186+
FuturesTransferTypeToFutures FuturesTransferType = 1
187+
FuturesTransferTypeToMain FuturesTransferType = 2
188+
FuturesTransferTypeToFuturesCM FuturesTransferType = 3
189+
FuturesTransferTypeFuturesCMToMain FuturesTransferType = 4
185190

186191
MarginLoanStatusTypePending MarginLoanStatusType = "PENDING"
187192
MarginLoanStatusTypeConfirmed MarginLoanStatusType = "CONFIRMED"
@@ -287,6 +292,9 @@ const (
287292
UserUniversalTransferStatusTypePending UserUniversalTransferStatusType = "PENDING"
288293
UserUniversalTransferStatusTypeConfirmed UserUniversalTransferStatusType = "CONFIRMED"
289294
UserUniversalTransferStatusTypeFailed UserUniversalTransferStatusType = "FAILED"
295+
296+
FuturesOrderBookHistoryDataTypeTDepth FuturesOrderBookHistoryDataType = "T_DEPTH"
297+
FuturesOrderBookHistoryDataTypeSDepth FuturesOrderBookHistoryDataType = "S_DEPTH"
290298
)
291299

292300
func currentTimestamp() int64 {
@@ -1291,3 +1299,8 @@ func (c *Client) NewSubAccountTransactionStatisticsService() *SubAccountTransact
12911299
func (c *Client) NewSubAccountFuturesAccountV2Service() *SubAccountFuturesAccountV2Service {
12921300
return &SubAccountFuturesAccountV2Service{c: c}
12931301
}
1302+
1303+
// Futures order book history service
1304+
func (c *Client) NewFuturesOrderBookHistoryService() *FuturesOrderBookHistoryService {
1305+
return &FuturesOrderBookHistoryService{c: c}
1306+
}

v2/futures_service.go

+66-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (s *FuturesTransferService) Do(ctx context.Context, opts ...RequestOption)
5959
// ListFuturesTransferService list futures transfer
6060
type ListFuturesTransferService struct {
6161
c *Client
62-
asset string
62+
asset *string
6363
startTime int64
6464
endTime *int64
6565
current *int64
@@ -68,7 +68,7 @@ type ListFuturesTransferService struct {
6868

6969
// Asset set asset
7070
func (s *ListFuturesTransferService) Asset(asset string) *ListFuturesTransferService {
71-
s.asset = asset
71+
s.asset = &asset
7272
return s
7373
}
7474

@@ -104,9 +104,11 @@ func (s *ListFuturesTransferService) Do(ctx context.Context, opts ...RequestOpti
104104
secType: secTypeSigned,
105105
}
106106
r.setParams(params{
107-
"asset": s.asset,
108107
"startTime": s.startTime,
109108
})
109+
if s.asset != nil {
110+
r.setParam("asset", *s.asset)
111+
}
110112
if s.endTime != nil {
111113
r.setParam("endTime", *s.endTime)
112114
}
@@ -143,3 +145,64 @@ type FuturesTransfer struct {
143145
Timestamp int64 `json:"timestamp"`
144146
Status FuturesTransferStatusType `json:"status"`
145147
}
148+
149+
type FuturesOrderBookHistoryService struct {
150+
c *Client
151+
symbol string
152+
dataType string
153+
startTime int64
154+
endTime int64
155+
}
156+
157+
func (s *FuturesOrderBookHistoryService) Symbol(symbol string) *FuturesOrderBookHistoryService {
158+
s.symbol = symbol
159+
return s
160+
}
161+
162+
func (s *FuturesOrderBookHistoryService) DataType(dataType FuturesOrderBookHistoryDataType) *FuturesOrderBookHistoryService {
163+
s.dataType = string(dataType)
164+
return s
165+
}
166+
167+
func (s *FuturesOrderBookHistoryService) StartTime(startTime int64) *FuturesOrderBookHistoryService {
168+
s.startTime = startTime
169+
return s
170+
}
171+
172+
func (s *FuturesOrderBookHistoryService) EndTime(endTime int64) *FuturesOrderBookHistoryService {
173+
s.endTime = endTime
174+
return s
175+
}
176+
177+
func (s *FuturesOrderBookHistoryService) Do(ctx context.Context, opts ...RequestOption) (res *FuturesOrderBookHistory, err error) {
178+
r := &request{
179+
method: http.MethodGet,
180+
endpoint: "/sapi/v1/futures/histDataLink",
181+
secType: secTypeSigned,
182+
}
183+
r.setParams(params{
184+
"symbol": s.symbol,
185+
"dataType": s.dataType,
186+
"startTime": s.startTime,
187+
"endTime": s.endTime,
188+
})
189+
data, err := s.c.callAPI(ctx, r, opts...)
190+
if err != nil {
191+
return nil, err
192+
}
193+
res = new(FuturesOrderBookHistory)
194+
err = json.Unmarshal(data, &res)
195+
if err != nil {
196+
return nil, err
197+
}
198+
return res, nil
199+
}
200+
201+
type FuturesOrderBookHistoryItem struct {
202+
Day string `json:"day"`
203+
Url string `json:"url"`
204+
}
205+
206+
type FuturesOrderBookHistory struct {
207+
Data []*FuturesOrderBookHistoryItem `json:"data"`
208+
}

v2/futures_service_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,56 @@ func (s *futuresTransferTestSuite) assertFuturesTransferEqual(e, a FuturesTransf
105105
r.Equal(e.Timestamp, a.Timestamp, "Timestamp")
106106
r.Equal(e.Status, a.Status, "Status")
107107
}
108+
109+
type futuresOrderBookHistoryTestSuite struct {
110+
baseTestSuite
111+
}
112+
113+
func TestFuturesOrderBookHistoryService(t *testing.T) {
114+
suite.Run(t, new(futuresOrderBookHistoryTestSuite))
115+
}
116+
117+
func (s *futuresOrderBookHistoryTestSuite) TestFuturesOrderBookHistory() {
118+
data := []byte(`{
119+
"data": [
120+
{
121+
"day": "2023-06-30",
122+
"url": "https://bin-prod-user-rebate-bucket.s3.ap-northeast-1.amazonaws.com/future-data-symbol-update/2023-06-30/BTCUSDT_T_DEPTH_2023-06-30.tar.gz?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20230925T025710Z&X-Amz-SignedHeaders=host&X-Amz-Expires=86399&X-Amz-Credential=AKIAVL364M5ZNFZ74IPP%2F20230925%2Fap-northeast-1%2Fs3%2Faws4_request&X-Amz-Signature=5fffcb390d10f34d71615726f81f99e42d80a11532edeac77b858c51a88cbf59"
123+
}
124+
]
125+
}`)
126+
s.mockDo(data, nil)
127+
defer s.assertDo()
128+
symbol := "BTCUSDT"
129+
dataType := FuturesOrderBookHistoryDataTypeTDepth
130+
startTime := int64(1625040000000)
131+
endTime := int64(1625126399999)
132+
s.assertReq(func(r *request) {
133+
e := newSignedRequest().setParams(params{
134+
"symbol": symbol,
135+
"dataType": "T_DEPTH",
136+
"startTime": startTime,
137+
"endTime": endTime,
138+
})
139+
s.assertRequestEqual(e, r)
140+
})
141+
res, err := s.client.NewFuturesOrderBookHistoryService().Symbol(symbol).
142+
DataType(dataType).StartTime(startTime).EndTime(endTime).Do(newContext())
143+
s.r().NoError(err)
144+
e := &FuturesOrderBookHistory{
145+
Data: []*FuturesOrderBookHistoryItem{
146+
{
147+
Day: "2023-06-30",
148+
Url: "https://bin-prod-user-rebate-bucket.s3.ap-northeast-1.amazonaws.com/future-data-symbol-update/2023-06-30/BTCUSDT_T_DEPTH_2023-06-30.tar.gz?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20230925T025710Z&X-Amz-SignedHeaders=host&X-Amz-Expires=86399&X-Amz-Credential=AKIAVL364M5ZNFZ74IPP%2F20230925%2Fap-northeast-1%2Fs3%2Faws4_request&X-Amz-Signature=5fffcb390d10f34d71615726f81f99e42d80a11532edeac77b858c51a88cbf59",
149+
},
150+
},
151+
}
152+
s.assertFuturesOrderBookHistoryEqual(e, res)
153+
}
154+
155+
func (s *futuresOrderBookHistoryTestSuite) assertFuturesOrderBookHistoryEqual(a, e *FuturesOrderBookHistory) {
156+
for index, v := range a.Data {
157+
v.Day = e.Data[index].Day
158+
v.Url = e.Data[index].Url
159+
}
160+
}

0 commit comments

Comments
 (0)