From 151722664fcdd0a9d066ca18dd96771a38d2ddd0 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 28 Feb 2024 14:30:22 +0800 Subject: [PATCH 1/2] Use configuration instead of kine fixed interval --- pkg/bbgo/exit_protective_stop_loss.go | 12 ++++++++---- pkg/bbgo/exit_roi_stop_loss.go | 11 +++++++---- pkg/bbgo/exit_roi_take_profit.go | 10 +++++++--- 3 files changed, 22 insertions(+), 11 deletions(-) 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 From 2567bd0caa2157bd49a0286ef69a782222c9a985 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 28 Feb 2024 15:02:57 +0800 Subject: [PATCH 2/2] set the defauinteralv alue to 1m --- pkg/bbgo/exit_protective_stop_loss.go | 3 +++ pkg/bbgo/exit_roi_stop_loss.go | 3 +++ pkg/bbgo/exit_roi_take_profit.go | 3 +++ pkg/bbgo/exit_trailing_stop.go | 3 +++ 4 files changed, 12 insertions(+) diff --git a/pkg/bbgo/exit_protective_stop_loss.go b/pkg/bbgo/exit_protective_stop_loss.go index 5e35858b0..a1de551ba 100644 --- a/pkg/bbgo/exit_protective_stop_loss.go +++ b/pkg/bbgo/exit_protective_stop_loss.go @@ -41,6 +41,9 @@ type ProtectiveStopLoss struct { } func (s *ProtectiveStopLoss) Subscribe(session *ExchangeSession) { + if s.Interval == "" { + s.Interval = types.Interval1m + } // use kline to handle roi stop session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval}) } diff --git a/pkg/bbgo/exit_roi_stop_loss.go b/pkg/bbgo/exit_roi_stop_loss.go index ad8d5689a..43b0431ce 100644 --- a/pkg/bbgo/exit_roi_stop_loss.go +++ b/pkg/bbgo/exit_roi_stop_loss.go @@ -21,6 +21,9 @@ type RoiStopLoss struct { func (s *RoiStopLoss) Subscribe(session *ExchangeSession) { // use kline to handle roi stop + if s.Interval == "" { + s.Interval = types.Interval1m + } session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval}) } diff --git a/pkg/bbgo/exit_roi_take_profit.go b/pkg/bbgo/exit_roi_take_profit.go index 60672f5e8..2efcba785 100644 --- a/pkg/bbgo/exit_roi_take_profit.go +++ b/pkg/bbgo/exit_roi_take_profit.go @@ -23,6 +23,9 @@ type RoiTakeProfit struct { func (s *RoiTakeProfit) Subscribe(session *ExchangeSession) { // use kline to handle roi stop + if s.Interval == "" { + s.Interval = types.Interval1m + } session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval}) } diff --git a/pkg/bbgo/exit_trailing_stop.go b/pkg/bbgo/exit_trailing_stop.go index a46024c34..5c6360af9 100644 --- a/pkg/bbgo/exit_trailing_stop.go +++ b/pkg/bbgo/exit_trailing_stop.go @@ -42,6 +42,9 @@ type TrailingStop2 struct { } func (s *TrailingStop2) Subscribe(session *ExchangeSession) { + if s.Interval == "" { + s.Interval = types.Interval1m + } // use 1m kline to handle roi stop session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval}) }