From 80ea46ca92bac25840bf759e832f75ebca764521 Mon Sep 17 00:00:00 2001 From: chiahung Date: Thu, 9 Nov 2023 16:19:53 +0800 Subject: [PATCH] FEATURE: use rest quote to place the last order when opening grid --- pkg/strategy/grid2/strategy.go | 26 +++++++++++++++++++++----- pkg/types/backtest_stream.go | 1 + 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/pkg/strategy/grid2/strategy.go b/pkg/strategy/grid2/strategy.go index 620d91ce1..f463b4ad6 100644 --- a/pkg/strategy/grid2/strategy.go +++ b/pkg/strategy/grid2/strategy.go @@ -1362,7 +1362,11 @@ func (s *Strategy) generateGridOrders(totalQuote, totalBase, lastPrice fixedpoin ClientOrderID: s.newClientOrderID(), }) quoteQuantity := quantity.Mul(nextPrice) - usedQuote = usedQuote.Add(quoteQuantity) + + // because the precision issue, we need to round up quote quantity and add it into used quote + // e.g. quote we calculate : 8888.85, but it may lock 8888.9 due to their precision. + roundUpQuoteQuantity := quoteQuantity.Round(s.Market.VolumePrecision, fixedpoint.Up) + usedQuote = usedQuote.Add(roundUpQuoteQuantity) } } else { // if price spread is not enabled, and we have already placed a sell order index on the top of this price, @@ -1378,9 +1382,21 @@ func (s *Strategy) generateGridOrders(totalQuote, totalBase, lastPrice fixedpoin quoteQuantity := quantity.Mul(price) - if usedQuote.Add(quoteQuantity).Compare(totalQuote) > 0 { - s.logger.Warnf("used quote %f > total quote %f, this should not happen", usedQuote.Add(quoteQuantity).Float64(), totalQuote.Float64()) - continue + // because the precision issue, we need to round up quote quantity and add it into used quote + // e.g. quote we calculate : 8888.85, but it may lock 8888.9 due to their precision. + roundUpQuoteQuantity := quoteQuantity.Round(s.Market.VolumePrecision, fixedpoint.Up) + if usedQuote.Add(roundUpQuoteQuantity).Compare(totalQuote) > 0 { + if i > 0 { + s.logger.Errorf("used quote %f > total quote %f, this should not happen", usedQuote.Add(quoteQuantity).Float64(), totalQuote.Float64()) + return nil, fmt.Errorf("used quote %f > total quote %f, this should not happen", usedQuote.Add(quoteQuantity).Float64(), totalQuote.Float64()) + } else { + restQuote := totalQuote.Sub(usedQuote) + quantity = restQuote.Div(price).Round(s.Market.VolumePrecision, fixedpoint.Down) + if s.Market.MinQuantity.Compare(quantity) > 0 { + s.logger.Errorf("the round down quantity (%s) is less than min quantity (%s), we cannot place this order", quantity, s.Market.MinQuantity) + return nil, fmt.Errorf("the round down quantity (%s) is less than min quantity (%s), we cannot place this order", quantity, s.Market.MinQuantity) + } + } } submitOrders = append(submitOrders, types.SubmitOrder{ @@ -1395,7 +1411,7 @@ func (s *Strategy) generateGridOrders(totalQuote, totalBase, lastPrice fixedpoin GroupID: s.OrderGroupID, ClientOrderID: s.newClientOrderID(), }) - usedQuote = usedQuote.Add(quoteQuantity) + usedQuote = usedQuote.Add(roundUpQuoteQuantity) } } diff --git a/pkg/types/backtest_stream.go b/pkg/types/backtest_stream.go index ee46d31fc..dadd0c147 100644 --- a/pkg/types/backtest_stream.go +++ b/pkg/types/backtest_stream.go @@ -11,6 +11,7 @@ type BacktestStream struct { func (s *BacktestStream) Connect(ctx context.Context) error { s.EmitConnect() s.EmitStart() + s.EmitAuth() return nil }