2022-05-28 17:42:08 +00:00
|
|
|
package binance
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-02-05 04:11:36 +00:00
|
|
|
"fmt"
|
2022-05-28 17:42:08 +00:00
|
|
|
"time"
|
|
|
|
|
2024-02-05 04:11:36 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/exchange/binance/binanceapi"
|
2022-05-28 17:42:08 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2024-02-05 04:11:36 +00:00
|
|
|
type BorrowRepayType interface {
|
|
|
|
types.MarginLoan | types.MarginRepay
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryBorrowRepayHistory[T BorrowRepayType](e *Exchange, ctx context.Context, asset string, startTime, endTime *time.Time) ([]T, error) {
|
|
|
|
req := e.client2.NewGetMarginBorrowRepayHistoryRequest()
|
2022-05-28 17:42:08 +00:00
|
|
|
req.Asset(asset)
|
2022-05-29 03:52:25 +00:00
|
|
|
req.Size(100)
|
2022-05-28 17:42:08 +00:00
|
|
|
|
2024-02-05 04:11:36 +00:00
|
|
|
switch v := any(T{}); v.(type) {
|
|
|
|
case types.MarginLoan:
|
|
|
|
req.SetBorrowRepayType(binanceapi.BorrowRepayTypeBorrow)
|
|
|
|
case types.MarginRepay:
|
|
|
|
req.SetBorrowRepayType(binanceapi.BorrowRepayTypeRepay)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("T is other type")
|
|
|
|
}
|
|
|
|
|
2022-05-28 17:42:08 +00:00
|
|
|
if startTime != nil {
|
|
|
|
req.StartTime(*startTime)
|
2022-05-29 03:52:25 +00:00
|
|
|
|
|
|
|
// 6 months
|
|
|
|
if time.Since(*startTime) > time.Hour*24*30*6 {
|
|
|
|
req.Archived(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if startTime != nil && endTime != nil {
|
|
|
|
duration := endTime.Sub(*startTime)
|
|
|
|
if duration > time.Hour*24*30 {
|
|
|
|
t := startTime.Add(time.Hour * 24 * 30)
|
|
|
|
endTime = &t
|
|
|
|
}
|
2022-05-28 17:42:08 +00:00
|
|
|
}
|
2022-05-29 03:52:25 +00:00
|
|
|
|
2022-05-28 17:42:08 +00:00
|
|
|
if endTime != nil {
|
|
|
|
req.EndTime(*endTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.MarginSettings.IsIsolatedMargin {
|
|
|
|
req.IsolatedSymbol(e.MarginSettings.IsolatedMarginSymbol)
|
|
|
|
}
|
|
|
|
|
2022-05-29 03:52:25 +00:00
|
|
|
records, err := req.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-05 04:11:36 +00:00
|
|
|
var borrowRepay []T
|
2022-05-29 03:52:25 +00:00
|
|
|
for _, record := range records {
|
2024-02-05 04:11:36 +00:00
|
|
|
borrowRepay = append(borrowRepay, T{
|
|
|
|
Exchange: types.ExchangeBinance,
|
|
|
|
TransactionID: record.TxId,
|
|
|
|
Asset: record.Asset,
|
|
|
|
Principle: record.Principal,
|
|
|
|
Time: types.Time(record.Timestamp),
|
|
|
|
IsolatedSymbol: record.IsolatedSymbol,
|
|
|
|
})
|
2022-05-29 03:52:25 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 04:11:36 +00:00
|
|
|
return borrowRepay, nil
|
2022-05-29 03:52:25 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 04:11:36 +00:00
|
|
|
func (e *Exchange) QueryLoanHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]types.MarginLoan, error) {
|
|
|
|
return queryBorrowRepayHistory[types.MarginLoan](e, ctx, asset, startTime, endTime)
|
|
|
|
}
|
2022-05-29 03:52:25 +00:00
|
|
|
|
2024-02-05 04:11:36 +00:00
|
|
|
func (e *Exchange) QueryRepayHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]types.MarginRepay, error) {
|
|
|
|
return queryBorrowRepayHistory[types.MarginRepay](e, ctx, asset, startTime, endTime)
|
2022-05-29 03:52:25 +00:00
|
|
|
}
|
|
|
|
|
2022-05-30 07:55:49 +00:00
|
|
|
func (e *Exchange) QueryLiquidationHistory(ctx context.Context, startTime, endTime *time.Time) ([]types.MarginLiquidation, error) {
|
2022-05-28 17:42:08 +00:00
|
|
|
req := e.client2.NewGetMarginLiquidationHistoryRequest()
|
2022-05-30 07:55:49 +00:00
|
|
|
req.Size(100)
|
2022-05-28 17:42:08 +00:00
|
|
|
|
|
|
|
if startTime != nil {
|
|
|
|
req.StartTime(*startTime)
|
|
|
|
}
|
2022-05-30 07:55:49 +00:00
|
|
|
|
|
|
|
if startTime != nil && endTime != nil {
|
|
|
|
duration := endTime.Sub(*startTime)
|
|
|
|
if duration > time.Hour*24*30 {
|
|
|
|
t := startTime.Add(time.Hour * 24 * 30)
|
|
|
|
endTime = &t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 17:42:08 +00:00
|
|
|
if endTime != nil {
|
|
|
|
req.EndTime(*endTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.MarginSettings.IsIsolatedMargin {
|
|
|
|
req.IsolatedSymbol(e.MarginSettings.IsolatedMarginSymbol)
|
|
|
|
}
|
|
|
|
|
2022-05-30 07:55:49 +00:00
|
|
|
records, err := req.Do(ctx)
|
|
|
|
var liquidations []types.MarginLiquidation
|
|
|
|
for _, record := range records {
|
|
|
|
liquidations = append(liquidations, toGlobalLiquidation(record))
|
|
|
|
}
|
|
|
|
|
|
|
|
return liquidations, err
|
2022-05-28 17:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) QueryInterestHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]types.MarginInterest, error) {
|
|
|
|
req := e.client2.NewGetMarginInterestHistoryRequest()
|
|
|
|
req.Asset(asset)
|
2022-05-29 04:01:20 +00:00
|
|
|
req.Size(100)
|
2022-05-28 17:42:08 +00:00
|
|
|
|
|
|
|
if startTime != nil {
|
|
|
|
req.StartTime(*startTime)
|
2022-05-29 04:01:20 +00:00
|
|
|
|
|
|
|
// 6 months
|
|
|
|
if time.Since(*startTime) > time.Hour*24*30*6 {
|
|
|
|
req.Archived(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if startTime != nil && endTime != nil {
|
|
|
|
duration := endTime.Sub(*startTime)
|
|
|
|
if duration > time.Hour*24*30 {
|
|
|
|
t := startTime.Add(time.Hour * 24 * 30)
|
|
|
|
endTime = &t
|
|
|
|
}
|
2022-05-28 17:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if endTime != nil {
|
|
|
|
req.EndTime(*endTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.MarginSettings.IsIsolatedMargin {
|
|
|
|
req.IsolatedSymbol(e.MarginSettings.IsolatedMarginSymbol)
|
|
|
|
}
|
|
|
|
|
2022-05-29 04:01:20 +00:00
|
|
|
records, err := req.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var interests []types.MarginInterest
|
|
|
|
for _, record := range records {
|
|
|
|
interests = append(interests, toGlobalInterest(record))
|
|
|
|
}
|
|
|
|
|
|
|
|
return interests, err
|
|
|
|
}
|