Merge pull request #1405 from c9s/chiahung/grid2/use-rest-quote

FIX: [grid2] use rest quote to place the last order when opening grid
This commit is contained in:
kbearXD 2023-11-23 12:46:53 +08:00 committed by GitHub
commit 75b8be5e17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 5 deletions

View File

@ -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.PricePrecision, 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,19 @@ 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.PricePrecision, fixedpoint.Up)
if usedQuote.Add(roundUpQuoteQuantity).Compare(totalQuote) > 0 {
if i > 0 {
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 {
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 +1409,7 @@ func (s *Strategy) generateGridOrders(totalQuote, totalBase, lastPrice fixedpoin
GroupID: s.OrderGroupID,
ClientOrderID: s.newClientOrderID(),
})
usedQuote = usedQuote.Add(quoteQuantity)
usedQuote = usedQuote.Add(roundUpQuoteQuantity)
}
}

View File

@ -11,6 +11,7 @@ type BacktestStream struct {
func (s *BacktestStream) Connect(ctx context.Context) error {
s.EmitConnect()
s.EmitStart()
s.EmitAuth()
return nil
}