Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: [exit] Use configuration instead of kine fixed interval #1549

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions pkg/bbgo/exit_protective_stop_loss.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,22 @@ 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"`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you set the default value to 1m if it’s empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


session *ExchangeSession
orderExecutor *GeneralOrderExecutor
stopLossPrice fixedpoint.Value
stopLossOrder *types.Order
}

func (s *ProtectiveStopLoss) Subscribe(session *ExchangeSession) {
// use 1m kline to handle roi stop
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: types.Interval1m})
if s.Interval == "" {
s.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 {
Expand Down Expand Up @@ -131,8 +138,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) {
Expand Down
14 changes: 10 additions & 4 deletions pkg/bbgo/exit_roi_stop_loss.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ 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
if s.Interval == "" {
s.Interval = types.Interval1m
}
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
}

func (s *RoiStopLoss) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExecutor) {
Expand All @@ -30,8 +36,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) {
Expand Down
13 changes: 10 additions & 3 deletions pkg/bbgo/exit_roi_take_profit.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,28 @@ 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
if s.Interval == "" {
s.Interval = types.Interval1m
}
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
}

func (s *RoiTakeProfit) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExecutor) {
s.session = session
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
Expand Down
3 changes: 3 additions & 0 deletions pkg/bbgo/exit_trailing_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
Expand Down
Loading