grid2: rewrite the base+quote algo

This commit is contained in:
c9s 2023-05-19 15:04:17 +08:00
parent 86a99b5902
commit 0c4cd7049f
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -779,21 +779,21 @@ func (s *Strategy) calculateBaseQuoteInvestmentQuantity(quoteInvestment, baseInv
// if the maxBaseQuantity is less than minQuantity, then we need to reduce the number of the sell orders
// so that the quantity can be increased.
maxNumberOfSellOrders := numberOfSellOrders + 1
minBaseQuantity := fixedpoint.Max(s.Market.MinNotional.Div(lastPrice), s.Market.MinQuantity)
maxBaseQuantity := fixedpoint.Zero
for maxBaseQuantity.Compare(s.Market.MinQuantity) <= 0 || maxBaseQuantity.Compare(minBaseQuantity) <= 0 {
maxNumberOfSellOrders--
maxBaseQuantity = baseInvestment.Div(fixedpoint.NewFromInt(int64(maxNumberOfSellOrders)))
baseQuantity := s.Market.TruncateQuantity(
baseInvestment.Div(
fixedpoint.NewFromInt(
int64(numberOfSellOrders))))
// maxBaseQuantity = s.Market.RoundDownQuantityByPrecision(maxBaseQuantity)
maxBaseQuantity = s.Market.TruncateQuantity(maxBaseQuantity)
minBaseQuantity := fixedpoint.Max(
s.Market.MinNotional.Div(lastPrice),
s.Market.MinQuantity)
if baseQuantity.Compare(minBaseQuantity) <= 0 {
numberOfSellOrders = int(math.Floor(baseInvestment.Div(minBaseQuantity).Float64()))
}
s.logger.Infof("grid base investment sell orders: %d", maxNumberOfSellOrders)
if maxNumberOfSellOrders > 0 {
s.logger.Infof("grid base investment quantity: %f (base investment) / %d (number of sell orders) = %f (base quantity per order)", baseInvestment.Float64(), maxNumberOfSellOrders, maxBaseQuantity.Float64())
}
s.logger.Infof("grid base investment sell orders: %d", numberOfSellOrders)
s.logger.Infof("grid base investment quantity: %f (base investment) / %d (number of sell orders) = %f (base quantity per order)", baseInvestment.Float64(), numberOfSellOrders, baseQuantity.Float64())
// calculate quantity with quote investment
totalQuotePrice := fixedpoint.Zero
@ -802,7 +802,7 @@ func (s *Strategy) calculateBaseQuoteInvestmentQuantity(quoteInvestment, baseInv
// quoteInvestment = (p1 + p2 + p3) * q
// maxBuyQuantity = quoteInvestment / (p1 + p2 + p3)
si := -1
for i := len(pins) - 1 - maxNumberOfSellOrders; i >= 0; i-- {
for i := len(pins) - 1 - numberOfSellOrders; i >= 0; i-- {
pin := pins[i]
price := fixedpoint.Value(pin)
@ -838,8 +838,8 @@ func (s *Strategy) calculateBaseQuoteInvestmentQuantity(quoteInvestment, baseInv
}
quoteSideQuantity := quoteInvestment.Div(totalQuotePrice)
if maxNumberOfSellOrders > 0 {
return fixedpoint.Min(quoteSideQuantity, maxBaseQuantity), nil
if numberOfSellOrders > 0 {
return fixedpoint.Min(quoteSideQuantity, baseQuantity), nil
}
return quoteSideQuantity, nil