From 04da988639d4424939525ecceb4bec43253a880d Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 24 Feb 2023 18:45:18 +0800 Subject: [PATCH] grid2: check if we have o.AveragePrice, use it for newQuantity --- pkg/strategy/grid2/strategy.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/strategy/grid2/strategy.go b/pkg/strategy/grid2/strategy.go index 216f332da..b2ac9f3ff 100644 --- a/pkg/strategy/grid2/strategy.go +++ b/pkg/strategy/grid2/strategy.go @@ -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) } }