diff --git a/pkg/bbgo/exit_hh_ll_stop.go b/pkg/bbgo/exit_hh_ll_stop.go index 64069dcab..b29c20d20 100644 --- a/pkg/bbgo/exit_hh_ll_stop.go +++ b/pkg/bbgo/exit_hh_ll_stop.go @@ -61,6 +61,7 @@ func (s *HigherHighLowerLowStop) Subscribe(session *ExchangeSession) { // updateActivated checks the position cost against the close price, activation ratio, and deactivation ratio to // determine whether this stop should be activated func (s *HigherHighLowerLowStop) updateActivated(position *types.Position, closePrice fixedpoint.Value) { + // deactivate when no position if position.IsClosed() || position.IsDust(closePrice) { s.activated = false @@ -68,19 +69,27 @@ func (s *HigherHighLowerLowStop) updateActivated(position *types.Position, close } + // activation/deactivation price + var price_deactive fixedpoint.Value + var price_active fixedpoint.Value + if position.IsLong() { + price_deactive = position.AverageCost.Mul(fixedpoint.One.Add(s.DeactivationRatio)) + price_active = position.AverageCost.Mul(fixedpoint.One.Add(s.ActivationRatio)) + } else { + price_deactive = position.AverageCost.Mul(fixedpoint.One.Sub(s.DeactivationRatio)) + price_active = position.AverageCost.Mul(fixedpoint.One.Sub(s.ActivationRatio)) + } + if s.activated { if position.IsLong() { - r_deactive := fixedpoint.One.Add(s.DeactivationRatio) - r_active := fixedpoint.One.Add(s.ActivationRatio) - - if closePrice.Compare(position.AverageCost.Mul(r_deactive)) >= 0 { + if closePrice.Compare(price_deactive) >= 0 { s.activated = false Notify("[hhllStop] Stop of %s deactivated for long position, deactivation ratio %s", s.Symbol, s.DeactivationRatio.Percentage()) - } else if closePrice.Compare(position.AverageCost.Mul(r_active)) < 0 { + } else if closePrice.Compare(price_active) < 0 { s.activated = false Notify("[hhllStop] Stop of %s deactivated for long position, activation ratio %s", s.Symbol, s.ActivationRatio.Percentage()) @@ -89,16 +98,13 @@ func (s *HigherHighLowerLowStop) updateActivated(position *types.Position, close } else if position.IsShort() { - r_deactive := fixedpoint.One.Sub(s.DeactivationRatio) - r_active := fixedpoint.One.Sub(s.ActivationRatio) - // for short position, if the close price is less than the activation price then this is a profit position. - if closePrice.Compare(position.AverageCost.Mul(r_deactive)) <= 0 { + if closePrice.Compare(price_deactive) <= 0 { s.activated = false Notify("[hhllStop] Stop of %s deactivated for short position, deactivation ratio %s", s.Symbol, s.DeactivationRatio.Percentage()) - } else if closePrice.Compare(position.AverageCost.Mul(r_active)) > 0 { + } else if closePrice.Compare(price_active) > 0 { s.activated = false Notify("[hhllStop] Stop of %s deactivated for short position, activation ratio %s", s.Symbol, s.ActivationRatio.Percentage()) @@ -110,10 +116,7 @@ func (s *HigherHighLowerLowStop) updateActivated(position *types.Position, close if position.IsLong() { - r_deactive := fixedpoint.One.Add(s.DeactivationRatio) - r_active := fixedpoint.One.Add(s.ActivationRatio) - - if closePrice.Compare(position.AverageCost.Mul(r_active)) >= 0 && closePrice.Compare(position.AverageCost.Mul(r_deactive)) < 0 { + if closePrice.Compare(price_active) >= 0 && closePrice.Compare(price_deactive) < 0 { s.activated = true Notify("[hhllStop] %s stop is activated for long position, activation ratio %s, deactivation ratio %s", s.Symbol, s.ActivationRatio.Percentage(), s.DeactivationRatio.Percentage()) @@ -122,11 +125,8 @@ func (s *HigherHighLowerLowStop) updateActivated(position *types.Position, close } else if position.IsShort() { - r_deactive := fixedpoint.One.Sub(s.DeactivationRatio) - r_active := fixedpoint.One.Sub(s.ActivationRatio) - // for short position, if the close price is less than the activation price then this is a profit position. - if closePrice.Compare(position.AverageCost.Mul(r_active)) <= 0 && closePrice.Compare(position.AverageCost.Mul(r_deactive)) > 0 { + if closePrice.Compare(price_active) <= 0 && closePrice.Compare(price_deactive) > 0 { s.activated = true Notify("[hhllStop] %s stop is activated for short position, activation ratio %s, deactivation ratio %s", s.Symbol, s.ActivationRatio.Percentage(), s.DeactivationRatio.Percentage())