strategy: allow setting the interval and the window for trigger MA

This commit is contained in:
Andy Cheng 2021-12-19 18:28:47 +08:00
parent 7c85f8caa4
commit e4bdb1de06
No known key found for this signature in database
GPG Key ID: 936427CF651A9D28
3 changed files with 19 additions and 11 deletions

View File

@ -55,7 +55,9 @@ exchangeStrategies:
symbol: LINKUSDT symbol: LINKUSDT
interval: 1m interval: 1m
minVolume: 1_000 minVolume: 1_000
movingAverageWindow: 99 triggerMovingAverage:
interval: 5m
window: 99
longTermMovingAverage: longTermMovingAverage:
interval: 1h interval: 1h
window: 99 window: 99

View File

@ -14,9 +14,13 @@ This strategy uses K-lines with high volume as support and buys the target asset
- `minVolume` - `minVolume`
- The threshold, e.g., `1000000`, `5000000`. A K-line with volume larger than this is seen as a support, and - 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. triggers a market buy order.
- `movingAverageWindow` - `triggerMovingAverage`
- The MA window in the current K-line interval to filter out noises, e.g., 99. The closed price must be below this - The MA window in the current K-line interval to filter out noises. The closed price must be below this MA to
MA to trigger the buy order. 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` - `longTermMovingAverage`
- The MA window in a longer K-line interval. The closed price must be above this MA to trigger the buy order. - The MA window in a longer K-line interval. The closed price must be above this MA to trigger the buy order.
- `interval` - `interval`

View File

@ -96,7 +96,7 @@ type Strategy struct {
Interval types.Interval `json:"interval"` Interval types.Interval `json:"interval"`
// moving average window for checking support (support should be under the moving average line) // 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 is the second moving average line for checking support position
LongTermMovingAverage types.IntervalWindow `json:"longTermMovingAverage"` LongTermMovingAverage types.IntervalWindow `json:"longTermMovingAverage"`
@ -148,6 +148,10 @@ func (s *Strategy) Validate() error {
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: string(s.Interval)}) 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 { if s.LongTermMovingAverage != zeroiw {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: string(s.LongTermMovingAverage.Interval)}) 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 s.Interval = types.Interval5m
} }
if s.MovingAverageWindow == 0 {
s.MovingAverageWindow = 99
}
if s.Sensitivity > 0 { if s.Sensitivity > 0 {
volRange, err := s.ScaleQuantity.ByVolumeRule.Range() volRange, err := s.ScaleQuantity.ByVolumeRule.Range()
if err != nil { 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) return fmt.Errorf("standardIndicatorSet is nil, symbol %s", s.Symbol)
} }
if s.TriggerMovingAverage != zeroiw {
s.triggerEMA = standardIndicatorSet.EWMA(s.TriggerMovingAverage)
}
if s.LongTermMovingAverage != zeroiw { if s.LongTermMovingAverage != zeroiw {
s.longTermEMA = standardIndicatorSet.EWMA(s.LongTermMovingAverage) 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 = bbgo.NewOrderStore(s.Symbol)
s.orderStore.BindStream(session.UserDataStream) s.orderStore.BindStream(session.UserDataStream)
s.triggerEMA = standardIndicatorSet.EWMA(types.IntervalWindow{Interval: s.Interval, Window: s.MovingAverageWindow})
if err := s.LoadState(); err != nil { if err := s.LoadState(); err != nil {
return err return err
} else { } else {