mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-21 22:43:52 +00:00
kucoin: add orderbook api
This commit is contained in:
parent
18653aca7e
commit
4d57967664
|
@ -21,6 +21,7 @@ func init() {
|
|||
rootCmd.AddCommand(subAccountsCmd)
|
||||
rootCmd.AddCommand(symbolsCmd)
|
||||
rootCmd.AddCommand(tickersCmd)
|
||||
rootCmd.AddCommand(orderbookCmd)
|
||||
}
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
|
|
40
examples/kucoin/orderbook.go
Normal file
40
examples/kucoin/orderbook.go
Normal 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
|
||||
},
|
||||
}
|
|
@ -1,7 +1,10 @@
|
|||
package kucoinapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
|
@ -135,7 +138,7 @@ func (s *MarketDataService) GetTicker(symbol string) (*Ticker, error) {
|
|||
}
|
||||
]
|
||||
}
|
||||
*/
|
||||
*/
|
||||
|
||||
type Ticker24H struct {
|
||||
Symbol string `json:"symbol"`
|
||||
|
@ -163,7 +166,6 @@ type AllTickers struct {
|
|||
Ticker []Ticker24H `json:"ticker"`
|
||||
}
|
||||
|
||||
|
||||
func (s *MarketDataService) ListTickers() (*AllTickers, error) {
|
||||
req, err := s.client.newRequest("GET", "/api/v1/market/allTickers", nil, nil)
|
||||
if err != nil {
|
||||
|
@ -188,3 +190,64 @@ func (s *MarketDataService) ListTickers() (*AllTickers, error) {
|
|||
return apiResponse.Data, nil
|
||||
}
|
||||
|
||||
/*
|
||||
{
|
||||
"sequence": "3262786978",
|
||||
"time": 1550653727731,
|
||||
"bids": [["6500.12", "0.45054140"],
|
||||
["6500.11", "0.45054140"]], //[price,size]
|
||||
"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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user