Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement futures orderbook history #596

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 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 @@ -180,8 +183,10 @@ const (
MarginTransferTypeToMargin MarginTransferType = 1
MarginTransferTypeToMain MarginTransferType = 2

FuturesTransferTypeToFutures FuturesTransferType = 1
FuturesTransferTypeToMain FuturesTransferType = 2
FuturesTransferTypeToFutures FuturesTransferType = 1
FuturesTransferTypeToMain FuturesTransferType = 2
FuturesTransferTypeToFuturesCM FuturesTransferType = 3
FuturesTransferTypeFuturesCMToMain FuturesTransferType = 4

MarginLoanStatusTypePending MarginLoanStatusType = "PENDING"
MarginLoanStatusTypeConfirmed MarginLoanStatusType = "CONFIRMED"
Expand Down Expand Up @@ -287,6 +292,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 +1299,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}
}
69 changes: 66 additions & 3 deletions v2/futures_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (s *FuturesTransferService) Do(ctx context.Context, opts ...RequestOption)
// ListFuturesTransferService list futures transfer
type ListFuturesTransferService struct {
c *Client
asset string
asset *string
startTime int64
endTime *int64
current *int64
Expand All @@ -68,7 +68,7 @@ type ListFuturesTransferService struct {

// Asset set asset
func (s *ListFuturesTransferService) Asset(asset string) *ListFuturesTransferService {
s.asset = asset
s.asset = &asset
return s
}

Expand Down Expand Up @@ -104,9 +104,11 @@ func (s *ListFuturesTransferService) Do(ctx context.Context, opts ...RequestOpti
secType: secTypeSigned,
}
r.setParams(params{
"asset": s.asset,
"startTime": s.startTime,
})
if s.asset != nil {
r.setParam("asset", *s.asset)
}
if s.endTime != nil {
r.setParam("endTime", *s.endTime)
}
Expand Down Expand Up @@ -143,3 +145,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
}
}
Loading