Skip to content

Commit

Permalink
strategy: allow setting the interval and the window for trigger MA
Browse files Browse the repository at this point in the history
  • Loading branch information
andycheng123 committed Dec 19, 2021
1 parent 7c85f8c commit e4bdb1d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
4 changes: 3 additions & 1 deletion config/support.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ exchangeStrategies:
symbol: LINKUSDT
interval: 1m
minVolume: 1_000
movingAverageWindow: 99
triggerMovingAverage:
interval: 5m
window: 99
longTermMovingAverage:
interval: 1h
window: 99
Expand Down
10 changes: 7 additions & 3 deletions doc/strategy/support.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
16 changes: 9 additions & 7 deletions pkg/strategy/support/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)})
}
Expand Down Expand Up @@ -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 {
Expand All @@ -286,15 +286,17 @@ 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)
}

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 {
Expand Down

0 comments on commit e4bdb1d

Please sign in to comment.