Merge pull request #1056 from c9s/fix/grid2/upper-price

grid2: fix upper price error
This commit is contained in:
Yo-An Lin 2023-02-10 18:10:04 +08:00 committed by GitHub
commit 7ed0a0225e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 2 deletions

View File

@ -40,13 +40,16 @@ func calculateArithmeticPins(lower, upper, spread, tickSize fixedpoint.Value) []
var ts = tickSize.Float64()
var prec = int(math.Round(math.Log10(ts) * -1.0))
var pow10 = math.Pow10(prec)
for p := lower; p.Compare(upper) <= 0; p = p.Add(spread) {
for p := lower; p.Compare(upper.Sub(spread)) <= 0; p = p.Add(spread) {
pp := math.Round(p.Float64()*pow10*10.0) / 10.0
pp = math.Trunc(pp) / pow10
pin := Pin(fixedpoint.NewFromFloat(pp))
pins = append(pins, pin)
}
// this makes sure there is no error at the upper price
pins = append(pins, Pin(upper))
return pins
}

View File

@ -956,7 +956,8 @@ func (s *Strategy) generateGridOrders(totalQuote, totalBase, lastPrice fixedpoin
})
usedBase = usedBase.Add(quantity)
} else if i > 0 {
// next price
// if we don't have enough base asset
// then we need to place a buy order at the next price.
nextPin := pins[i-1]
nextPrice := fixedpoint.Value(nextPin)
submitOrders = append(submitOrders, types.SubmitOrder{
@ -975,10 +976,17 @@ func (s *Strategy) generateGridOrders(totalQuote, totalBase, lastPrice fixedpoin
// skip i == 0
}
} else {
// if price spread is not enabled, and we have already placed a sell order index on the top of this price,
// then we should skip
if s.ProfitSpread.IsZero() && i+1 == si {
continue
}
// should never place a buy order at the upper price
if i == len(pins)-1 {
continue
}
submitOrders = append(submitOrders, types.SubmitOrder{
Symbol: s.Symbol,
Type: types.OrderTypeLimit,