From b0cc128b795a1c97d7ce1674b39f1d0166fc7345 Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 29 Oct 2020 17:03:36 +0800 Subject: [PATCH] pull out trend types --- pkg/strategy/swing/strategy.go | 4 ++-- pkg/types/kline.go | 15 ++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/pkg/strategy/swing/strategy.go b/pkg/strategy/swing/strategy.go index b6c4a466f..9c8cf1782 100644 --- a/pkg/strategy/swing/strategy.go +++ b/pkg/strategy/swing/strategy.go @@ -121,7 +121,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se trend := kline.GetTrend() switch trend { - case 1: + case types.TrendUp: // if it goes up and it's above the moving average price, then we sell if closePrice > movingAveragePrice { s.notify(":chart_with_upwards_trend: closePrice %f is above movingAveragePrice %f, submitting SELL order", closePrice, movingAveragePrice) @@ -137,7 +137,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se log.WithError(err).Error("submit order error") } } - case -1: + case types.TrendDown: // if it goes down and it's below the moving average price, then we buy if closePrice < movingAveragePrice { s.notify(":chart_with_downwards_trend: closePrice %f is below movingAveragePrice %f, submitting BUY order", closePrice, movingAveragePrice) diff --git a/pkg/types/kline.go b/pkg/types/kline.go index 6061cd43e..d4c1f4ebb 100644 --- a/pkg/types/kline.go +++ b/pkg/types/kline.go @@ -10,6 +10,12 @@ import ( "github.com/c9s/bbgo/pkg/util" ) +type Trend int + +const TrendUp = 1 +const TrendFlat = 0 +const TrendDown = -1 + type KLineOrWindow interface { GetInterval() string GetTrend() int @@ -87,16 +93,16 @@ func (k KLine) BounceDown() bool { return trend > 0 && k.Open < mid && k.Close < mid } -func (k KLine) GetTrend() int { +func (k KLine) GetTrend() Trend { o := k.GetOpen() c := k.GetClose() if c > o { - return 1 + return TrendUp } else if c < o { - return -1 + return TrendDown } - return 0 + return TrendFlat } func (k KLine) GetHigh() float64 { @@ -442,4 +448,3 @@ func (k KLineWindow) SlackAttachment() slack.Attachment { } type KLineCallback func(kline KLine) -