mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 14:55:16 +00:00
okex: implement QueryTickers
This commit is contained in:
parent
c9aa0df054
commit
c6c353b29a
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user