From b9c41b7ad7cb01535bef91c4b72a726119de9602 Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 20 Aug 2024 14:14:19 +0800 Subject: [PATCH] twap: improve cancelContextIfTargetQuantityFilled check method --- pkg/twap/v2/stream_executor.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/twap/v2/stream_executor.go b/pkg/twap/v2/stream_executor.go index 042a1fa94..caa43686d 100644 --- a/pkg/twap/v2/stream_executor.go +++ b/pkg/twap/v2/stream_executor.go @@ -220,10 +220,14 @@ func (e *FixedQuantityExecutor) handleFilledOrder(order types.Order) { } func (e *FixedQuantityExecutor) cancelContextIfTargetQuantityFilled() bool { + // ensure that the trades are processed + e.tradeCollector.Process() + + // now get the base quantity from the position base := e.position.GetBase() - if base.Abs().Compare(e.targetQuantity) >= 0 { - e.logger.Infof("filled target quantity, canceling the order execution context") + if base.Abs().Sub(e.targetQuantity).Compare(e.market.MinQuantity.Neg()) >= 0 { + e.logger.Infof("position is filled with target quantity, canceling the order execution context") e.cancelExecution() return true }