mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 16:25:16 +00:00
pkg/exchange: use v2 query ticker
This commit is contained in:
parent
5e5b8e1388
commit
53bce6d5c1
|
@ -93,4 +93,10 @@ func TestClient(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
t.Logf("resp: %+v", resp)
|
||||
})
|
||||
|
||||
t.Run("GetTickersRequest", func(t *testing.T) {
|
||||
resp, err := client.NewGetTickersRequest().Do(ctx)
|
||||
assert.NoError(t, err)
|
||||
t.Logf("resp: %+v", resp)
|
||||
})
|
||||
}
|
||||
|
|
40
pkg/exchange/bitget/bitgetapi/v2/get_tickers_request.go
Normal file
40
pkg/exchange/bitget/bitgetapi/v2/get_tickers_request.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package bitgetapi
|
||||
|
||||
import (
|
||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
"github.com/c9s/requestgen"
|
||||
)
|
||||
|
||||
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
|
||||
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data
|
||||
|
||||
type Ticker struct {
|
||||
Symbol string `json:"symbol"`
|
||||
High24H fixedpoint.Value `json:"high24h"`
|
||||
Open fixedpoint.Value `json:"open"`
|
||||
Low24H fixedpoint.Value `json:"low24h"`
|
||||
LastPr fixedpoint.Value `json:"lastPr"`
|
||||
QuoteVolume fixedpoint.Value `json:"quoteVolume"`
|
||||
BaseVolume fixedpoint.Value `json:"baseVolume"`
|
||||
UsdtVolume fixedpoint.Value `json:"usdtVolume"`
|
||||
BidPr fixedpoint.Value `json:"bidPr"`
|
||||
AskPr fixedpoint.Value `json:"askPr"`
|
||||
BidSz fixedpoint.Value `json:"bidSz"`
|
||||
AskSz fixedpoint.Value `json:"askSz"`
|
||||
OpenUtc fixedpoint.Value `json:"openUtc"`
|
||||
Ts types.MillisecondTimestamp `json:"ts"`
|
||||
ChangeUtc24H fixedpoint.Value `json:"changeUtc24h"`
|
||||
Change24H fixedpoint.Value `json:"change24h"`
|
||||
}
|
||||
|
||||
//go:generate GetRequest -url "/api/v2/spot/market/tickers" -type GetTickersRequest -responseDataType []Ticker
|
||||
type GetTickersRequest struct {
|
||||
client requestgen.APIClient
|
||||
|
||||
symbol *string `param:"symbol,query"`
|
||||
}
|
||||
|
||||
func (s *Client) NewGetTickersRequest() *GetTickersRequest {
|
||||
return &GetTickersRequest{client: s.Client}
|
||||
}
|
|
@ -0,0 +1,174 @@
|
|||
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -url /api/v2/spot/market/tickers -type GetTickersRequest -responseDataType []Ticker"; DO NOT EDIT.
|
||||
|
||||
package bitgetapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/c9s/bbgo/pkg/exchange/bitget/bitgetapi"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func (g *GetTickersRequest) Symbol(symbol string) *GetTickersRequest {
|
||||
g.symbol = &symbol
|
||||
return g
|
||||
}
|
||||
|
||||
// GetQueryParameters builds and checks the query parameters and returns url.Values
|
||||
func (g *GetTickersRequest) GetQueryParameters() (url.Values, error) {
|
||||
var params = map[string]interface{}{}
|
||||
// check symbol field -> json key symbol
|
||||
if g.symbol != nil {
|
||||
symbol := *g.symbol
|
||||
|
||||
// assign parameter of symbol
|
||||
params["symbol"] = symbol
|
||||
} else {
|
||||
}
|
||||
|
||||
query := url.Values{}
|
||||
for _k, _v := range params {
|
||||
query.Add(_k, fmt.Sprintf("%v", _v))
|
||||
}
|
||||
|
||||
return query, nil
|
||||
}
|
||||
|
||||
// GetParameters builds and checks the parameters and return the result in a map object
|
||||
func (g *GetTickersRequest) GetParameters() (map[string]interface{}, error) {
|
||||
var params = map[string]interface{}{}
|
||||
|
||||
return params, nil
|
||||
}
|
||||
|
||||
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
|
||||
func (g *GetTickersRequest) GetParametersQuery() (url.Values, error) {
|
||||
query := url.Values{}
|
||||
|
||||
params, err := g.GetParameters()
|
||||
if err != nil {
|
||||
return query, err
|
||||
}
|
||||
|
||||
for _k, _v := range params {
|
||||
if g.isVarSlice(_v) {
|
||||
g.iterateSlice(_v, func(it interface{}) {
|
||||
query.Add(_k+"[]", fmt.Sprintf("%v", it))
|
||||
})
|
||||
} else {
|
||||
query.Add(_k, fmt.Sprintf("%v", _v))
|
||||
}
|
||||
}
|
||||
|
||||
return query, nil
|
||||
}
|
||||
|
||||
// GetParametersJSON converts the parameters from GetParameters into the JSON format
|
||||
func (g *GetTickersRequest) GetParametersJSON() ([]byte, error) {
|
||||
params, err := g.GetParameters()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return json.Marshal(params)
|
||||
}
|
||||
|
||||
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
|
||||
func (g *GetTickersRequest) GetSlugParameters() (map[string]interface{}, error) {
|
||||
var params = map[string]interface{}{}
|
||||
|
||||
return params, nil
|
||||
}
|
||||
|
||||
func (g *GetTickersRequest) applySlugsToUrl(url string, slugs map[string]string) string {
|
||||
for _k, _v := range slugs {
|
||||
needleRE := regexp.MustCompile(":" + _k + "\\b")
|
||||
url = needleRE.ReplaceAllString(url, _v)
|
||||
}
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
func (g *GetTickersRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
|
||||
sliceValue := reflect.ValueOf(slice)
|
||||
for _i := 0; _i < sliceValue.Len(); _i++ {
|
||||
it := sliceValue.Index(_i).Interface()
|
||||
_f(it)
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GetTickersRequest) isVarSlice(_v interface{}) bool {
|
||||
rt := reflect.TypeOf(_v)
|
||||
switch rt.Kind() {
|
||||
case reflect.Slice:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (g *GetTickersRequest) GetSlugsMap() (map[string]string, error) {
|
||||
slugs := map[string]string{}
|
||||
params, err := g.GetSlugParameters()
|
||||
if err != nil {
|
||||
return slugs, nil
|
||||
}
|
||||
|
||||
for _k, _v := range params {
|
||||
slugs[_k] = fmt.Sprintf("%v", _v)
|
||||
}
|
||||
|
||||
return slugs, nil
|
||||
}
|
||||
|
||||
// GetPath returns the request path of the API
|
||||
func (g *GetTickersRequest) GetPath() string {
|
||||
return "/api/v2/spot/market/tickers"
|
||||
}
|
||||
|
||||
// Do generates the request object and send the request object to the API endpoint
|
||||
func (g *GetTickersRequest) Do(ctx context.Context) ([]Ticker, error) {
|
||||
|
||||
// no body params
|
||||
var params interface{}
|
||||
query, err := g.GetQueryParameters()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var apiURL string
|
||||
|
||||
apiURL = g.GetPath()
|
||||
|
||||
req, err := g.client.NewRequest(ctx, "GET", apiURL, query, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response, err := g.client.SendRequest(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var apiResponse bitgetapi.APIResponse
|
||||
if err := response.DecodeJSON(&apiResponse); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
type responseValidator interface {
|
||||
Validate() error
|
||||
}
|
||||
validator, ok := interface{}(apiResponse).(responseValidator)
|
||||
if ok {
|
||||
if err := validator.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
var data []Ticker
|
||||
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return data, nil
|
||||
}
|
|
@ -47,16 +47,16 @@ func toGlobalMarket(s v2.Symbol) types.Market {
|
|||
}
|
||||
}
|
||||
|
||||
func toGlobalTicker(ticker bitgetapi.Ticker) types.Ticker {
|
||||
func toGlobalTicker(ticker v2.Ticker) types.Ticker {
|
||||
return types.Ticker{
|
||||
Time: ticker.Ts.Time(),
|
||||
Volume: ticker.BaseVol,
|
||||
Last: ticker.Close,
|
||||
Open: ticker.OpenUtc0,
|
||||
Volume: ticker.BaseVolume,
|
||||
Last: ticker.LastPr,
|
||||
Open: ticker.Open,
|
||||
High: ticker.High24H,
|
||||
Low: ticker.Low24H,
|
||||
Buy: ticker.BuyOne,
|
||||
Sell: ticker.SellOne,
|
||||
Buy: ticker.BidPr,
|
||||
Sell: ticker.AskPr,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -100,39 +100,41 @@ func Test_toGlobalMarket(t *testing.T) {
|
|||
|
||||
func Test_toGlobalTicker(t *testing.T) {
|
||||
// sample:
|
||||
// {
|
||||
// "symbol": "BTCUSDT",
|
||||
// "high24h": "24175.65",
|
||||
// "low24h": "23677.75",
|
||||
// "close": "24014.11",
|
||||
// "quoteVol": "177689342.3025",
|
||||
// "baseVol": "7421.5009",
|
||||
// "usdtVol": "177689342.302407",
|
||||
// "ts": "1660704288118",
|
||||
// "buyOne": "24013.94",
|
||||
// "sellOne": "24014.06",
|
||||
// "bidSz": "0.0663",
|
||||
// "askSz": "0.0119",
|
||||
// "openUtc0": "23856.72",
|
||||
// "changeUtc":"0.00301",
|
||||
// "change":"0.00069"
|
||||
// }
|
||||
ticker := bitgetapi.Ticker{
|
||||
Symbol: "BTCUSDT",
|
||||
High24H: fixedpoint.NewFromFloat(24175.65),
|
||||
Low24H: fixedpoint.NewFromFloat(23677.75),
|
||||
Close: fixedpoint.NewFromFloat(24014.11),
|
||||
QuoteVol: fixedpoint.NewFromFloat(177689342.3025),
|
||||
BaseVol: fixedpoint.NewFromFloat(7421.5009),
|
||||
UsdtVol: fixedpoint.NewFromFloat(177689342.302407),
|
||||
Ts: types.NewMillisecondTimestampFromInt(1660704288118),
|
||||
BuyOne: fixedpoint.NewFromFloat(24013.94),
|
||||
SellOne: fixedpoint.NewFromFloat(24014.06),
|
||||
BidSz: fixedpoint.NewFromFloat(0.0663),
|
||||
AskSz: fixedpoint.NewFromFloat(0.0119),
|
||||
OpenUtc0: fixedpoint.NewFromFloat(23856.72),
|
||||
ChangeUtc: fixedpoint.NewFromFloat(0.00301),
|
||||
Change: fixedpoint.NewFromFloat(0.00069),
|
||||
//{
|
||||
// "open":"36465.96",
|
||||
// "symbol":"BTCUSDT",
|
||||
// "high24h":"37040.25",
|
||||
// "low24h":"36202.65",
|
||||
// "lastPr":"36684.42",
|
||||
// "quoteVolume":"311893591.2805",
|
||||
// "baseVolume":"8507.3684",
|
||||
// "usdtVolume":"311893591.280427",
|
||||
// "ts":"1699947106122",
|
||||
// "bidPr":"36684.49",
|
||||
// "askPr":"36684.51",
|
||||
// "bidSz":"0.3812",
|
||||
// "askSz":"0.0133",
|
||||
// "openUtc":"36465.96",
|
||||
// "changeUtc24h":"0.00599",
|
||||
// "change24h":"-0.00426"
|
||||
//}
|
||||
ticker := v2.Ticker{
|
||||
Symbol: "BTCUSDT",
|
||||
High24H: fixedpoint.NewFromFloat(24175.65),
|
||||
Low24H: fixedpoint.NewFromFloat(23677.75),
|
||||
LastPr: fixedpoint.NewFromFloat(24014.11),
|
||||
QuoteVolume: fixedpoint.NewFromFloat(177689342.3025),
|
||||
BaseVolume: fixedpoint.NewFromFloat(7421.5009),
|
||||
UsdtVolume: fixedpoint.NewFromFloat(177689342.302407),
|
||||
Ts: types.NewMillisecondTimestampFromInt(1660704288118),
|
||||
BidPr: fixedpoint.NewFromFloat(24013.94),
|
||||
AskPr: fixedpoint.NewFromFloat(24014.06),
|
||||
BidSz: fixedpoint.NewFromFloat(0.0663),
|
||||
AskSz: fixedpoint.NewFromFloat(0.0119),
|
||||
OpenUtc: fixedpoint.NewFromFloat(23856.72),
|
||||
ChangeUtc24H: fixedpoint.NewFromFloat(0.00301),
|
||||
Change24H: fixedpoint.NewFromFloat(0.00069),
|
||||
Open: fixedpoint.NewFromFloat(23856.72),
|
||||
}
|
||||
|
||||
assert.Equal(t, types.Ticker{
|
||||
|
|
|
@ -113,14 +113,17 @@ func (e *Exchange) QueryTicker(ctx context.Context, symbol string) (*types.Ticke
|
|||
return nil, fmt.Errorf("ticker rate limiter wait error: %w", err)
|
||||
}
|
||||
|
||||
req := e.client.NewGetTickerRequest()
|
||||
req := e.v2Client.NewGetTickersRequest()
|
||||
req.Symbol(symbol)
|
||||
resp, err := req.Do(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query ticker: %w", err)
|
||||
}
|
||||
if len(resp) != 1 {
|
||||
return nil, fmt.Errorf("unexpected length of query single symbol: %+v", resp)
|
||||
}
|
||||
|
||||
ticker := toGlobalTicker(*resp)
|
||||
ticker := toGlobalTicker(resp[1])
|
||||
return &ticker, nil
|
||||
}
|
||||
|
||||
|
@ -143,7 +146,7 @@ func (e *Exchange) QueryTickers(ctx context.Context, symbols ...string) (map[str
|
|||
return nil, fmt.Errorf("tickers rate limiter wait error: %w", err)
|
||||
}
|
||||
|
||||
resp, err := e.client.NewGetAllTickersRequest().Do(ctx)
|
||||
resp, err := e.v2Client.NewGetTickersRequest().Do(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query tickers: %w", err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user