Merge pull request #1549 from anywhy/fix_exit_interval

This commit is contained in:
c9s 2024-03-05 00:33:35 +08:00 committed by GitHub
commit ca5f31b311
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 34 additions and 11 deletions

View File

@ -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,11 @@ 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})
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 {
@ -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) {

View File

@ -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) {
@ -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) {

View File

@ -13,13 +13,20 @@ 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) {
@ -27,7 +34,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

View File

@ -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})
}