bbgo_origin/pkg/bbgo/exit_roi_take_profit.go

48 lines
1.5 KiB
Go
Raw Permalink Normal View History

package bbgo
import (
"context"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
// RoiTakeProfit force takes the profit by the given ROI percentage.
type RoiTakeProfit struct {
Symbol string `json:"symbol"`
Percentage fixedpoint.Value `json:"percentage"`
CancelActiveOrders bool `json:"cancelActiveOrders"`
session *ExchangeSession
orderExecutor *GeneralOrderExecutor
}
2022-07-19 03:00:45 +00:00
func (s *RoiTakeProfit) Subscribe(session *ExchangeSession) {
// use 1m kline to handle roi stop
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: types.Interval1m})
}
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) {
closePrice := kline.Close
if position.IsClosed() || position.IsDust(closePrice) {
return
}
roi := position.ROI(closePrice)
if roi.Compare(s.Percentage) >= 0 {
// stop loss
Notify("[RoiTakeProfit] %s take profit is triggered by ROI %s/%s, price: %f", position.Symbol, roi.Percentage(), s.Percentage.Percentage(), kline.Close.Float64())
if s.CancelActiveOrders {
_ = s.orderExecutor.GracefulCancel(context.Background())
}
2022-06-27 10:17:57 +00:00
_ = orderExecutor.ClosePosition(context.Background(), fixedpoint.One, "roiTakeProfit")
return
}
}))
}