From 574e142cf93a150c3dc00e88a15ef1ede0aba930 Mon Sep 17 00:00:00 2001 From: Andy Cheng Date: Fri, 8 Jul 2022 16:42:31 +0800 Subject: [PATCH 1/2] strategy/supertrend: use types.IntervalWindow instead of types.Interval --- config/supertrend.yaml | 12 +++++------- pkg/strategy/supertrend/double_dema.go | 8 ++++---- pkg/strategy/supertrend/strategy.go | 17 +++++++---------- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/config/supertrend.yaml b/config/supertrend.yaml index 8f856b3d9..e5b26e6a4 100644 --- a/config/supertrend.yaml +++ b/config/supertrend.yaml @@ -38,6 +38,11 @@ exchangeStrategies: # interval is how long do you want to update your order price and quantity interval: 5m + # ATR window used by Supertrend + window: 49 + # ATR Multiplier for calculating super trend prices, the higher, the stronger the trends are + supertrendMultiplier: 4 + # leverage is the leverage of the orders leverage: 1.0 @@ -45,13 +50,6 @@ exchangeStrategies: fastDEMAWindow: 144 slowDEMAWindow: 169 - # Supertrend indicator parameters - superTrend: - # ATR window used by Supertrend - averageTrueRangeWindow: 49 - # ATR Multiplier for calculating super trend prices, the higher, the stronger the trends are - averageTrueRangeMultiplier: 4 - # Use linear regression as trend confirmation linearRegression: interval: 5m diff --git a/pkg/strategy/supertrend/double_dema.go b/pkg/strategy/supertrend/double_dema.go index 3f454dab7..fa40a1be6 100644 --- a/pkg/strategy/supertrend/double_dema.go +++ b/pkg/strategy/supertrend/double_dema.go @@ -7,7 +7,7 @@ import ( ) type DoubleDema struct { - Interval types.Interval `json:"interval"` + DemaInterval types.Interval `json:"demaInterval"` // FastDEMAWindow DEMA window for checking breakout FastDEMAWindow int `json:"fastDEMAWindow"` @@ -46,19 +46,19 @@ func (dd *DoubleDema) preloadDema(kLineStore *bbgo.MarketDataStore) { // setupDoubleDema initializes double DEMA indicators func (dd *DoubleDema) setupDoubleDema(kLineStore *bbgo.MarketDataStore, interval types.Interval) { - dd.Interval = interval + dd.DemaInterval = interval // DEMA if dd.FastDEMAWindow == 0 { dd.FastDEMAWindow = 144 } - dd.fastDEMA = &indicator.DEMA{IntervalWindow: types.IntervalWindow{Interval: dd.Interval, Window: dd.FastDEMAWindow}} + dd.fastDEMA = &indicator.DEMA{IntervalWindow: types.IntervalWindow{Interval: dd.DemaInterval, Window: dd.FastDEMAWindow}} dd.fastDEMA.Bind(kLineStore) if dd.SlowDEMAWindow == 0 { dd.SlowDEMAWindow = 169 } - dd.slowDEMA = &indicator.DEMA{IntervalWindow: types.IntervalWindow{Interval: dd.Interval, Window: dd.SlowDEMAWindow}} + dd.slowDEMA = &indicator.DEMA{IntervalWindow: types.IntervalWindow{Interval: dd.DemaInterval, Window: dd.SlowDEMAWindow}} dd.slowDEMA.Bind(kLineStore) dd.preloadDema(kLineStore) diff --git a/pkg/strategy/supertrend/strategy.go b/pkg/strategy/supertrend/strategy.go index 91817d200..105bebf77 100644 --- a/pkg/strategy/supertrend/strategy.go +++ b/pkg/strategy/supertrend/strategy.go @@ -39,17 +39,13 @@ type Strategy struct { // Symbol is the market symbol you want to trade Symbol string `json:"symbol"` + types.IntervalWindow + // Double DEMA DoubleDema - // Interval is how long do you want to update your order price and quantity - Interval types.Interval `json:"interval"` - // SuperTrend indicator - // SuperTrend SuperTrend `json:"superTrend"` Supertrend *indicator.Supertrend - // SupertrendWindow ATR window for calculation of supertrend - SupertrendWindow int `json:"supertrendWindow"` // SupertrendMultiplier ATR multiplier for calculation of supertrend SupertrendMultiplier float64 `json:"supertrendMultiplier"` @@ -112,6 +108,7 @@ func (s *Strategy) Validate() error { func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval}) + session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.LinearRegression.Interval}) } // Position control @@ -168,14 +165,14 @@ func (s *Strategy) setupIndicators() { s.setupDoubleDema(kLineStore, s.Interval) // Supertrend - if s.SupertrendWindow == 0 { - s.SupertrendWindow = 39 + if s.Window == 0 { + s.Window = 39 } if s.SupertrendMultiplier == 0 { s.SupertrendMultiplier = 3 } - s.Supertrend = &indicator.Supertrend{IntervalWindow: types.IntervalWindow{Window: s.SupertrendWindow, Interval: s.Interval}, ATRMultiplier: s.SupertrendMultiplier} - s.Supertrend.AverageTrueRange = &indicator.ATR{IntervalWindow: types.IntervalWindow{Window: s.SupertrendWindow, Interval: s.Interval}} + s.Supertrend = &indicator.Supertrend{IntervalWindow: types.IntervalWindow{Window: s.Window, Interval: s.Interval}, ATRMultiplier: s.SupertrendMultiplier} + s.Supertrend.AverageTrueRange = &indicator.ATR{IntervalWindow: types.IntervalWindow{Window: s.Window, Interval: s.Interval}} s.Supertrend.Bind(kLineStore) preloadSupertrend(s.Supertrend, kLineStore) From 761ed101105e40e619baec914ad1cd17233158b8 Mon Sep 17 00:00:00 2001 From: Andy Cheng Date: Fri, 8 Jul 2022 17:15:38 +0800 Subject: [PATCH 2/2] strategy/supertrend: update config --- config/supertrend.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/config/supertrend.yaml b/config/supertrend.yaml index e5b26e6a4..b0098b089 100644 --- a/config/supertrend.yaml +++ b/config/supertrend.yaml @@ -74,3 +74,7 @@ exchangeStrategies: # roiStopLoss is the stop loss percentage of the position ROI (currently the price change) - roiStopLoss: percentage: 4% + - protectiveStopLoss: + activationRatio: 3% + stopLossRatio: 2% + placeStopOrder: false