From 61ea41d99998de1683e76a633bc1a06b3e9cc86a Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 23 Sep 2024 22:16:53 +0800 Subject: [PATCH] xdepthmaker: simplify hedge --- pkg/exchange/bitget/stream.go | 6 +++--- pkg/strategy/xdepthmaker/strategy.go | 32 +++++----------------------- pkg/strategy/xmaker/strategy.go | 19 ++++++++++------- 3 files changed, 19 insertions(+), 38 deletions(-) diff --git a/pkg/exchange/bitget/stream.go b/pkg/exchange/bitget/stream.go index 415618b4e..89df0ca98 100644 --- a/pkg/exchange/bitget/stream.go +++ b/pkg/exchange/bitget/stream.go @@ -435,13 +435,13 @@ func (s *Stream) handleOrderTradeEvent(m OrderTradeEvent) { return } - debugf("received %s (%s) OrderTradeEvent: %+v", m.instId, m.actionType, m) + debugf("received OrderTradeEvent %s (%s): %+v", m.instId, m.actionType, m) for _, order := range m.Orders { if order.TradeId == 0 { - debugf("%s order update #%d: %+v", m.instId, order.OrderId, order) + debugf("received %s order update #%d: %+v", m.instId, order.OrderId, order) } else { - debugf("%s order update #%d and trade update #%d: %+v", m.instId, order.OrderId, order, order.TradeId) + debugf("received %s order update #%d and trade update #%d: %+v", m.instId, order.OrderId, order.TradeId, order) } globalOrder, err := order.toGlobalOrder() diff --git a/pkg/strategy/xdepthmaker/strategy.go b/pkg/strategy/xdepthmaker/strategy.go index 52029172c..2dde64388 100644 --- a/pkg/strategy/xdepthmaker/strategy.go +++ b/pkg/strategy/xdepthmaker/strategy.go @@ -16,6 +16,7 @@ import ( "github.com/c9s/bbgo/pkg/exchange/retry" "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/strategy/common" + "github.com/c9s/bbgo/pkg/strategy/xmaker" "github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/util" "github.com/c9s/bbgo/pkg/util/tradingutil" @@ -570,37 +571,15 @@ func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) { // adjust quantity according to the balances account := s.hedgeSession.GetAccount() - switch side { - case types.SideTypeBuy: - // check quote quantity - if quote, ok := account.Balance(s.hedgeMarket.QuoteCurrency); ok { - if quote.Available.Compare(notional) < 0 { - // adjust price to higher 0.1%, so that we can ensure that the order can be executed - quantity = bbgo.AdjustQuantityByMaxAmount(quantity, lastPrice.Mul(lastPriceModifier), quote.Available) - quantity = s.hedgeMarket.TruncateQuantity(quantity) - } - } - - case types.SideTypeSell: - // check quote quantity - if base, ok := account.Balance(s.hedgeMarket.BaseCurrency); ok { - if base.Available.Compare(quantity) < 0 { - quantity = base.Available - } - } - } + quantity = xmaker.AdjustHedgeQuantityWithAvailableBalance(account, + s.hedgeMarket, side, quantity, lastPrice) // truncate quantity for the supported precision quantity = s.hedgeMarket.TruncateQuantity(quantity) - if notional.Compare(s.hedgeMarket.MinNotional.Mul(minGap)) <= 0 { - log.Warnf("the adjusted amount %v is less than minimal notional %v, skipping hedge", notional, s.hedgeMarket.MinNotional) - return - } - - if quantity.Compare(s.hedgeMarket.MinQuantity.Mul(minGap)) <= 0 { - log.Warnf("the adjusted quantity %v is less than minimal quantity %v, skipping hedge", quantity, s.hedgeMarket.MinQuantity) + if s.hedgeMarket.IsDustQuantity(quantity, lastPrice) { + log.Warnf("skip dust quantity: %s @ price %f", quantity.String(), lastPrice.Float64()) return } @@ -613,7 +592,6 @@ func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) { s.hedgeErrorRateReservation = nil } - log.Infof("submitting %s hedge order %s %v", s.HedgeSymbol, side.String(), quantity) bbgo.Notify("Submitting %s hedge order %s %v", s.HedgeSymbol, side.String(), quantity) _, err := s.HedgeOrderExecutor.SubmitOrders(ctx, types.SubmitOrder{ diff --git a/pkg/strategy/xmaker/strategy.go b/pkg/strategy/xmaker/strategy.go index b6074e210..5826207fe 100644 --- a/pkg/strategy/xmaker/strategy.go +++ b/pkg/strategy/xmaker/strategy.go @@ -1030,17 +1030,19 @@ func (s *Strategy) tryArbitrage(ctx context.Context, quote *Quote, makerBalances return true, nil } -func (s *Strategy) adjustHedgeQuantityWithAvailableBalance( - account *types.Account, side types.SideType, quantity, lastPrice fixedpoint.Value, +func AdjustHedgeQuantityWithAvailableBalance( + account *types.Account, + market types.Market, + side types.SideType, quantity, lastPrice fixedpoint.Value, ) fixedpoint.Value { switch side { case types.SideTypeBuy: // check quote quantity - if quote, ok := account.Balance(s.sourceMarket.QuoteCurrency); ok { - if quote.Available.Compare(s.sourceMarket.MinNotional) < 0 { + if quote, ok := account.Balance(market.QuoteCurrency); ok { + if quote.Available.Compare(market.MinNotional) < 0 { // adjust price to higher 0.1%, so that we can ensure that the order can be executed - availableQuote := s.sourceMarket.TruncateQuoteQuantity(quote.Available) + availableQuote := market.TruncateQuoteQuantity(quote.Available) quantity = bbgo.AdjustQuantityByMaxAmount(quantity, lastPrice, availableQuote) } @@ -1048,7 +1050,7 @@ func (s *Strategy) adjustHedgeQuantityWithAvailableBalance( case types.SideTypeSell: // check quote quantity - if base, ok := account.Balance(s.sourceMarket.BaseCurrency); ok { + if base, ok := account.Balance(market.BaseCurrency); ok { if base.Available.Compare(quantity) < 0 { quantity = base.Available } @@ -1056,7 +1058,7 @@ func (s *Strategy) adjustHedgeQuantityWithAvailableBalance( } // truncate the quantity to the supported precision - return s.sourceMarket.TruncateQuantity(quantity) + return market.TruncateQuantity(quantity) } func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) { @@ -1094,7 +1096,8 @@ func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) { return } } else { - quantity = s.adjustHedgeQuantityWithAvailableBalance(account, side, quantity, lastPrice) + quantity = AdjustHedgeQuantityWithAvailableBalance( + account, s.sourceMarket, side, quantity, lastPrice) } // truncate quantity for the supported precision