From ac1b5aa0e200e96ab91f0fff0fdb84b5869072fb Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 21 Jun 2023 17:58:18 +0800 Subject: [PATCH] bbgo: trigger price check when kline is updated (not just closed) --- pkg/bbgo/exit_protective_stop_loss.go | 7 +++++-- pkg/bbgo/exit_roi_stop_loss.go | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pkg/bbgo/exit_protective_stop_loss.go b/pkg/bbgo/exit_protective_stop_loss.go index 456fc50c6..8a741dd6d 100644 --- a/pkg/bbgo/exit_protective_stop_loss.go +++ b/pkg/bbgo/exit_protective_stop_loss.go @@ -124,14 +124,17 @@ func (s *ProtectiveStopLoss) Bind(session *ExchangeSession, orderExecutor *Gener }) position := orderExecutor.Position() - session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, types.Interval1m, func(kline types.KLine) { + + f := func(kline types.KLine) { isPositionOpened := !position.IsClosed() && !position.IsDust(kline.Close) if isPositionOpened { s.handleChange(context.Background(), position, kline.Close, s.orderExecutor) } else { s.stopLossPrice = fixedpoint.Zero } - })) + } + session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, types.Interval1m, f)) + session.MarketDataStream.OnKLine(types.KLineWith(s.Symbol, types.Interval1m, f)) if !IsBackTesting && enableMarketTradeStop { session.MarketDataStream.OnMarketTrade(func(trade types.Trade) { diff --git a/pkg/bbgo/exit_roi_stop_loss.go b/pkg/bbgo/exit_roi_stop_loss.go index cb1c884f1..bdcf6e480 100644 --- a/pkg/bbgo/exit_roi_stop_loss.go +++ b/pkg/bbgo/exit_roi_stop_loss.go @@ -26,9 +26,12 @@ func (s *RoiStopLoss) Bind(session *ExchangeSession, orderExecutor *GeneralOrder s.orderExecutor = orderExecutor position := orderExecutor.Position() - session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, types.Interval1m, func(kline types.KLine) { + f := func(kline types.KLine) { s.checkStopPrice(kline.Close, position) - })) + } + + session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, types.Interval1m, f)) + session.MarketDataStream.OnKLine(types.KLineWith(s.Symbol, types.Interval1m, f)) if !IsBackTesting && enableMarketTradeStop { session.MarketDataStream.OnMarketTrade(func(trade types.Trade) {