From 2e00e584421b9235713295fd4e063960afc70cbb Mon Sep 17 00:00:00 2001 From: Andy Cheng Date: Thu, 16 Mar 2023 18:39:27 +0800 Subject: [PATCH] exits/hhllstop: add hhllstop to exits --- pkg/bbgo/exit.go | 20 +++++++++++++++----- pkg/bbgo/exit_hh_ll_stop.go | 14 +++++++------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/pkg/bbgo/exit.go b/pkg/bbgo/exit.go index e4a59e6e0..aedc936d6 100644 --- a/pkg/bbgo/exit.go +++ b/pkg/bbgo/exit.go @@ -29,16 +29,17 @@ func (s *ExitMethodSet) Bind(session *ExchangeSession, orderExecutor *GeneralOrd } type ExitMethod struct { - RoiStopLoss *RoiStopLoss `json:"roiStopLoss"` - ProtectiveStopLoss *ProtectiveStopLoss `json:"protectiveStopLoss"` - RoiTakeProfit *RoiTakeProfit `json:"roiTakeProfit"` - TrailingStop *TrailingStop2 `json:"trailingStop"` + RoiStopLoss *RoiStopLoss `json:"roiStopLoss"` + ProtectiveStopLoss *ProtectiveStopLoss `json:"protectiveStopLoss"` + RoiTakeProfit *RoiTakeProfit `json:"roiTakeProfit"` + TrailingStop *TrailingStop2 `json:"trailingStop"` + HigherHighLowerLowStop *HigherHighLowerLowStop `json:"higherHighLowerLowStopLoss"` // Exit methods for short positions // ================================================= LowerShadowTakeProfit *LowerShadowTakeProfit `json:"lowerShadowTakeProfit"` CumulatedVolumeTakeProfit *CumulatedVolumeTakeProfit `json:"cumulatedVolumeTakeProfit"` - SupportTakeProfit *SupportTakeProfit `json:"supportTakeProfit"` + SupportTakeProfit *SupportTakeProfit `json:"supportTakeProfit"` } func (e ExitMethod) String() string { @@ -78,6 +79,11 @@ func (e ExitMethod) String() string { buf.WriteString("supportTakeProfit: " + string(b) + ", ") } + if e.HigherHighLowerLowStop != nil { + b, _ := json.Marshal(e.HigherHighLowerLowStop) + buf.WriteString("hhllStop: " + string(b) + ", ") + } + return buf.String() } @@ -135,4 +141,8 @@ func (m *ExitMethod) Bind(session *ExchangeSession, orderExecutor *GeneralOrderE if m.TrailingStop != nil { m.TrailingStop.Bind(session, orderExecutor) } + + if m.HigherHighLowerLowStop != nil { + m.HigherHighLowerLowStop.Bind(session, orderExecutor) + } } diff --git a/pkg/bbgo/exit_hh_ll_stop.go b/pkg/bbgo/exit_hh_ll_stop.go index 7c3c0270d..0b3d57fdd 100644 --- a/pkg/bbgo/exit_hh_ll_stop.go +++ b/pkg/bbgo/exit_hh_ll_stop.go @@ -8,7 +8,7 @@ import ( "github.com/c9s/bbgo/pkg/types" ) -type HigherHighLowerLowStopLoss struct { +type HigherHighLowerLowStop struct { Symbol string `json:"symbol"` Side types.SideType `json:"side"` @@ -45,14 +45,14 @@ type HigherHighLowerLowStopLoss struct { } // Subscribe required k-line stream -func (s *HigherHighLowerLowStopLoss) Subscribe(session *ExchangeSession) { +func (s *HigherHighLowerLowStop) Subscribe(session *ExchangeSession) { // use 1m kline to handle roi stop session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval}) } // updateActivated checks the position cost against the close price, activation ratio, and deactivation ratio to // determine whether this stop should be activated -func (s *HigherHighLowerLowStopLoss) updateActivated(position *types.Position, closePrice fixedpoint.Value) { +func (s *HigherHighLowerLowStop) updateActivated(position *types.Position, closePrice fixedpoint.Value) { if position.IsClosed() || position.IsDust(closePrice) { s.activated = false } else if s.activated { @@ -88,7 +88,7 @@ func (s *HigherHighLowerLowStopLoss) updateActivated(position *types.Position, c } } -func (s *HigherHighLowerLowStopLoss) updateHighLowNumber(kline types.KLine) { +func (s *HigherHighLowerLowStop) updateHighLowNumber(kline types.KLine) { if !s.activated { s.reset() return @@ -116,7 +116,7 @@ func (s *HigherHighLowerLowStopLoss) updateHighLowNumber(kline types.KLine) { s.klines.Truncate(s.Window - 1) } -func (s *HigherHighLowerLowStopLoss) shouldStop(position *types.Position) bool { +func (s *HigherHighLowerLowStop) shouldStop(position *types.Position) bool { if s.activated { highs := 0 lows := 0 @@ -146,12 +146,12 @@ func (s *HigherHighLowerLowStopLoss) shouldStop(position *types.Position) bool { return false } -func (s *HigherHighLowerLowStopLoss) reset() { +func (s *HigherHighLowerLowStop) reset() { s.highLows = []types.Direction{} s.klines.Truncate(0) } -func (s *HigherHighLowerLowStopLoss) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExecutor) { +func (s *HigherHighLowerLowStop) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExecutor) { s.session = session s.orderExecutor = orderExecutor