binance: refactor query methods

This commit is contained in:
c9s 2022-05-29 01:21:43 +08:00
parent 4c30fce917
commit f58f44ffd8
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -1284,8 +1284,7 @@ func (e *Exchange) QueryKLines(ctx context.Context, symbol string, interval type
return kLines, nil
}
func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) (trades []types.Trade, err error) {
if e.IsMargin {
func (e *Exchange) queryMarginTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) (trades []types.Trade, err error) {
var remoteTrades []*binance.TradeV3
req := e.client.NewListMarginTradesService().
IsIsolated(e.IsIsolatedMargin).
@ -1330,9 +1329,11 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type
}
trades = types.SortTradesAscending(trades)
return trades, nil
} else if e.IsFutures {
}
func (e *Exchange) queryFuturesTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) (trades []types.Trade, err error) {
var remoteTrades []*futures.AccountTrade
req := e.futuresClient.NewListAccountTradeService().
Symbol(symbol)
@ -1378,7 +1379,9 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type
trades = types.SortTradesAscending(trades)
return trades, nil
} else {
}
func (e *Exchange) querySpotTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) (trades []types.Trade, err error) {
var remoteTrades []*binance.TradeV3
req := e.client.NewListTradesService().
Symbol(symbol)
@ -1423,6 +1426,15 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type
trades = types.SortTradesAscending(trades)
return trades, nil
}
func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) (trades []types.Trade, err error) {
if e.IsMargin {
return e.queryMarginTrades(ctx, symbol, options)
} else if e.IsFutures {
return e.queryFuturesTrades(ctx, symbol, options)
} else {
return e.querySpotTrades(ctx, symbol, options)
}
}
@ -1480,37 +1492,10 @@ func (e *Exchange) QueryDepth(ctx context.Context, symbol string) (snapshot type
return snapshot, finalUpdateID, nil
}
func (e *Exchange) BatchQueryKLines(ctx context.Context, symbol string, interval types.Interval, startTime, endTime time.Time) ([]types.KLine, error) {
var allKLines []types.KLine
for startTime.Before(endTime) {
klines, err := e.QueryKLines(ctx, symbol, interval, types.KLineQueryOptions{
StartTime: &startTime,
Limit: 1000,
})
if err != nil {
return nil, err
}
for _, kline := range klines {
if kline.EndTime.After(endTime) {
return allKLines, nil
}
allKLines = append(allKLines, kline)
startTime = kline.EndTime.Time()
}
}
return allKLines, nil
}
// QueryPremiumIndex is only for futures
func (e *Exchange) QueryPremiumIndex(ctx context.Context, symbol string) (*types.PremiumIndex, error) {
futuresClient := binance.NewFuturesClient(e.key, e.secret)
// when symbol is set, only one index will be returned.
indexes, err := futuresClient.NewPremiumIndexService().Symbol(symbol).Do(ctx)
indexes, err := e.futuresClient.NewPremiumIndexService().Symbol(symbol).Do(ctx)
if err != nil {
return nil, err
}
@ -1519,8 +1504,7 @@ func (e *Exchange) QueryPremiumIndex(ctx context.Context, symbol string) (*types
}
func (e *Exchange) QueryFundingRateHistory(ctx context.Context, symbol string) (*types.FundingRate, error) {
futuresClient := binance.NewFuturesClient(e.key, e.secret)
rates, err := futuresClient.NewFundingRateService().
rates, err := e.futuresClient.NewFundingRateService().
Symbol(symbol).
Limit(1).
Do(ctx)
@ -1546,10 +1530,8 @@ func (e *Exchange) QueryFundingRateHistory(ctx context.Context, symbol string) (
}
func (e *Exchange) QueryPositionRisk(ctx context.Context, symbol string) (*types.PositionRisk, error) {
futuresClient := binance.NewFuturesClient(e.key, e.secret)
// when symbol is set, only one position risk will be returned.
risks, err := futuresClient.NewGetPositionRiskService().Symbol(symbol).Do(ctx)
risks, err := e.futuresClient.NewGetPositionRiskService().Symbol(symbol).Do(ctx)
if err != nil {
return nil, err
}