diff --git a/pkg/exchange/batch/margin_interest.go b/pkg/exchange/batch/margin_interest.go new file mode 100644 index 000000000..4332db1b3 --- /dev/null +++ b/pkg/exchange/batch/margin_interest.go @@ -0,0 +1,35 @@ +package batch + +import ( + "context" + "time" + + "golang.org/x/time/rate" + + "github.com/c9s/bbgo/pkg/types" +) + +type MarginInterestBatchQuery struct { + types.MarginHistory +} + +func (e *MarginInterestBatchQuery) Query(ctx context.Context, asset string, startTime, endTime time.Time) (c chan types.MarginInterest, errC chan error) { + query := &AsyncTimeRangedBatchQuery{ + Type: types.MarginInterest{}, + Limiter: rate.NewLimiter(rate.Every(5*time.Second), 2), + Q: func(startTime, endTime time.Time) (interface{}, error) { + return e.QueryInterestHistory(ctx, asset, &startTime, &endTime) + }, + T: func(obj interface{}) time.Time { + return time.Time(obj.(types.MarginInterest).Time) + }, + ID: func(obj interface{}) string { + interest := obj.(types.MarginInterest) + 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/batch/margin_loan.go b/pkg/exchange/batch/margin_loan.go new file mode 100644 index 000000000..3cfa5b57b --- /dev/null +++ b/pkg/exchange/batch/margin_loan.go @@ -0,0 +1,36 @@ +package batch + +import ( + "context" + "strconv" + "time" + + "golang.org/x/time/rate" + + "github.com/c9s/bbgo/pkg/types" +) + +type MarginLoanBatchQuery struct { + types.MarginHistory +} + +func (e *MarginLoanBatchQuery) Query(ctx context.Context, asset string, startTime, endTime time.Time) (c chan types.MarginLoan, errC chan error) { + query := &AsyncTimeRangedBatchQuery{ + Type: types.MarginLoan{}, + Limiter: rate.NewLimiter(rate.Every(5*time.Second), 2), + Q: func(startTime, endTime time.Time) (interface{}, error) { + return e.QueryLoanHistory(ctx, asset, &startTime, &endTime) + }, + T: func(obj interface{}) time.Time { + return time.Time(obj.(types.MarginRepay).Time) + }, + ID: func(obj interface{}) string { + loan := obj.(types.MarginLoan) + return strconv.FormatUint(loan.TransactionID, 10) + }, + } + + c = make(chan types.MarginLoan, 100) + errC = query.Query(ctx, c, startTime, endTime) + return c, errC +} diff --git a/pkg/exchange/batch/margin_repay.go b/pkg/exchange/batch/margin_repay.go new file mode 100644 index 000000000..60e2bdc69 --- /dev/null +++ b/pkg/exchange/batch/margin_repay.go @@ -0,0 +1,36 @@ +package batch + +import ( + "context" + "strconv" + "time" + + "golang.org/x/time/rate" + + "github.com/c9s/bbgo/pkg/types" +) + +type MarginRepayBatchQuery struct { + types.MarginHistory +} + +func (e *MarginRepayBatchQuery) Query(ctx context.Context, asset string, startTime, endTime time.Time) (c chan types.MarginRepay, errC chan error) { + query := &AsyncTimeRangedBatchQuery{ + Type: types.MarginRepay{}, + Limiter: rate.NewLimiter(rate.Every(5*time.Second), 2), + Q: func(startTime, endTime time.Time) (interface{}, error) { + return e.QueryRepayHistory(ctx, asset, &startTime, &endTime) + }, + T: func(obj interface{}) time.Time { + return time.Time(obj.(types.MarginRepay).Time) + }, + ID: func(obj interface{}) string { + loan := obj.(types.MarginRepay) + return strconv.FormatUint(loan.TransactionID, 10) + }, + } + + c = make(chan types.MarginRepay, 100) + errC = query.Query(ctx, c, startTime, endTime) + return c, errC +}