xfunding: fix transferOut, and de-leverage the trade amount from the caller

This commit is contained in:
c9s 2023-06-16 17:26:14 +08:00
parent e82341b2bd
commit 2813ede7ed
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 7 additions and 5 deletions

View File

@ -405,8 +405,11 @@ func (s *Strategy) CrossRun(ctx context.Context, orderExecutionRouter bbgo.Order
switch s.getPositionState() {
case PositionClosing:
// de-leverage and get the collateral base quantity for transfer
quantity := trade.Quantity.Div(s.Leverage)
if err := backoff.RetryGeneral(ctx, func() error {
return s.transferOut(ctx, s.binanceSpot, s.spotMarket.BaseCurrency, trade.Quantity)
return s.transferOut(ctx, s.binanceSpot, s.spotMarket.BaseCurrency, quantity)
}); err != nil {
log.WithError(err).Errorf("spot-to-futures transfer in retry failed")
return

View File

@ -41,15 +41,12 @@ func (s *Strategy) resetTransfer(ctx context.Context, ex FuturesTransfer, asset
return nil
}
func (s *Strategy) transferOut(ctx context.Context, ex FuturesTransfer, asset string, tradeQuantity fixedpoint.Value) error {
func (s *Strategy) transferOut(ctx context.Context, ex FuturesTransfer, asset string, quantity fixedpoint.Value) error {
// if transfer done
if s.State.TotalBaseTransfer.IsZero() {
return nil
}
// de-leverage and get the collateral base quantity for transfer
quantity := tradeQuantity.Div(s.Leverage)
balances, err := s.futuresSession.Exchange.QueryAccountBalances(ctx)
if err != nil {
log.Infof("balance query error, adding to pending base transfer: %s %s + %s", quantity.String(), asset, s.State.PendingBaseTransfer.String())
@ -64,6 +61,8 @@ func (s *Strategy) transferOut(ctx context.Context, ex FuturesTransfer, asset st
return fmt.Errorf("%s balance not found", asset)
}
log.Infof("found futures balance: %+v", b)
// add the previous pending base transfer and the current trade quantity
amount := s.State.PendingBaseTransfer.Add(quantity)