grid2: fix calculateQuoteBaseInvestmentQuantity grid calculation

This commit is contained in:
c9s 2022-12-06 01:21:41 +08:00
parent 541c0e76b5
commit 7e0ac66ea1

View File

@ -534,7 +534,7 @@ func (s *Strategy) calculateQuoteBaseInvestmentQuantity(quoteInvestment, baseInv
if sellPrice.Compare(lastPrice) < 0 { if sellPrice.Compare(lastPrice) < 0 {
break break
} }
numberOfSellOrders++ numberOfSellOrders++
} }
@ -552,17 +552,25 @@ func (s *Strategy) calculateQuoteBaseInvestmentQuantity(quoteInvestment, baseInv
s.logger.Infof("grid base investment quantity range: %f <=> %f", minBaseQuantity.Float64(), maxBaseQuantity.Float64()) s.logger.Infof("grid base investment quantity range: %f <=> %f", minBaseQuantity.Float64(), maxBaseQuantity.Float64())
} }
buyPlacedPrice := fixedpoint.Zero
totalQuotePrice := fixedpoint.Zero totalQuotePrice := fixedpoint.Zero
// quoteInvestment = (p1 * q) + (p2 * q) + (p3 * q) + .... // quoteInvestment = (p1 * q) + (p2 * q) + (p3 * q) + ....
// => // =>
// quoteInvestment = (p1 + p2 + p3) * q // quoteInvestment = (p1 + p2 + p3) * q
// maxBuyQuantity = quoteInvestment / (p1 + p2 + p3) // maxBuyQuantity = quoteInvestment / (p1 + p2 + p3)
si := len(pins) - 1
for i := len(pins) - 1; i >= 0; i-- { for i := len(pins) - 1; i >= 0; i-- {
pin := pins[i] pin := pins[i]
price := fixedpoint.Value(pin) price := fixedpoint.Value(pin)
sellPrice := price
// when profitSpread is set, the sell price is shift upper with the given spread
if s.ProfitSpread.Sign() > 0 {
sellPrice = sellPrice.Add(s.ProfitSpread)
}
if price.Compare(lastPrice) >= 0 { if price.Compare(lastPrice) >= 0 {
si = i
// for orders that sell // for orders that sell
// if we still have the base balance // if we still have the base balance
// quantity := amount.Div(lastPrice) // quantity := amount.Div(lastPrice)
@ -572,11 +580,10 @@ func (s *Strategy) calculateQuoteBaseInvestmentQuantity(quoteInvestment, baseInv
nextLowerPrice := fixedpoint.Value(nextLowerPin) nextLowerPrice := fixedpoint.Value(nextLowerPin)
// requiredQuote = requiredQuote.Add(quantity.Mul(nextLowerPrice)) // requiredQuote = requiredQuote.Add(quantity.Mul(nextLowerPrice))
totalQuotePrice = totalQuotePrice.Add(nextLowerPrice) totalQuotePrice = totalQuotePrice.Add(nextLowerPrice)
buyPlacedPrice = nextLowerPrice
} }
} else { } else {
// for orders that buy // for orders that buy
if !buyPlacedPrice.IsZero() && price.Compare(buyPlacedPrice) == 0 { if s.ProfitSpread.IsZero() && i+1 == si {
continue continue
} }