Skip to content

Commit

Permalink
Merge pull request #1802 from c9s/c9s/xmaker/truncate-balances
Browse files Browse the repository at this point in the history
FIX: [xmaker] truncate balances before submitting orders
  • Loading branch information
c9s authored Nov 4, 2024
2 parents 1d6e850 + c3b058c commit 6f1b216
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/strategy/xmaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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))
Expand All @@ -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,
Expand Down

0 comments on commit 6f1b216

Please sign in to comment.