From 9f8576bb38b9141d9269b8a2e38d66ae37b43c22 Mon Sep 17 00:00:00 2001 From: Andy Cheng Date: Thu, 20 Apr 2023 18:37:48 +0800 Subject: [PATCH] improve/supertrend: different way to calculate order amount for backtesting --- pkg/strategy/supertrend/strategy.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/strategy/supertrend/strategy.go b/pkg/strategy/supertrend/strategy.go index 309ecc4cf..1003c621e 100644 --- a/pkg/strategy/supertrend/strategy.go +++ b/pkg/strategy/supertrend/strategy.go @@ -626,8 +626,14 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se // Add opposite position amount if any if (side == types.SideTypeSell && s.Position.IsLong()) || (side == types.SideTypeBuy && s.Position.IsShort()) { - bbgo.Notify("add existing opposite position amount of %s to the amount of open new position order", s.Symbol) - amount = amount.Add(s.Position.GetQuantity()) + if bbgo.IsBackTesting { + _ = s.ClosePosition(ctx, fixedpoint.One) + bbgo.Notify("close existing %s position before open a new position", s.Symbol) + amount = s.calculateQuantity(ctx, closePrice, side) + } else { + bbgo.Notify("add existing opposite position amount %f of %s to the amount %f of open new position order", s.Position.GetQuantity().Float64(), s.Symbol, amount.Float64()) + amount = amount.Add(s.Position.GetQuantity()) + } } else if !s.Position.IsDust(closePrice) { bbgo.Notify("existing %s position has the same direction as the signal", s.Symbol) return