Skip to content

Commit

Permalink
Binance: update orderbook and ticker rate limits (thrasher-corp#1697)
Browse files Browse the repository at this point in the history
* updates some rate limits

* WOAH FRIDAY FAILURES

* remove secret code, DONT LOOK

* types.Number, rv ob num, fr changes

* ever so slight neaten

* Remove FundingRateInfo for CMF only

* revert to using the funding rate intervals
  • Loading branch information
gloriousCode authored Nov 13, 2024
1 parent 8f9ebcb commit 85ecd0d
Show file tree
Hide file tree
Showing 10 changed files with 61,804 additions and 24,642 deletions.
45 changes: 34 additions & 11 deletions exchanges/binance/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ func (b *Binance) GetOrderBook(ctx context.Context, obd OrderBookDataRequestPara
}
params.Set("symbol", symbol)
params.Set("limit", strconv.Itoa(obd.Limit))

var resp OrderBookData
if err := b.SendHTTPRequest(ctx,
exchange.RestSpotSupplementary,
Expand Down Expand Up @@ -491,29 +490,53 @@ func (b *Binance) GetAveragePrice(ctx context.Context, symbol currency.Pair) (Av
// GetPriceChangeStats returns price change statistics for the last 24 hours
//
// symbol: string of currency pair
func (b *Binance) GetPriceChangeStats(ctx context.Context, symbol currency.Pair) (PriceChangeStats, error) {
func (b *Binance) GetPriceChangeStats(ctx context.Context, symbol currency.Pair) (*PriceChangeStats, error) {
resp := PriceChangeStats{}
params := url.Values{}
rateLimit := spotPriceChangeAllRate
rateLimit := spotTickerAllRate
if !symbol.IsEmpty() {
rateLimit = spotDefaultRate
rateLimit = spotTicker1Rate
symbolValue, err := b.FormatSymbol(symbol, asset.Spot)
if err != nil {
return resp, err
return nil, err
}
params.Set("symbol", symbolValue)
}
path := priceChange + "?" + params.Encode()

return resp, b.SendHTTPRequest(ctx,
exchange.RestSpotSupplementary, path, rateLimit, &resp)
return &resp, b.SendHTTPRequest(ctx, exchange.RestSpotSupplementary, path, rateLimit, &resp)
}

// GetTickers returns the ticker data for the last 24 hrs
func (b *Binance) GetTickers(ctx context.Context) ([]PriceChangeStats, error) {
func (b *Binance) GetTickers(ctx context.Context, symbols ...currency.Pair) ([]PriceChangeStats, error) {
var resp []PriceChangeStats
return resp, b.SendHTTPRequest(ctx,
exchange.RestSpotSupplementary, priceChange, spotPriceChangeAllRate, &resp)
symbolLength := len(symbols)
params := url.Values{}
var rl request.EndpointLimit
switch {
case symbolLength == 1:
rl = spotTicker1Rate
case symbolLength > 1 && symbolLength <= 20:
rl = spotTicker20Rate
case symbolLength > 20 && symbolLength <= 100:
rl = spotTicker100Rate
case symbolLength > 100, symbolLength == 0:
rl = spotTickerAllRate
}
path := priceChange
if symbolLength > 0 {
symbolValues := make([]string, symbolLength)
for i := range symbols {
symbolValue, err := b.FormatSymbol(symbols[i], asset.Spot)
if err != nil {
return resp, err
}
symbolValues[i] = "\"" + symbolValue + "\""
}
params.Set("symbols", "["+strings.Join(symbolValues, ",")+"]")
path += "?" + params.Encode()
}
return resp, b.SendHTTPRequest(ctx, exchange.RestSpotSupplementary, path, rl, &resp)
}

// GetLatestSpotPrice returns latest spot price of symbol
Expand All @@ -522,7 +545,7 @@ func (b *Binance) GetTickers(ctx context.Context) ([]PriceChangeStats, error) {
func (b *Binance) GetLatestSpotPrice(ctx context.Context, symbol currency.Pair) (SymbolPrice, error) {
resp := SymbolPrice{}
params := url.Values{}
rateLimit := spotSymbolPriceAllRate
rateLimit := spotTickerAllRate
if !symbol.IsEmpty() {
rateLimit = spotDefaultRate
symbolValue, err := b.FormatSymbol(symbol, asset.Spot)
Expand Down
6 changes: 2 additions & 4 deletions exchanges/binance/binance_cfutures.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,14 @@ func (b *Binance) GetFuturesOrderbook(ctx context.Context, symbol currency.Pair,
params.Set("limit", strconv.FormatInt(limit, 10))
}

rateBudget := cFuturesDefaultRate
rateBudget := cFuturesOrderbook1000Rate
switch {
case limit == 5, limit == 10, limit == 20, limit == 50:
rateBudget = cFuturesOrderbook50Rate
case limit >= 100 && limit < 500:
rateBudget = cFuturesOrderbook100Rate
case limit >= 500 && limit < 1000:
case limit == 0, limit >= 500 && limit < 1000:
rateBudget = cFuturesOrderbook500Rate
case limit == 1000:
rateBudget = cFuturesOrderbook1000Rate
}

var data OrderbookData
Expand Down
13 changes: 8 additions & 5 deletions exchanges/binance/binance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1192,11 +1192,14 @@ func TestGetPriceChangeStats(t *testing.T) {

func TestGetTickers(t *testing.T) {
t.Parallel()

_, err := b.GetTickers(context.Background())
if err != nil {
t.Error("Binance TestGetTickers error", err)
}
require.NoError(t, err)

resp, err := b.GetTickers(context.Background(),
currency.NewPair(currency.BTC, currency.USDT),
currency.NewPair(currency.ETH, currency.USDT))
require.NoError(t, err)
require.Len(t, resp, 2)
}

func TestGetLatestSpotPrice(t *testing.T) {
Expand Down Expand Up @@ -2819,7 +2822,7 @@ func TestUpdateOrderExecutionLimits(t *testing.T) {
}
}

func TestGetFundingRates(t *testing.T) {
func TestGetHistoricalFundingRates(t *testing.T) {
t.Parallel()
s, e := getTime()
_, err := b.GetHistoricalFundingRates(context.Background(), &fundingrate.HistoricalRatesRequest{
Expand Down
44 changes: 23 additions & 21 deletions exchanges/binance/binance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ type IndexMarkPrice struct {
IndexPrice types.Number `json:"indexPrice"`
EstimatedSettlePrice types.Number `json:"estimatedSettlePrice"`
LastFundingRate types.Number `json:"lastFundingRate"`
NextFundingTime int64 `json:"nextFundingTime"`
Time int64 `json:"time"`
NextFundingTime types.Time `json:"nextFundingTime"`
Time types.Time `json:"time"`
}

// CandleStick holds kline data
Expand All @@ -327,25 +327,27 @@ type AveragePrice struct {

// PriceChangeStats contains statistics for the last 24 hours trade
type PriceChangeStats struct {
Symbol string `json:"symbol"`
PriceChange float64 `json:"priceChange,string"`
PriceChangePercent float64 `json:"priceChangePercent,string"`
WeightedAvgPrice float64 `json:"weightedAvgPrice,string"`
PrevClosePrice float64 `json:"prevClosePrice,string"`
LastPrice float64 `json:"lastPrice,string"`
LastQty float64 `json:"lastQty,string"`
BidPrice float64 `json:"bidPrice,string"`
AskPrice float64 `json:"askPrice,string"`
OpenPrice float64 `json:"openPrice,string"`
HighPrice float64 `json:"highPrice,string"`
LowPrice float64 `json:"lowPrice,string"`
Volume float64 `json:"volume,string"`
QuoteVolume float64 `json:"quoteVolume,string"`
OpenTime time.Time `json:"openTime"`
CloseTime time.Time `json:"closeTime"`
FirstID int64 `json:"firstId"`
LastID int64 `json:"lastId"`
Count int64 `json:"count"`
Symbol string `json:"symbol"`
PriceChange types.Number `json:"priceChange"`
PriceChangePercent types.Number `json:"priceChangePercent"`
WeightedAvgPrice types.Number `json:"weightedAvgPrice"`
PrevClosePrice types.Number `json:"prevClosePrice"`
LastPrice types.Number `json:"lastPrice"`
LastQty types.Number `json:"lastQty"`
BidPrice types.Number `json:"bidPrice"`
AskPrice types.Number `json:"askPrice"`
BidQuantity types.Number `json:"bidQty"`
AskQuantity types.Number `json:"askQty"`
OpenPrice types.Number `json:"openPrice"`
HighPrice types.Number `json:"highPrice"`
LowPrice types.Number `json:"lowPrice"`
Volume types.Number `json:"volume"`
QuoteVolume types.Number `json:"quoteVolume"`
OpenTime time.Time `json:"openTime"`
CloseTime time.Time `json:"closeTime"`
FirstID int64 `json:"firstId"`
LastID int64 `json:"lastId"`
Count int64 `json:"count"`
}

// SymbolPrice holds basic symbol price
Expand Down
6 changes: 2 additions & 4 deletions exchanges/binance/binance_ufutures.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,14 @@ func (b *Binance) UFuturesOrderbook(ctx context.Context, symbol currency.Pair, l
params.Set("limit", strLimit)
}

rateBudget := uFuturesDefaultRate
rateBudget := uFuturesOrderbook1000Rate
switch {
case limit == 5, limit == 10, limit == 20, limit == 50:
rateBudget = uFuturesOrderbook50Rate
case limit >= 100 && limit < 500:
rateBudget = uFuturesOrderbook100Rate
case limit >= 500 && limit < 1000:
case limit == 0, limit >= 500 && limit < 1000:
rateBudget = uFuturesOrderbook500Rate
case limit == 1000:
rateBudget = uFuturesOrderbook1000Rate
}

var data OrderbookData
Expand Down
Loading

0 comments on commit 85ecd0d

Please sign in to comment.