Merge pull request #379 from andycheng123/main

strategy: support strategy update
This commit is contained in:
Yo-An Lin 2021-12-21 17:27:31 +08:00 committed by GitHub
commit e2367648b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 14 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,10 +14,15 @@ 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 support higher than the MA is seen as invalid
- `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 MA window in a longer K-line interval. The closed price must be above this MA to trigger the buy order.
- `interval`
- The K-line interval, e.g., `5m`, `1h`
- `window`

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 {
@ -342,8 +344,8 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
}
}
if s.longTermEMA != nil && closePriceF > s.longTermEMA.Last() {
s.Notify("%s: closed price is above the long term moving average line %f, skipping this support",
if s.longTermEMA != nil && closePriceF < s.longTermEMA.Last() {
s.Notify("%s: closed price is below the long term moving average line %f, skipping this support",
s.Symbol,
s.longTermEMA.Last(),
kline,
@ -351,10 +353,20 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
return
}
s.Notify("Found %s support: the close price %f is under EMA %f and volume %f > minimum volume %f",
if s.triggerEMA != nil && closePriceF > s.triggerEMA.Last() {
s.Notify("%s: closed price is above the trigger moving average line %f, skipping this support",
s.Symbol,
s.triggerEMA.Last(),
kline,
)
return
}
s.Notify("Found %s support: the close price %f is below trigger EMA %f and above long term EMA %f and volume %f > minimum volume %f",
s.Symbol,
closePrice.Float64(),
s.triggerEMA.Last(),
s.longTermEMA.Last(),
kline.Volume,
s.MinVolume.Float64(),
kline)