okex: implement QueryTickers

This commit is contained in:
c9s 2021-05-26 03:04:49 +08:00
parent c9aa0df054
commit c6c353b29a
4 changed files with 54 additions and 18 deletions

View File

@ -92,7 +92,7 @@ var rootCmd = &cobra.Command{
} }
log.Infof("MARKET TICKERS:") log.Infof("MARKET TICKERS:")
tickers, err := client.MarketTickers("SPOT") tickers, err := client.MarketTickers(okexapi.InstrumentTypeSpot)
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,6 +1,11 @@
package okex package okex
import "strings" import (
"strings"
"github.com/c9s/bbgo/pkg/exchange/okex/okexapi"
"github.com/c9s/bbgo/pkg/types"
)
func toGlobalSymbol(symbol string) string { func toGlobalSymbol(symbol string) string {
return strings.ReplaceAll(symbol, "-", "") return strings.ReplaceAll(symbol, "-", "")
@ -16,3 +21,17 @@ func toLocalSymbol(symbol string) string {
log.Errorf("failed to look up local symbol from %s", symbol) log.Errorf("failed to look up local symbol from %s", symbol)
return symbol return symbol
} }
func toGlobalTicker(marketTicker okexapi.MarketTicker) *types.Ticker {
return &types.Ticker{
Time: marketTicker.Timestamp.Time(),
Volume: marketTicker.Volume24H.Float64(),
Last: marketTicker.Last.Float64(),
Open: marketTicker.Open24H.Float64(),
High: marketTicker.High24H.Float64(),
Low: marketTicker.Low24H.Float64(),
Buy: marketTicker.BidPrice.Float64(),
Sell: marketTicker.AskPrice.Float64(),
}
}

View File

@ -2,6 +2,7 @@ package okex
import ( import (
"context" "context"
"fmt"
"math" "math"
"github.com/c9s/bbgo/pkg/exchange/okex/okexapi" "github.com/c9s/bbgo/pkg/exchange/okex/okexapi"
@ -87,20 +88,36 @@ func (e *Exchange) QueryTicker(ctx context.Context, symbol string) (*types.Ticke
return nil, err return nil, err
} }
return &types.Ticker{ return toGlobalTicker(*marketTicker), nil
Time: marketTicker.Timestamp.Time(),
Volume: marketTicker.Volume24H.Float64(),
Last: marketTicker.Last.Float64(),
Open: marketTicker.Open24H.Float64(),
High: marketTicker.High24H.Float64(),
Low: marketTicker.Low24H.Float64(),
Buy: marketTicker.BidPrice.Float64(),
Sell: marketTicker.AskPrice.Float64(),
}, nil
} }
func (e *Exchange) QueryTickers(ctx context.Context, symbol string) (*types.Ticker, error) { func (e *Exchange) QueryTickers(ctx context.Context, symbols ...string) (map[string]types.Ticker, error) {
return nil, nil marketTickers, err := e.client.MarketTickers(okexapi.InstrumentTypeSpot)
if err != nil {
return nil, err
}
tickers := make(map[string]types.Ticker)
for _, marketTicker := range marketTickers {
symbol := toGlobalSymbol(marketTicker.InstrumentID)
ticker := toGlobalTicker(marketTicker)
tickers[symbol] = *ticker
}
if len(symbols) == 0 {
return tickers, nil
}
selectedTickers := make(map[string]types.Ticker)
for _, symbol := range symbols {
if ticker, ok := tickers[symbol]; ok {
selectedTickers[symbol] = ticker
} else {
return selectedTickers, fmt.Errorf("ticker of symbol %s not found", symbols)
}
}
return selectedTickers, nil
} }
func (e *Exchange) PlatformFeeCurrency() string { func (e *Exchange) PlatformFeeCurrency() string {

View File

@ -335,7 +335,7 @@ func (c *RestClient) MarketTicker(instId string) (*MarketTicker, error) {
var params = url.Values{} var params = url.Values{}
params.Add("instId", instId) params.Add("instId", instId)
req, err := c.newAuthenticatedRequest("GET", "/api/v5/market/ticker", params, nil) req, err := c.newRequest("GET", "/api/v5/market/ticker", params, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -361,12 +361,12 @@ func (c *RestClient) MarketTicker(instId string) (*MarketTicker, error) {
return &tickerResponse.Data[0], nil return &tickerResponse.Data[0], nil
} }
func (c *RestClient) MarketTickers(instType string) ([]MarketTicker, error) { func (c *RestClient) MarketTickers(instType InstrumentType) ([]MarketTicker, error) {
// SPOT, SWAP, FUTURES, OPTION // SPOT, SWAP, FUTURES, OPTION
var params = url.Values{} var params = url.Values{}
params.Add("instType", instType) params.Add("instType", string(instType))
req, err := c.newAuthenticatedRequest("GET", "/api/v5/market/tickers", params, nil) req, err := c.newRequest("GET", "/api/v5/market/tickers", params, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }