From c3b058ce38af3474868d5af65cf21e637d26aedd Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 4 Nov 2024 17:23:36 +0800 Subject: [PATCH] xmaker: truncate balances before submitting orders --- pkg/strategy/xmaker/strategy.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/strategy/xmaker/strategy.go b/pkg/strategy/xmaker/strategy.go index ad4c0ecbc..c3b31c78f 100644 --- a/pkg/strategy/xmaker/strategy.go +++ b/pkg/strategy/xmaker/strategy.go @@ -1060,9 +1060,11 @@ func (s *Strategy) tryArbitrage(ctx context.Context, quote *Quote, makerBalances return false, nil } + availableQuote := s.makerMarket.TruncateQuoteQuantity(quoteBalance.Available) + askPvs := s.makerBook.SideBook(types.SideTypeSell) sumPv := aggregatePriceVolumeSliceWithPriceFilter(types.SideTypeSell, askPvs, marginBidPrice) - qty := fixedpoint.Min(quoteBalance.Available.Div(sumPv.Price), sumPv.Volume) + qty := fixedpoint.Min(availableQuote.Div(sumPv.Price), sumPv.Volume) if sourceBase, ok := hedgeBalances[s.sourceMarket.BaseCurrency]; ok { qty = fixedpoint.Min(qty, sourceBase.Available) @@ -1077,6 +1079,7 @@ func (s *Strategy) tryArbitrage(ctx context.Context, quote *Quote, makerBalances iocOrders = append(iocOrders, types.SubmitOrder{ Symbol: s.Symbol, + Market: s.makerMarket, Type: types.OrderTypeLimit, Side: types.SideTypeBuy, Price: sumPv.Price, @@ -1090,9 +1093,11 @@ func (s *Strategy) tryArbitrage(ctx context.Context, quote *Quote, makerBalances return false, nil } + availableBase := s.makerMarket.TruncateQuantity(baseBalance.Available) + bidPvs := s.makerBook.SideBook(types.SideTypeBuy) sumPv := aggregatePriceVolumeSliceWithPriceFilter(types.SideTypeBuy, bidPvs, marginAskPrice) - qty := fixedpoint.Min(baseBalance.Available, sumPv.Volume) + qty := fixedpoint.Min(availableBase, sumPv.Volume) if sourceQuote, ok := hedgeBalances[s.sourceMarket.QuoteCurrency]; ok { qty = fixedpoint.Min(qty, quote.BestAskPrice.Div(sourceQuote.Available)) @@ -1108,6 +1113,7 @@ func (s *Strategy) tryArbitrage(ctx context.Context, quote *Quote, makerBalances // send ioc order for arbitrage iocOrders = append(iocOrders, types.SubmitOrder{ Symbol: s.Symbol, + Market: s.makerMarket, Type: types.OrderTypeLimit, Side: types.SideTypeSell, Price: sumPv.Price,