bbgo: use types.KLineWith for roi take profit

This commit is contained in:
c9s 2022-08-10 23:45:21 +08:00
parent f655881a17
commit 677b122964
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -26,22 +26,18 @@ func (s *RoiTakeProfit) Bind(session *ExchangeSession, orderExecutor *GeneralOrd
s.orderExecutor = orderExecutor
position := orderExecutor.Position()
session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {
if kline.Symbol != position.Symbol || kline.Interval != types.Interval1m {
return
}
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 {
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())
_ = orderExecutor.ClosePosition(context.Background(), fixedpoint.One, "roiTakeProfit")
return
}
})
}))
}