Skip to content

Commit

Permalink
feat: v5 get orderbook (#102)
Browse files Browse the repository at this point in the history
* feat: implement

* test: integration

* test: unit

* docs: update
  • Loading branch information
hirokisan authored Mar 9, 2023
1 parent d0165d4 commit 95ea813
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ The following API endpoints have been implemented
- [`/v5/market/index-price-kline` Get Index Price Kline](https://bybit-exchange.github.io/docs/v5/market/index-kline)
- [`/v5/market/premium-index-price-kline` Get Premium Index Price Kline](https://bybit-exchange.github.io/docs/v5/market/preimum-index-kline)
- [`/v5/market/instruments-info` Get Instruments Info](https://bybit-exchange.github.io/docs/v5/market/instrument)
- [`/v5/market/orderbook` Get Orderbook](https://bybit-exchange.github.io/docs/v5/market/orderbook)
- [`/v5/market/tickers` Get Tickers](https://bybit-exchange.github.io/docs/v5/market/tickers)

#### Position
Expand Down
14 changes: 14 additions & 0 deletions integrationtest/v5/market/market_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ func TestGetInstrumentsInfo(t *testing.T) {
}
}

func TestGetOrderbook(t *testing.T) {
client := bybit.NewTestClient()
res, err := client.V5().Market().GetOrderbook(bybit.V5GetOrderbookParam{
Category: bybit.CategoryV5Spot,
Symbol: bybit.SymbolV5BTCUSDT,
})
require.NoError(t, err)
{
goldenFilename := "./testdata/v5-market-get-orderbook.json"
testhelper.Compare(t, goldenFilename, testhelper.ConvertToJSON(res.Result))
testhelper.UpdateFile(t, goldenFilename, testhelper.ConvertToJSON(res.Result))
}
}

func TestGetTickers(t *testing.T) {
client := bybit.NewTestClient()
{
Expand Down
17 changes: 17 additions & 0 deletions integrationtest/v5/market/testdata/v5-market-get-orderbook.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"s": "BTCUSDT",
"b": [
{
"price": "21638.47",
"quantity": "0.047237"
}
],
"a": [
{
"price": "21643.13",
"quantity": "0.033243"
}
],
"ts": 1678369185237,
"u": 3936309
}
73 changes: 73 additions & 0 deletions v5_market_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type V5MarketServiceI interface {
GetIndexPriceKline(V5GetIndexPriceKlineParam) (*V5GetIndexPriceKlineResponse, error)
GetPremiumIndexPriceKline(V5GetPremiumIndexPriceKlineParam) (*V5GetPremiumIndexPriceKlineResponse, error)
GetInstrumentsInfo(V5GetInstrumentsInfoParam) (*V5GetInstrumentsInfoResponse, error)
GetOrderbook(V5GetOrderbookParam) (*V5GetOrderbookResponse, error)
GetTickers(V5GetTickersParam) (*V5GetTickersResponse, error)
}

Expand Down Expand Up @@ -485,6 +486,78 @@ func (s *V5MarketService) GetInstrumentsInfo(param V5GetInstrumentsInfoParam) (*
return &res, nil
}

// V5GetOrderbookParam :
type V5GetOrderbookParam struct {
Category CategoryV5 `url:"category"`
Symbol SymbolV5 `url:"symbol"`

// spot: [1, 50]. Default: 1.
// linear&inverse: [1, 200]. Default: 25.
// option: [1, 25]. Default: 1.
Limit *int `url:"limit,omitempty"`
}

// V5GetOrderbookResponse :
type V5GetOrderbookResponse struct {
CommonV5Response `json:",inline"`
Result V5GetOrderbookResult `json:"result"`
}

// V5GetOrderbookResult :
type V5GetOrderbookResult struct {
Symbol SymbolV5 `json:"s"`
Bids V5GetOrderbookBidAsks `json:"b"`
Asks V5GetOrderbookBidAsks `json:"a"`
Timestamp int64 `json:"ts"`
UpdateID int `json:"u"`
}

// V5GetOrderbookBidAsks :
type V5GetOrderbookBidAsks []V5GetOrderbookBidAsk

// UnmarshalJSON :
func (b *V5GetOrderbookBidAsks) UnmarshalJSON(data []byte) error {
parsedData := [][]string{}
if err := json.Unmarshal(data, &parsedData); err != nil {
return err
}
items := V5GetOrderbookBidAsks{}
for _, item := range parsedData {
item := item
if len(item) != 2 {
return errors.New("so far len(item) must be 2, please check it on documents")
}
items = append(items, V5GetOrderbookBidAsk{
Price: item[0],
Quantity: item[1],
})
}
*b = items
return nil
}

// V5GetOrderbookBidAsk :
type V5GetOrderbookBidAsk struct {
Price string `json:"price"`
Quantity string `json:"quantity"`
}

// GetOrderbook :
func (s *V5MarketService) GetOrderbook(param V5GetOrderbookParam) (*V5GetOrderbookResponse, error) {
var res V5GetOrderbookResponse

queryString, err := query.Values(param)
if err != nil {
return nil, err
}

if err := s.client.getPublicly("/v5/market/orderbook", queryString, &res); err != nil {
return nil, err
}

return &res, nil
}

// V5GetTickersParam :
type V5GetTickersParam struct {
Category CategoryV5 `url:"category"`
Expand Down
48 changes: 48 additions & 0 deletions v5_market_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,54 @@ func TestV5Market_GetInstrumentsInfo(t *testing.T) {
})
}

func TestV5Market_GetOrderbook(t *testing.T) {
param := V5GetOrderbookParam{
Category: CategoryV5Spot,
Symbol: SymbolV5BTCUSDT,
}

path := "/v5/market/orderbook"
method := http.MethodGet
status := http.StatusOK
respBody := map[string]interface{}{
"result": map[string]interface{}{
"s": "BTCUSDT",
"a": [][]interface{}{
{
"16638.64",
"0.008479",
},
},
"b": [][]interface{}{
{
"16638.27",
"0.305749",
},
},
"ts": 1672765737733,
"u": 5277055,
},
}
bytesBody, err := json.Marshal(respBody)
require.NoError(t, err)

server, teardown := testhelper.NewServer(
testhelper.WithHandlerOption(path, method, status, bytesBody),
)
defer teardown()

client := NewTestClient().
WithBaseURL(server.URL)

resp, err := client.V5().Market().GetOrderbook(param)
require.NoError(t, err)

require.NotNil(t, resp)
assert.Equal(t, respBody["result"].(map[string]interface{})["s"], string(resp.Result.Symbol))
assert.Equal(t, respBody["result"].(map[string]interface{})["a"].([][]interface{})[0][0], string(resp.Result.Asks[0].Price))
assert.Equal(t, respBody["result"].(map[string]interface{})["b"].([][]interface{})[0][0], string(resp.Result.Bids[0].Price))
}

func TestV5Market_GetTickers(t *testing.T) {
t.Run("linear", func(t *testing.T) {
param := V5GetTickersParam{
Expand Down

0 comments on commit 95ea813

Please sign in to comment.