diff --git a/pkg/bbgo/exit_protective_stop_loss.go b/pkg/bbgo/exit_protective_stop_loss.go index d2e61815a..5e35858b0 100644 --- a/pkg/bbgo/exit_protective_stop_loss.go +++ b/pkg/bbgo/exit_protective_stop_loss.go @@ -30,6 +30,10 @@ type ProtectiveStopLoss struct { // PlaceStopOrder places the stop order on exchange and lock the balance PlaceStopOrder bool `json:"placeStopOrder"` + // Interval is the time resolution to update the stop order + // KLine per Interval will be used for updating the stop order + Interval types.Interval `json:"interval,omitempty"` + session *ExchangeSession orderExecutor *GeneralOrderExecutor stopLossPrice fixedpoint.Value @@ -37,8 +41,8 @@ type ProtectiveStopLoss struct { } func (s *ProtectiveStopLoss) Subscribe(session *ExchangeSession) { - // use 1m kline to handle roi stop - session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: types.Interval1m}) + // use kline to handle roi stop + session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval}) } func (s *ProtectiveStopLoss) shouldActivate(position *types.Position, closePrice fixedpoint.Value) bool { @@ -131,8 +135,8 @@ func (s *ProtectiveStopLoss) Bind(session *ExchangeSession, orderExecutor *Gener s.stopLossPrice = fixedpoint.Zero } } - session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, types.Interval1m, f)) - session.MarketDataStream.OnKLine(types.KLineWith(s.Symbol, types.Interval1m, f)) + session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.Interval, f)) + session.MarketDataStream.OnKLine(types.KLineWith(s.Symbol, s.Interval, 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 b026ecfa0..ad8d5689a 100644 --- a/pkg/bbgo/exit_roi_stop_loss.go +++ b/pkg/bbgo/exit_roi_stop_loss.go @@ -11,14 +11,17 @@ type RoiStopLoss struct { Symbol string Percentage fixedpoint.Value `json:"percentage"` CancelActiveOrders bool `json:"cancelActiveOrders"` + // Interval is the time resolution to update the stop order + // KLine per Interval will be used for updating the stop order + Interval types.Interval `json:"interval,omitempty"` session *ExchangeSession orderExecutor *GeneralOrderExecutor } func (s *RoiStopLoss) Subscribe(session *ExchangeSession) { - // use 1m kline to handle roi stop - session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: types.Interval1m}) + // use kline to handle roi stop + session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval}) } func (s *RoiStopLoss) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExecutor) { @@ -30,8 +33,8 @@ func (s *RoiStopLoss) Bind(session *ExchangeSession, orderExecutor *GeneralOrder 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)) + session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.Interval, f)) + session.MarketDataStream.OnKLine(types.KLineWith(s.Symbol, s.Interval, f)) if !IsBackTesting && enableMarketTradeStop { session.MarketDataStream.OnMarketTrade(func(trade types.Trade) { diff --git a/pkg/bbgo/exit_roi_take_profit.go b/pkg/bbgo/exit_roi_take_profit.go index c5853a0e5..60672f5e8 100644 --- a/pkg/bbgo/exit_roi_take_profit.go +++ b/pkg/bbgo/exit_roi_take_profit.go @@ -13,13 +13,17 @@ type RoiTakeProfit struct { Percentage fixedpoint.Value `json:"percentage"` CancelActiveOrders bool `json:"cancelActiveOrders"` + // Interval is the time resolution to update the stop order + // KLine per Interval will be used for updating the stop order + Interval types.Interval `json:"interval,omitempty"` + session *ExchangeSession orderExecutor *GeneralOrderExecutor } func (s *RoiTakeProfit) Subscribe(session *ExchangeSession) { - // use 1m kline to handle roi stop - session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: types.Interval1m}) + // use kline to handle roi stop + session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval}) } func (s *RoiTakeProfit) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExecutor) { @@ -27,7 +31,7 @@ func (s *RoiTakeProfit) Bind(session *ExchangeSession, orderExecutor *GeneralOrd s.orderExecutor = orderExecutor position := orderExecutor.Position() - session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, types.Interval1m, func(kline types.KLine) { + session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.Interval, func(kline types.KLine) { closePrice := kline.Close if position.IsClosed() || position.IsDust(closePrice) || position.IsClosing() { return