Skip to content

Commit

Permalink
implement futures orderbook history
Browse files Browse the repository at this point in the history
  • Loading branch information
wayne163 committed Jul 31, 2024
1 parent 5905362 commit 67095c2
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
11 changes: 11 additions & 0 deletions v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ type UserUniversalTransferType string
// UserUniversalTransferStatus define the user universal transfer status
type UserUniversalTransferStatusType string

// FuturesOrderBookHistoryDataType define the futures order book history data types
type FuturesOrderBookHistoryDataType string

// Endpoints
var (
BaseAPIMainURL = "https://api.binance.com"
Expand Down Expand Up @@ -287,6 +290,9 @@ const (
UserUniversalTransferStatusTypePending UserUniversalTransferStatusType = "PENDING"
UserUniversalTransferStatusTypeConfirmed UserUniversalTransferStatusType = "CONFIRMED"
UserUniversalTransferStatusTypeFailed UserUniversalTransferStatusType = "FAILED"

FuturesOrderBookHistoryDataTypeTDepth FuturesOrderBookHistoryDataType = "T_DEPTH"
FuturesOrderBookHistoryDataTypeSDepth FuturesOrderBookHistoryDataType = "S_DEPTH"
)

func currentTimestamp() int64 {
Expand Down Expand Up @@ -1291,3 +1297,8 @@ func (c *Client) NewSubAccountTransactionStatisticsService() *SubAccountTransact
func (c *Client) NewSubAccountFuturesAccountV2Service() *SubAccountFuturesAccountV2Service {
return &SubAccountFuturesAccountV2Service{c: c}
}

// Futures order book history service
func (c *Client) NewFuturesOrderBookHistoryService() *FuturesOrderBookHistoryService {
return &FuturesOrderBookHistoryService{c: c}
}
61 changes: 61 additions & 0 deletions v2/futures_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,64 @@ type FuturesTransfer struct {
Timestamp int64 `json:"timestamp"`
Status FuturesTransferStatusType `json:"status"`
}

type FuturesOrderBookHistoryService struct {
c *Client
symbol string
dataType string
startTime int64
endTime int64
}

func (s *FuturesOrderBookHistoryService) Symbol(symbol string) *FuturesOrderBookHistoryService {
s.symbol = symbol
return s
}

func (s *FuturesOrderBookHistoryService) DataType(dataType FuturesOrderBookHistoryDataType) *FuturesOrderBookHistoryService {
s.dataType = string(dataType)
return s
}

func (s *FuturesOrderBookHistoryService) StartTime(startTime int64) *FuturesOrderBookHistoryService {
s.startTime = startTime
return s
}

func (s *FuturesOrderBookHistoryService) EndTime(endTime int64) *FuturesOrderBookHistoryService {
s.endTime = endTime
return s
}

func (s *FuturesOrderBookHistoryService) Do(ctx context.Context, opts ...RequestOption) (res *FuturesOrderBookHistory, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/sapi/v1/futures/histDataLink",
secType: secTypeSigned,
}
r.setParams(params{
"symbol": s.symbol,
"dataType": s.dataType,
"startTime": s.startTime,
"endTime": s.endTime,
})
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
res = new(FuturesOrderBookHistory)
err = json.Unmarshal(data, &res)
if err != nil {
return nil, err
}
return res, nil
}

type FuturesOrderBookHistoryItem struct {
Day string `json:"day"`
Url string `json:"url"`
}

type FuturesOrderBookHistory struct {
Data []*FuturesOrderBookHistoryItem `json:"data"`
}
53 changes: 53 additions & 0 deletions v2/futures_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,56 @@ func (s *futuresTransferTestSuite) assertFuturesTransferEqual(e, a FuturesTransf
r.Equal(e.Timestamp, a.Timestamp, "Timestamp")
r.Equal(e.Status, a.Status, "Status")
}

type futuresOrderBookHistoryTestSuite struct {
baseTestSuite
}

func TestFuturesOrderBookHistoryService(t *testing.T) {
suite.Run(t, new(futuresOrderBookHistoryTestSuite))
}

func (s *futuresOrderBookHistoryTestSuite) TestFuturesOrderBookHistory() {
data := []byte(`{
"data": [
{
"day": "2023-06-30",
"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"
}
]
}`)
s.mockDo(data, nil)
defer s.assertDo()
symbol := "BTCUSDT"
dataType := FuturesOrderBookHistoryDataTypeTDepth
startTime := int64(1625040000000)
endTime := int64(1625126399999)
s.assertReq(func(r *request) {
e := newSignedRequest().setParams(params{
"symbol": symbol,
"dataType": "T_DEPTH",
"startTime": startTime,
"endTime": endTime,
})
s.assertRequestEqual(e, r)
})
res, err := s.client.NewFuturesOrderBookHistoryService().Symbol(symbol).
DataType(dataType).StartTime(startTime).EndTime(endTime).Do(newContext())
s.r().NoError(err)
e := &FuturesOrderBookHistory{
Data: []*FuturesOrderBookHistoryItem{
{
Day: "2023-06-30",
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",
},
},
}
s.assertFuturesOrderBookHistoryEqual(e, res)
}

func (s *futuresOrderBookHistoryTestSuite) assertFuturesOrderBookHistoryEqual(a, e *FuturesOrderBookHistory) {
for index, v := range a.Data {
v.Day = e.Data[index].Day
v.Url = e.Data[index].Url
}
}

0 comments on commit 67095c2

Please sign in to comment.