diff --git a/pkg/bbgo/exit_trailing_stop.go b/pkg/bbgo/exit_trailing_stop.go index fc953037b..2162512e9 100644 --- a/pkg/bbgo/exit_trailing_stop.go +++ b/pkg/bbgo/exit_trailing_stop.go @@ -47,6 +47,7 @@ func (s *TrailingStop2) Subscribe(session *ExchangeSession) { func (s *TrailingStop2) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExecutor) { s.session = session s.orderExecutor = orderExecutor + s.latestHigh = fixedpoint.Zero position := orderExecutor.Position() session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.Interval, func(kline types.KLine) { @@ -139,6 +140,11 @@ func (s *TrailingStop2) checkStopPrice(price fixedpoint.Value, position *types.P } func (s *TrailingStop2) triggerStop(price fixedpoint.Value) error { + // reset activated flag + defer func() { + s.activated = false + s.latestHigh = fixedpoint.Zero + }() Notify("[TrailingStop] %s stop loss triggered. price: %f callback rate: %f", s.Symbol, price.Float64(), s.CallbackRate.Float64()) ctx := context.Background() return s.orderExecutor.ClosePosition(ctx, fixedpoint.One, "trailingStop") diff --git a/pkg/bbgo/exit_trailing_stop_test.go b/pkg/bbgo/exit_trailing_stop_test.go index d1285a1a3..ddf53c566 100644 --- a/pkg/bbgo/exit_trailing_stop_test.go +++ b/pkg/bbgo/exit_trailing_stop_test.go @@ -96,7 +96,7 @@ func TestTrailingStop(t *testing.T) { err = stop.checkStopPrice(currentPrice, position) if assert.NoError(t, err) { assert.Equal(t, fixedpoint.NewFromFloat(19798.02), currentPrice) - assert.Equal(t, fixedpoint.NewFromFloat(19602.0), stop.latestHigh) - assert.True(t, stop.activated) + assert.Equal(t, fixedpoint.Zero, stop.latestHigh) + assert.False(t, stop.activated) } }