kucoin: add orderbook api

This commit is contained in:
c9s 2021-12-11 01:41:23 +08:00
parent 18653aca7e
commit 4d57967664
3 changed files with 123 additions and 19 deletions

View File

@ -21,6 +21,7 @@ func init() {
rootCmd.AddCommand(subAccountsCmd) rootCmd.AddCommand(subAccountsCmd)
rootCmd.AddCommand(symbolsCmd) rootCmd.AddCommand(symbolsCmd)
rootCmd.AddCommand(tickersCmd) rootCmd.AddCommand(tickersCmd)
rootCmd.AddCommand(orderbookCmd)
} }
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{

View File

@ -0,0 +1,40 @@
package main
import (
"strconv"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var orderbookCmd = &cobra.Command{
Use: "orderbook",
// SilenceUsage is an option to silence usage when an error occurs.
SilenceUsage: true,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return nil
}
var depth = 0
if len(args) > 1 {
v, err := strconv.Atoi(args[1])
if err != nil {
return err
}
depth = v
}
orderBook, err := client.MarketDataService.GetOrderBook(args[0], depth)
if err != nil {
return err
}
logrus.Infof("orderBook: %+v", orderBook)
return nil
},
}

View File

@ -1,7 +1,10 @@
package kucoinapi package kucoinapi
import ( import (
"fmt"
"net/http"
"net/url" "net/url"
"strconv"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
@ -163,7 +166,6 @@ type AllTickers struct {
Ticker []Ticker24H `json:"ticker"` Ticker []Ticker24H `json:"ticker"`
} }
func (s *MarketDataService) ListTickers() (*AllTickers, error) { func (s *MarketDataService) ListTickers() (*AllTickers, error) {
req, err := s.client.newRequest("GET", "/api/v1/market/allTickers", nil, nil) req, err := s.client.newRequest("GET", "/api/v1/market/allTickers", nil, nil)
if err != nil { if err != nil {
@ -188,3 +190,64 @@ func (s *MarketDataService) ListTickers() (*AllTickers, error) {
return apiResponse.Data, nil return apiResponse.Data, nil
} }
/*
{
"sequence": "3262786978",
"time": 1550653727731,
"bids": [["6500.12", "0.45054140"],
["6500.11", "0.45054140"]], //[pricesize]
"asks": [["6500.16", "0.57753524"],
["6500.15", "0.57753524"]]
}
*/
type OrderBook struct {
Sequence string `json:"sequence"`
Time types.MillisecondTimestamp `json:"time"`
Bids [][]fixedpoint.Value `json:"bids,omitempty"`
Asks [][]fixedpoint.Value `json:"asks,omitempty"`
}
func (s *MarketDataService) GetOrderBook(symbol string, depth int) (*OrderBook, error) {
params := url.Values{}
params["symbol"] = []string{symbol}
var req *http.Request
var err error
switch depth {
case 20, 100:
refURL := "/api/v1/market/orderbook/level2_" + strconv.Itoa(depth)
req, err = s.client.newRequest("GET", refURL, params, nil)
if err != nil {
return nil, err
}
case 0:
refURL := "/api/v3/market/orderbook/level2"
req, err = s.client.newAuthenticatedRequest("GET", refURL, params, nil)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("depth %d is not supported, use 20, 100 or 0", depth)
}
response, err := s.client.sendRequest(req)
if err != nil {
return nil, err
}
var apiResponse struct {
Code string `json:"code"`
Message string `json:"msg"`
Data *OrderBook `json:"data"`
}
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
return apiResponse.Data, nil
}