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
interval: 1m
minVolume: 1_000
movingAverageWindow: 99
triggerMovingAverage:
interval: 5m
window: 99
longTermMovingAverage:
interval: 1h
window: 99

View File

@ -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`

View File

@ -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 {