From a8134561f5263d1a3d95f5b32f6465efedec0d8e Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 9 Jun 2022 18:16:32 +0800 Subject: [PATCH] pivotshort: add stopEMA --- config/pivotshort-ETHUSDT.yaml | 5 ++++- pkg/strategy/pivotshort/strategy.go | 24 ++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/config/pivotshort-ETHUSDT.yaml b/config/pivotshort-ETHUSDT.yaml index 1bd3de852..1186ecb8c 100644 --- a/config/pivotshort-ETHUSDT.yaml +++ b/config/pivotshort-ETHUSDT.yaml @@ -18,7 +18,10 @@ exchangeStrategies: breakLow: ratio: 0.1% quantity: 10.0 - # stopLossPercentage: 1% + stopEMARange: 5% + stopEMA: + interval: 1h + window: 99 bounceShort: quantity: 10.0 diff --git a/pkg/strategy/pivotshort/strategy.go b/pkg/strategy/pivotshort/strategy.go index ed9c896fc..5e64589de 100644 --- a/pkg/strategy/pivotshort/strategy.go +++ b/pkg/strategy/pivotshort/strategy.go @@ -26,8 +26,10 @@ type IntervalWindowSetting struct { // BreakLow -- when price breaks the previous pivot low, we set a trade entry type BreakLow struct { - Ratio fixedpoint.Value `json:"ratio"` - Quantity fixedpoint.Value `json:"quantity"` + Ratio fixedpoint.Value `json:"ratio"` + Quantity fixedpoint.Value `json:"quantity"` + StopEMARange fixedpoint.Value `json:"stopEMARange"` + StopEMA *types.IntervalWindow `json:"stopEMA"` } type Entry struct { @@ -76,6 +78,7 @@ type Strategy struct { lastLow fixedpoint.Value pivot *indicator.Pivot + ewma *indicator.EWMA pivotLowPrices []fixedpoint.Value // StrategyController @@ -210,6 +213,11 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.pivot = &indicator.Pivot{IntervalWindow: iw} s.pivot.Bind(store) + standardIndicator, _ := session.StandardIndicatorSet(s.Symbol) + if s.BreakLow.StopEMA != nil { + s.ewma = standardIndicator.EWMA(*s.BreakLow.StopEMA) + } + s.lastLow = fixedpoint.Zero session.UserDataStream.OnStart(func() { @@ -281,6 +289,18 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.pivotLowPrices = s.pivotLowPrices[len(s.pivotLowPrices)-10:] } + if s.ewma != nil && !s.BreakLow.StopEMARange.IsZero() { + ema := fixedpoint.NewFromFloat(s.ewma.Last()) + if ema.IsZero() { + return + } + + emaStopShortPrice := ema.Mul(fixedpoint.One.Sub(s.BreakLow.StopEMARange)) + if kline.Close.Compare(emaStopShortPrice) < 0 { + return + } + } + ratio := fixedpoint.One.Sub(s.BreakLow.Ratio) breakPrice := previousLow.Mul(ratio) if kline.Close.Compare(breakPrice) > 0 {