From e4bdb1de064bde4d298b88c10a05f3b0ba07018f Mon Sep 17 00:00:00 2001 From: Andy Cheng Date: Sun, 19 Dec 2021 18:28:47 +0800 Subject: [PATCH] strategy: allow setting the interval and the window for trigger MA --- config/support.yaml | 4 +++- doc/strategy/support.md | 10 +++++++--- pkg/strategy/support/strategy.go | 16 +++++++++------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/config/support.yaml b/config/support.yaml index e4e801116..7f1c19555 100644 --- a/config/support.yaml +++ b/config/support.yaml @@ -55,7 +55,9 @@ exchangeStrategies: symbol: LINKUSDT interval: 1m minVolume: 1_000 - movingAverageWindow: 99 + triggerMovingAverage: + interval: 5m + window: 99 longTermMovingAverage: interval: 1h window: 99 diff --git a/doc/strategy/support.md b/doc/strategy/support.md index e8bf60259..d9931c6c8 100644 --- a/doc/strategy/support.md +++ b/doc/strategy/support.md @@ -14,9 +14,13 @@ This strategy uses K-lines with high volume as support and buys the target asset - `minVolume` - The threshold, e.g., `1000000`, `5000000`. A K-line with volume larger than this is seen as a support, and triggers a market buy order. -- `movingAverageWindow` - - The MA window in the current K-line interval to filter out noises, e.g., 99. The closed price must be below this - MA to trigger the buy order. +- `triggerMovingAverage` + - The MA window in the current K-line interval to filter out noises. The closed price must be below this MA to + trigger the buy order. + - `interval` + - The K-line interval, e.g., `5m`, `1h` + - `window` + - The MA window in the specified K-line interval to filter out noises. - `longTermMovingAverage` - The MA window in a longer K-line interval. The closed price must be above this MA to trigger the buy order. - `interval` diff --git a/pkg/strategy/support/strategy.go b/pkg/strategy/support/strategy.go index b2abcaa41..be68b35f9 100644 --- a/pkg/strategy/support/strategy.go +++ b/pkg/strategy/support/strategy.go @@ -96,7 +96,7 @@ type Strategy struct { Interval types.Interval `json:"interval"` // moving average window for checking support (support should be under the moving average line) - MovingAverageWindow int `json:"movingAverageWindow"` + TriggerMovingAverage types.IntervalWindow `json:"triggerMovingAverage"` // LongTermMovingAverage is the second moving average line for checking support position LongTermMovingAverage types.IntervalWindow `json:"longTermMovingAverage"` @@ -148,6 +148,10 @@ func (s *Strategy) Validate() error { func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: string(s.Interval)}) + if s.TriggerMovingAverage != zeroiw { + session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: string(s.TriggerMovingAverage.Interval)}) + } + if s.LongTermMovingAverage != zeroiw { session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: string(s.LongTermMovingAverage.Interval)}) } @@ -261,10 +265,6 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.Interval = types.Interval5m } - if s.MovingAverageWindow == 0 { - s.MovingAverageWindow = 99 - } - if s.Sensitivity > 0 { volRange, err := s.ScaleQuantity.ByVolumeRule.Range() if err != nil { @@ -286,6 +286,10 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se return fmt.Errorf("standardIndicatorSet is nil, symbol %s", s.Symbol) } + if s.TriggerMovingAverage != zeroiw { + s.triggerEMA = standardIndicatorSet.EWMA(s.TriggerMovingAverage) + } + if s.LongTermMovingAverage != zeroiw { s.longTermEMA = standardIndicatorSet.EWMA(s.LongTermMovingAverage) } @@ -293,8 +297,6 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.orderStore = bbgo.NewOrderStore(s.Symbol) s.orderStore.BindStream(session.UserDataStream) - s.triggerEMA = standardIndicatorSet.EWMA(types.IntervalWindow{Interval: s.Interval, Window: s.MovingAverageWindow}) - if err := s.LoadState(); err != nil { return err } else {