grid2: check if we have o.AveragePrice, use it for newQuantity

This commit is contained in:
c9s 2023-02-24 18:45:18 +08:00
parent 7eb953093c
commit 04da988639
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -363,7 +363,13 @@ func (s *Strategy) processFilledOrder(o types.Order) {
newSide := types.SideTypeSell
newPrice := o.Price
newQuantity := o.Quantity
orderQuoteQuantity := o.Quantity.Mul(o.Price)
executedPrice := o.Price
if o.AveragePrice.Sign() > 0 {
executedPrice = o.AveragePrice
}
// will be used for calculating quantity
orderExecutedQuoteAmount := o.Quantity.Mul(executedPrice)
// collect trades
baseSellQuantityReduction := fixedpoint.Zero
@ -382,7 +388,7 @@ func (s *Strategy) processFilledOrder(o types.Order) {
// use the profit to buy more inventory in the grid
if s.Compound || s.EarnBase {
newQuantity = fixedpoint.Max(orderQuoteQuantity.Div(newPrice), s.Market.MinQuantity)
newQuantity = fixedpoint.Max(orderExecutedQuoteAmount.Div(newPrice), s.Market.MinQuantity)
} else if s.QuantityOrAmount.Quantity.Sign() > 0 {
newQuantity = s.QuantityOrAmount.Quantity
}
@ -415,7 +421,7 @@ func (s *Strategy) processFilledOrder(o types.Order) {
}
if s.EarnBase {
newQuantity = fixedpoint.Max(orderQuoteQuantity.Div(newPrice).Sub(baseSellQuantityReduction), s.Market.MinQuantity)
newQuantity = fixedpoint.Max(orderExecutedQuoteAmount.Div(newPrice).Sub(baseSellQuantityReduction), s.Market.MinQuantity)
}
}