From 265b69a0ee1ec3bafc781e4219c0405bd4f0a7cd Mon Sep 17 00:00:00 2001 From: c9s Date: Sun, 26 Mar 2023 00:53:29 +0800 Subject: [PATCH] batch: add funding fee batch query --- pkg/exchange/batch/funding_fee.go | 41 +++++++++++++++++++++++++++++++ pkg/exchange/binance/futures.go | 12 ++++++--- 2 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 pkg/exchange/batch/funding_fee.go diff --git a/pkg/exchange/batch/funding_fee.go b/pkg/exchange/batch/funding_fee.go new file mode 100644 index 000000000..a7c5ce3ee --- /dev/null +++ b/pkg/exchange/batch/funding_fee.go @@ -0,0 +1,41 @@ +package batch + +import ( + "context" + "time" + + "golang.org/x/time/rate" + + "github.com/c9s/bbgo/pkg/exchange/binance/binanceapi" + "github.com/c9s/bbgo/pkg/types" +) + +type BinanceFuturesIncomeHistoryService interface { + QueryFuturesIncomeHistory(ctx context.Context, symbol string, incomeType binanceapi.FuturesIncomeType, startTime, endTime *time.Time) ([]binanceapi.FuturesIncome, error) +} + +type FuturesFundingFeeBatchQuery struct { + BinanceFuturesIncomeHistoryService +} + +func (e *FuturesFundingFeeBatchQuery) Query(ctx context.Context, symbol string, startTime, endTime time.Time) (c chan types.MarginInterest, errC chan error) { + query := &AsyncTimeRangedBatchQuery{ + Type: types.MarginInterest{}, + Limiter: rate.NewLimiter(rate.Every(3*time.Second), 1), + JumpIfEmpty: time.Hour * 24 * 30, + Q: func(startTime, endTime time.Time) (interface{}, error) { + return e.QueryFuturesIncomeHistory(ctx, symbol, binanceapi.FuturesIncomeFundingFee, &startTime, &endTime) + }, + T: func(obj interface{}) time.Time { + return time.Time(obj.(binanceapi.FuturesIncome).Time) + }, + ID: func(obj interface{}) string { + interest := obj.(binanceapi.FuturesIncome) + return interest.Time.String() + }, + } + + c = make(chan types.MarginInterest, 100) + errC = query.Query(ctx, c, startTime, endTime) + return c, errC +} diff --git a/pkg/exchange/binance/futures.go b/pkg/exchange/binance/futures.go index 3374a79d5..224a18599 100644 --- a/pkg/exchange/binance/futures.go +++ b/pkg/exchange/binance/futures.go @@ -369,12 +369,18 @@ func (e *Exchange) queryFuturesDepth(ctx context.Context, symbol string) (snapsh // QueryFuturesIncomeHistory queries the income history on the binance futures account // This is more binance futures specific API, the convert function is not designed yet. // TODO: consider other futures platforms and design the common data structure for this -func (e *Exchange) QueryFuturesIncomeHistory(ctx context.Context, symbol string, incomeType binanceapi.FuturesIncomeType, startTime, endTime time.Time) ([]binanceapi.FuturesIncome, error) { +func (e *Exchange) QueryFuturesIncomeHistory(ctx context.Context, symbol string, incomeType binanceapi.FuturesIncomeType, startTime, endTime *time.Time) ([]binanceapi.FuturesIncome, error) { req := e.futuresClient2.NewFuturesGetIncomeHistoryRequest() req.Symbol(symbol) req.IncomeType(incomeType) - req.StartTime(startTime) - req.EndTime(endTime) + if startTime != nil { + req.StartTime(*startTime) + } + + if endTime != nil { + req.EndTime(*endTime) + } + resp, err := req.Do(ctx) return resp, err }