kucoin: implement query tickers

This commit is contained in:
c9s 2021-12-22 01:26:00 +08:00
parent c32f3ab2f3
commit 0b6e66348e
3 changed files with 70 additions and 6 deletions

View File

@ -22,13 +22,20 @@ var tickersCmd = &cobra.Command{
return nil return nil
} }
ticker, err := client.MarketDataService.GetTicker(args[0]) ticker, err := client.MarketDataService.GetTicker(args[0])
if err != nil { if err != nil {
return err return err
} }
logrus.Infof("ticker: %+v", ticker) logrus.Infof("ticker: %+v", ticker)
tickerStats, err := client.MarketDataService.GetTicker24HStat(args[0])
if err != nil {
return err
}
logrus.Infof("ticker 24h stats: %+v", tickerStats)
return nil return nil
}, },
} }

View File

@ -104,11 +104,39 @@ func (e *Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error) {
} }
func (e *Exchange) QueryTicker(ctx context.Context, symbol string) (*types.Ticker, error) { func (e *Exchange) QueryTicker(ctx context.Context, symbol string) (*types.Ticker, error) {
panic("implement me") s, err := e.client.MarketDataService.GetTicker24HStat(symbol)
if err != nil {
return nil, err
} }
func (e *Exchange) QueryTickers(ctx context.Context, symbol ...string) (map[string]types.Ticker, error) { return &types.Ticker{
panic("implement me") Time: s.Time.Time(),
Volume: s.Volume.Float64(),
Last: s.Last.Float64(),
Open: s.Last.Float64() - s.ChangePrice.Float64(),
High: s.High.Float64(),
Low: s.Low.Float64(),
Buy: s.Buy.Float64(),
Sell: s.Sell.Float64(),
}, nil
}
func (e *Exchange) QueryTickers(ctx context.Context, symbols ...string) (map[string]types.Ticker, error) {
tickers := map[string]types.Ticker{}
if len(symbols) > 0 {
for _, s := range symbols {
t, err := e.QueryTicker(ctx, s)
if err != nil {
return nil, err
}
tickers[s] = *t
}
return tickers, nil
}
return tickers, nil
} }
func (e *Exchange) QueryKLines(ctx context.Context, symbol string, interval types.Interval, options types.KLineQueryOptions) ([]types.KLine, error) { func (e *Exchange) QueryKLines(ctx context.Context, symbol string, interval types.Interval, options types.KLineQueryOptions) ([]types.KLine, error) {

View File

@ -120,7 +120,7 @@ func (s *MarketDataService) GetTicker(symbol string) (*Ticker, error) {
"ticker":[ "ticker":[
{ {
"symbol": "BTC-USDT", // symbol "symbol": "BTC-USDT", // symbol
"symbolName":"BTC-USDT", // Name of trading pairs, it would change after renaming "symbolName":"BTC-USDT", // SymbolName of trading pairs, it would change after renaming
"buy": "11328.9", // bestAsk "buy": "11328.9", // bestAsk
"sell": "11329", // bestBid "sell": "11329", // bestBid
"changeRate": "-0.0055", // 24h change rate "changeRate": "-0.0055", // 24h change rate
@ -142,7 +142,7 @@ func (s *MarketDataService) GetTicker(symbol string) (*Ticker, error) {
type Ticker24H struct { type Ticker24H struct {
Symbol string `json:"symbol"` Symbol string `json:"symbol"`
Name string `json:"symbolName"` SymbolName string `json:"symbolName"`
Buy fixedpoint.Value `json:"buy"` Buy fixedpoint.Value `json:"buy"`
Sell fixedpoint.Value `json:"sell"` Sell fixedpoint.Value `json:"sell"`
ChangeRate fixedpoint.Value `json:"changeRate"` ChangeRate fixedpoint.Value `json:"changeRate"`
@ -159,6 +159,8 @@ type Ticker24H struct {
TakerCoefficient fixedpoint.Value `json:"takerCoefficient"` TakerCoefficient fixedpoint.Value `json:"takerCoefficient"`
MakerCoefficient fixedpoint.Value `json:"makerCoefficient"` MakerCoefficient fixedpoint.Value `json:"makerCoefficient"`
Time types.MillisecondTimestamp `json:"time"`
} }
type AllTickers struct { type AllTickers struct {
@ -190,6 +192,33 @@ func (s *MarketDataService) ListTickers() (*AllTickers, error) {
return apiResponse.Data, nil return apiResponse.Data, nil
} }
func (s *MarketDataService) GetTicker24HStat(symbol string) (*Ticker24H, error) {
var params = url.Values{}
params.Add("symbol", symbol)
req, err := s.client.newRequest("GET", "/api/v1/market/stats", params, nil)
if err != nil {
return nil, err
}
response, err := s.client.sendRequest(req)
if err != nil {
return nil, err
}
var apiResponse struct {
Code string `json:"code"`
Message string `json:"msg"`
Data *Ticker24H `json:"data"`
}
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
return apiResponse.Data, nil
}
/* /*
{ {
"sequence": "3262786978", "sequence": "3262786978",