diff --git a/pkg/exchange/binance/convert_margin.go b/pkg/exchange/binance/convert_margin.go index 40aa56e4e..d74494e9d 100644 --- a/pkg/exchange/binance/convert_margin.go +++ b/pkg/exchange/binance/convert_margin.go @@ -8,8 +8,8 @@ import ( "github.com/c9s/bbgo/pkg/types" ) -func toGlobalLoan(record binanceapi.MarginLoanRecord) types.MarginLoanRecord { - return types.MarginLoanRecord{ +func toGlobalLoan(record binanceapi.MarginLoanRecord) types.MarginLoan { + return types.MarginLoan{ TransactionID: uint64(record.TxId), Asset: record.Asset, Principle: record.Principal, @@ -18,8 +18,8 @@ func toGlobalLoan(record binanceapi.MarginLoanRecord) types.MarginLoanRecord { } } -func toGlobalRepay(record binanceapi.MarginRepayRecord) types.MarginRepayRecord { - return types.MarginRepayRecord{ +func toGlobalRepay(record binanceapi.MarginRepayRecord) types.MarginRepay { + return types.MarginRepay{ TransactionID: record.TxId, Asset: record.Asset, Principle: record.Principal, @@ -37,7 +37,21 @@ func toGlobalInterest(record binanceapi.MarginInterest) types.MarginInterest { IsolatedSymbol: record.IsolatedSymbol, Time: types.Time(record.InterestAccuredTime), } +} +func toGlobalLiquidation(record binanceapi.MarginLiquidationRecord) types.MarginLiquidation { + return types.MarginLiquidation{ + AveragePrice: record.AveragePrice, + ExecutedQuantity: record.ExecutedQuantity, + OrderID: record.OrderId, + Price: record.Price, + Quantity: record.Quantity, + Side: toGlobalSideType(record.Side), + Symbol: record.Symbol, + TimeInForce: types.TimeInForce(record.TimeInForce), + IsIsolated: record.IsIsolated, + UpdatedTime: types.Time(record.UpdatedTime), + } } func toGlobalIsolatedUserAsset(userAsset binance.IsolatedUserAsset) types.IsolatedUserAsset { diff --git a/pkg/exchange/binance/margin_history.go b/pkg/exchange/binance/margin_history.go index 59361fab2..5408e04ba 100644 --- a/pkg/exchange/binance/margin_history.go +++ b/pkg/exchange/binance/margin_history.go @@ -7,7 +7,7 @@ import ( "github.com/c9s/bbgo/pkg/types" ) -func (e *Exchange) QueryLoanHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]types.MarginLoanRecord, error) { +func (e *Exchange) QueryLoanHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]types.MarginLoan, error) { req := e.client2.NewGetMarginLoanHistoryRequest() req.Asset(asset) req.Size(100) @@ -42,7 +42,7 @@ func (e *Exchange) QueryLoanHistory(ctx context.Context, asset string, startTime return nil, err } - var loans []types.MarginLoanRecord + var loans []types.MarginLoan for _, record := range records { loans = append(loans, toGlobalLoan(record)) } @@ -50,7 +50,7 @@ func (e *Exchange) QueryLoanHistory(ctx context.Context, asset string, startTime return loans, err } -func (e *Exchange) QueryRepayHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]types.MarginRepayRecord, error) { +func (e *Exchange) QueryRepayHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]types.MarginRepay, error) { req := e.client2.NewGetMarginRepayHistoryRequest() req.Asset(asset) req.Size(100) @@ -82,7 +82,7 @@ func (e *Exchange) QueryRepayHistory(ctx context.Context, asset string, startTim records, err := req.Do(ctx) - var repays []types.MarginRepayRecord + var repays []types.MarginRepay for _, record := range records { repays = append(repays, toGlobalRepay(record)) } @@ -90,12 +90,22 @@ func (e *Exchange) QueryRepayHistory(ctx context.Context, asset string, startTim return repays, err } -func (e *Exchange) QueryLiquidationHistory(ctx context.Context, startTime, endTime *time.Time) ([]types.MarginLiquidationRecord, error) { +func (e *Exchange) QueryLiquidationHistory(ctx context.Context, startTime, endTime *time.Time) ([]types.MarginLiquidation, error) { req := e.client2.NewGetMarginLiquidationHistoryRequest() + req.Size(100) if startTime != nil { req.StartTime(*startTime) } + + if startTime != nil && endTime != nil { + duration := endTime.Sub(*startTime) + if duration > time.Hour*24*30 { + t := startTime.Add(time.Hour * 24 * 30) + endTime = &t + } + } + if endTime != nil { req.EndTime(*endTime) } @@ -104,8 +114,13 @@ func (e *Exchange) QueryLiquidationHistory(ctx context.Context, startTime, endTi req.IsolatedSymbol(e.MarginSettings.IsolatedMarginSymbol) } - _, err := req.Do(ctx) - return nil, err + records, err := req.Do(ctx) + var liquidations []types.MarginLiquidation + for _, record := range records { + liquidations = append(liquidations, toGlobalLiquidation(record)) + } + + return liquidations, err } func (e *Exchange) QueryInterestHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]types.MarginInterest, error) { @@ -150,4 +165,3 @@ func (e *Exchange) QueryInterestHistory(ctx context.Context, asset string, start return interests, err } - diff --git a/pkg/exchange/binance/parse.go b/pkg/exchange/binance/parse.go index 36edc3dac..4d245fff0 100644 --- a/pkg/exchange/binance/parse.go +++ b/pkg/exchange/binance/parse.go @@ -51,7 +51,7 @@ executionReport "O": 1499405658657, // Order creation time "Z": "0.00000000", // Cumulative quote asset transacted quantity "Y": "0.00000000", // Last quote asset transacted quantity (i.e. lastPrice * lastQty) - "Q": "0.00000000" // Quote Order Qty + "Q": "0.00000000" // Quote Order Quantity } */ type ExecutionReportEvent struct { diff --git a/pkg/exchange/binance/parse_test.go b/pkg/exchange/binance/parse_test.go index ad9d36e37..0d83664ce 100644 --- a/pkg/exchange/binance/parse_test.go +++ b/pkg/exchange/binance/parse_test.go @@ -168,7 +168,7 @@ func TestParseOrderUpdate(t *testing.T) { "O": 1499405658657, // Order creation time "Z": "0.1", // Cumulative quote asset transacted quantity "Y": "0.00000000", // Last quote asset transacted quantity (i.e. lastPrice * lastQty) - "Q": "2.0" // Quote Order Qty + "Q": "2.0" // Quote Order Quantity }` payload = jsCommentTrimmer.ReplaceAllLiteralString(payload, "") diff --git a/pkg/types/margin.go b/pkg/types/margin.go index 3739ad2c8..7230f5afe 100644 --- a/pkg/types/margin.go +++ b/pkg/types/margin.go @@ -68,7 +68,7 @@ type MarginInterest struct { Time Time `json:"time" db:"time"` } -type MarginLoanRecord struct { +type MarginLoan struct { TransactionID uint64 `json:"transactionID" db:"transaction_id"` Asset string `json:"asset" db:"asset"` Principle fixedpoint.Value `json:"principle" db:"principle"` @@ -76,7 +76,7 @@ type MarginLoanRecord struct { IsolatedSymbol string `json:"isolatedSymbol" db:"isolated_symbol"` } -type MarginRepayRecord struct { +type MarginRepay struct { TransactionID uint64 `json:"transactionID" db:"transaction_id"` Asset string `json:"asset" db:"asset"` Principle fixedpoint.Value `json:"principle" db:"principle"` @@ -84,12 +84,12 @@ type MarginRepayRecord struct { IsolatedSymbol string `json:"isolatedSymbol" db:"isolated_symbol"` } -type MarginLiquidationRecord struct { +type MarginLiquidation struct { AveragePrice fixedpoint.Value `json:"avgPrice"` ExecutedQuantity fixedpoint.Value `json:"executedQty"` - OrderId uint64 `json:"orderId"` + OrderID uint64 `json:"orderId"` Price fixedpoint.Value `json:"price"` - Qty fixedpoint.Value `json:"qty"` + Quantity fixedpoint.Value `json:"qty"` Side SideType `json:"side"` Symbol string `json:"symbol"` TimeInForce TimeInForce `json:"timeInForce"` @@ -99,9 +99,9 @@ type MarginLiquidationRecord struct { // MarginHistory provides the service of querying loan history and repay history type MarginHistory interface { - QueryLoanHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]MarginLoanRecord, error) - QueryRepayHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]MarginRepayRecord, error) - QueryLiquidationHistory(ctx context.Context, startTime, endTime *time.Time) ([]MarginLiquidationRecord, error) + QueryLoanHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]MarginLoan, error) + QueryRepayHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]MarginRepay, error) + QueryLiquidationHistory(ctx context.Context, startTime, endTime *time.Time) ([]MarginLiquidation, error) QueryInterestHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]MarginInterest, error) }