Skip to content

Commit

Permalink
Merge pull request #1760 from c9s/chiahung/max/query-depth
Browse files Browse the repository at this point in the history
FEATURE: [max] add QueryDepth v3 API to query orderbook
  • Loading branch information
kbearXD authored Oct 4, 2024
2 parents 0ba1fc5 + 2bbaf48 commit a548596
Show file tree
Hide file tree
Showing 4 changed files with 271 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pkg/exchange/max/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,20 @@ func convertWithdrawStatusV2(state max.WithdrawState) types.WithdrawStatus {
return types.WithdrawStatus(state)
}
}

func convertDepth(symbol string, depth *v3.Depth) (snapshot types.SliceOrderBook, finalUpdateID int64, err error) {
snapshot.Symbol = symbol
snapshot.Time = time.Unix(depth.Timestamp, 0)
snapshot.LastUpdateId = depth.LastUpdateId

finalUpdateID = depth.LastUpdateId
for _, entry := range depth.Bids {
snapshot.Bids = append(snapshot.Bids, types.PriceVolume{Price: entry[0], Volume: entry[1]})
}

for _, entry := range depth.Asks {
snapshot.Asks = append(snapshot.Asks, types.PriceVolume{Price: entry[0], Volume: entry[1]})
}

return snapshot, finalUpdateID, err
}
13 changes: 13 additions & 0 deletions pkg/exchange/max/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,19 @@ func (e *Exchange) QueryKLines(
return kLines, nil
}

func (e *Exchange) QueryDepth(ctx context.Context, symbol string, limit int) (snapshot types.SliceOrderBook, finalUpdateID int64, err error) {
req := e.v3client.NewGetDepthRequest()
req.Market(symbol)
req.Limit(limit)

depth, err := req.Do(ctx)
if err != nil {
return snapshot, finalUpdateID, err
}

return convertDepth(symbol, depth)
}

var Two = fixedpoint.NewFromInt(2)

func (e *Exchange) QueryAveragePrice(ctx context.Context, symbol string) (fixedpoint.Value, error) {
Expand Down
29 changes: 29 additions & 0 deletions pkg/exchange/max/maxapi/v3/get_depth_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package v3

//go:generate -command GetRequest requestgen -method GET

import (
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/requestgen"
)

type Depth struct {
Timestamp int64 `json:"timestamp"`
LastUpdateVersion int64 `json:"last_update_version"`
LastUpdateId int64 `json:"last_update_id"`
Bids [][]fixedpoint.Value `json:"bids"`
Asks [][]fixedpoint.Value `json:"asks"`
}

//go:generate GetRequest -url "/api/v3/depth" -type GetDepthRequest -responseType Depth
type GetDepthRequest struct {
client requestgen.APIClient

market string `param:"market,required"`
limit *int `param:"limit"`
sortByPrice *bool `param:"sort_by_price"`
}

func (s *Client) NewGetDepthRequest() *GetDepthRequest {
return &GetDepthRequest{client: s.Client}
}
212 changes: 212 additions & 0 deletions pkg/exchange/max/maxapi/v3/get_depth_request_requestgen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a548596

Please sign in to comment.