mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 01:01:56 +00:00
pkg/exchange: add query tickers api
This commit is contained in:
parent
de8f24cb7b
commit
00d4805321
|
@ -46,3 +46,16 @@ func toGlobalMarket(s bitgetapi.Symbol) types.Market {
|
|||
MaxPrice: fixedpoint.Zero,
|
||||
}
|
||||
}
|
||||
|
||||
func toGlobalTicker(ticker bitgetapi.Ticker) types.Ticker {
|
||||
return types.Ticker{
|
||||
Time: ticker.Ts.Time(),
|
||||
Volume: ticker.BaseVol,
|
||||
Last: ticker.Close,
|
||||
Open: ticker.OpenUtc0,
|
||||
High: ticker.High24H,
|
||||
Low: ticker.Low24H,
|
||||
Buy: ticker.BuyOne,
|
||||
Sell: ticker.SellOne,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -94,3 +94,52 @@ func Test_toGlobalMarket(t *testing.T) {
|
|||
|
||||
assert.Equal(t, toGlobalMarket(inst), exp)
|
||||
}
|
||||
|
||||
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),
|
||||
}
|
||||
|
||||
assert.Equal(t, types.Ticker{
|
||||
Time: types.NewMillisecondTimestampFromInt(1660704288118).Time(),
|
||||
Volume: fixedpoint.NewFromFloat(7421.5009),
|
||||
Last: fixedpoint.NewFromFloat(24014.11),
|
||||
Open: fixedpoint.NewFromFloat(23856.72),
|
||||
High: fixedpoint.NewFromFloat(24175.65),
|
||||
Low: fixedpoint.NewFromFloat(23677.75),
|
||||
Buy: fixedpoint.NewFromFloat(24013.94),
|
||||
Sell: fixedpoint.NewFromFloat(24014.06),
|
||||
}, toGlobalTicker(ticker))
|
||||
}
|
||||
|
|
|
@ -27,6 +27,8 @@ var (
|
|||
queryAccountRateLimiter = rate.NewLimiter(rate.Every(time.Second/5), 5)
|
||||
// queryTickerRateLimiter has its own rate limit. https://bitgetlimited.github.io/apidoc/en/spot/#get-single-ticker
|
||||
queryTickerRateLimiter = rate.NewLimiter(rate.Every(time.Second/10), 5)
|
||||
// queryTickersRateLimiter has its own rate limit. https://bitgetlimited.github.io/apidoc/en/spot/#get-all-tickers
|
||||
queryTickersRateLimiter = rate.NewLimiter(rate.Every(time.Second/10), 5)
|
||||
)
|
||||
|
||||
type Exchange struct {
|
||||
|
@ -90,26 +92,44 @@ func (e *Exchange) QueryTicker(ctx context.Context, symbol string) (*types.Ticke
|
|||
|
||||
req := e.client.NewGetTickerRequest()
|
||||
req.Symbol(symbol)
|
||||
ticker, err := req.Do(ctx)
|
||||
resp, err := req.Do(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query ticker: %w", err)
|
||||
}
|
||||
|
||||
return &types.Ticker{
|
||||
Time: ticker.Ts.Time(),
|
||||
Volume: ticker.BaseVol,
|
||||
Last: ticker.Close,
|
||||
Open: ticker.OpenUtc0,
|
||||
High: ticker.High24H,
|
||||
Low: ticker.Low24H,
|
||||
Buy: ticker.BuyOne,
|
||||
Sell: ticker.SellOne,
|
||||
}, nil
|
||||
ticker := toGlobalTicker(*resp)
|
||||
return &ticker, nil
|
||||
}
|
||||
|
||||
func (e *Exchange) QueryTickers(ctx context.Context, symbol ...string) (map[string]types.Ticker, error) {
|
||||
// TODO implement me
|
||||
panic("implement me")
|
||||
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
|
||||
}
|
||||
|
||||
if err := queryTickersRateLimiter.Wait(ctx); err != nil {
|
||||
return nil, fmt.Errorf("tickers rate limiter wait error: %w", err)
|
||||
}
|
||||
|
||||
resp, err := e.client.NewGetAllTickersRequest().Do(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query tickers: %w", err)
|
||||
}
|
||||
|
||||
for _, s := range resp {
|
||||
tickers[s.Symbol] = toGlobalTicker(s)
|
||||
}
|
||||
|
||||
return tickers, nil
|
||||
}
|
||||
|
||||
func (e *Exchange) QueryKLines(ctx context.Context, symbol string, interval types.Interval, options types.KLineQueryOptions) ([]types.KLine, error) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user