bbgo: trigger price check when kline is updated (not just closed)

This commit is contained in:
c9s 2023-06-21 17:58:18 +08:00
parent fdf2a91604
commit ac1b5aa0e2
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 10 additions and 4 deletions

View File

@ -124,14 +124,17 @@ func (s *ProtectiveStopLoss) Bind(session *ExchangeSession, orderExecutor *Gener
}) })
position := orderExecutor.Position() position := orderExecutor.Position()
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, types.Interval1m, func(kline types.KLine) {
f := func(kline types.KLine) {
isPositionOpened := !position.IsClosed() && !position.IsDust(kline.Close) isPositionOpened := !position.IsClosed() && !position.IsDust(kline.Close)
if isPositionOpened { if isPositionOpened {
s.handleChange(context.Background(), position, kline.Close, s.orderExecutor) s.handleChange(context.Background(), position, kline.Close, s.orderExecutor)
} else { } else {
s.stopLossPrice = fixedpoint.Zero s.stopLossPrice = fixedpoint.Zero
} }
})) }
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, types.Interval1m, f))
session.MarketDataStream.OnKLine(types.KLineWith(s.Symbol, types.Interval1m, f))
if !IsBackTesting && enableMarketTradeStop { if !IsBackTesting && enableMarketTradeStop {
session.MarketDataStream.OnMarketTrade(func(trade types.Trade) { session.MarketDataStream.OnMarketTrade(func(trade types.Trade) {

View File

@ -26,9 +26,12 @@ func (s *RoiStopLoss) Bind(session *ExchangeSession, orderExecutor *GeneralOrder
s.orderExecutor = orderExecutor s.orderExecutor = orderExecutor
position := orderExecutor.Position() position := orderExecutor.Position()
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, types.Interval1m, func(kline types.KLine) { f := func(kline types.KLine) {
s.checkStopPrice(kline.Close, position) 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))
if !IsBackTesting && enableMarketTradeStop { if !IsBackTesting && enableMarketTradeStop {
session.MarketDataStream.OnMarketTrade(func(trade types.Trade) { session.MarketDataStream.OnMarketTrade(func(trade types.Trade) {