xfunding: refactor transferOut with trade quantity

This commit is contained in:
c9s 2023-03-26 17:51:08 +08:00
parent 088a36a169
commit bc6ee59add
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 10 additions and 16 deletions

View File

@ -400,7 +400,7 @@ func (s *Strategy) CrossRun(ctx context.Context, orderExecutionRouter bbgo.Order
switch s.getPositionState() { switch s.getPositionState() {
case PositionClosing: case PositionClosing:
if err := backoff.RetryGeneral(ctx, func() error { if err := backoff.RetryGeneral(ctx, func() error {
return s.transferOut(ctx, s.binanceSpot, s.spotMarket.BaseCurrency, trade) return s.transferOut(ctx, s.binanceSpot, s.spotMarket.BaseCurrency, trade.Quantity)
}); err != nil { }); err != nil {
log.WithError(err).Errorf("spot-to-futures transfer in retry failed") log.WithError(err).Errorf("spot-to-futures transfer in retry failed")
return return

View File

@ -13,33 +13,27 @@ type FuturesTransfer interface {
QueryAccountBalances(ctx context.Context) (types.BalanceMap, error) QueryAccountBalances(ctx context.Context) (types.BalanceMap, error)
} }
func (s *Strategy) transferOut(ctx context.Context, ex FuturesTransfer, currency string, trade types.Trade) error { func (s *Strategy) transferOut(ctx context.Context, ex FuturesTransfer, asset string, tradeQuantity fixedpoint.Value) error {
// base asset needs BUY trades
if trade.Side != types.SideTypeBuy {
return nil
}
// if transfer done // if transfer done
if s.State.TotalBaseTransfer.IsZero() { if s.State.TotalBaseTransfer.IsZero() {
return nil return nil
} }
// de-leverage and get the collateral base quantity for transfer // de-leverage and get the collateral base quantity for transfer
quantity := trade.Quantity quantity := tradeQuantity.Div(s.Leverage)
quantity = quantity.Div(s.Leverage)
balances, err := s.futuresSession.Exchange.QueryAccountBalances(ctx) balances, err := s.futuresSession.Exchange.QueryAccountBalances(ctx)
if err != nil { if err != nil {
log.Infof("adding to pending base transfer: %s %s + %s", quantity.String(), currency, s.State.PendingBaseTransfer.String()) log.Infof("adding to pending base transfer: %s %s + %s", quantity.String(), asset, s.State.PendingBaseTransfer.String())
s.State.PendingBaseTransfer = s.State.PendingBaseTransfer.Add(quantity) s.State.PendingBaseTransfer = s.State.PendingBaseTransfer.Add(quantity)
return err return err
} }
b, ok := balances[currency] b, ok := balances[asset]
if !ok { if !ok {
log.Infof("adding to pending base transfer: %s %s + %s", quantity.String(), currency, s.State.PendingBaseTransfer.String()) log.Infof("adding to pending base transfer: %s %s + %s", quantity.String(), asset, s.State.PendingBaseTransfer.String())
s.State.PendingBaseTransfer = s.State.PendingBaseTransfer.Add(quantity) s.State.PendingBaseTransfer = s.State.PendingBaseTransfer.Add(quantity)
return fmt.Errorf("%s balance not found", currency) return fmt.Errorf("%s balance not found", asset)
} }
// add the previous pending base transfer and the current trade quantity // add the previous pending base transfer and the current trade quantity
@ -53,7 +47,7 @@ func (s *Strategy) transferOut(ctx context.Context, ex FuturesTransfer, currency
// TODO: according to the fee, we might not be able to get enough balance greater than the trade quantity, we can adjust the quantity here // TODO: according to the fee, we might not be able to get enough balance greater than the trade quantity, we can adjust the quantity here
if amount.IsZero() { if amount.IsZero() {
log.Infof("adding to pending base transfer: %s %s + %s ", quantity.String(), currency, s.State.PendingBaseTransfer.String()) log.Infof("adding to pending base transfer: %s %s + %s ", quantity.String(), asset, s.State.PendingBaseTransfer.String())
s.State.PendingBaseTransfer = s.State.PendingBaseTransfer.Add(quantity) s.State.PendingBaseTransfer = s.State.PendingBaseTransfer.Add(quantity)
return nil return nil
} }
@ -64,8 +58,8 @@ func (s *Strategy) transferOut(ctx context.Context, ex FuturesTransfer, currency
// if s.State.TotalBaseTransfer.Compare(collateralBase) // if s.State.TotalBaseTransfer.Compare(collateralBase)
log.Infof("transfering out futures account asset %s %s", amount, currency) log.Infof("transfering out futures account asset %s %s", amount, asset)
if err := ex.TransferFuturesAccountAsset(ctx, currency, amount, types.TransferOut); err != nil { if err := ex.TransferFuturesAccountAsset(ctx, asset, amount, types.TransferOut); err != nil {
return err return err
} }