From 1f94ae1c19b16a306c96bb2b5b89e3361d2d753e Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 26 Aug 2021 11:32:39 +0800 Subject: [PATCH] bbgo: move moving average settings struct into bbgo --- pkg/bbgo/moving_average_settings.go | 47 +++++++++++++++++++++++++++++ pkg/strategy/schedule/strategy.go | 45 ++------------------------- 2 files changed, 49 insertions(+), 43 deletions(-) create mode 100644 pkg/bbgo/moving_average_settings.go diff --git a/pkg/bbgo/moving_average_settings.go b/pkg/bbgo/moving_average_settings.go new file mode 100644 index 000000000..2b1f209bb --- /dev/null +++ b/pkg/bbgo/moving_average_settings.go @@ -0,0 +1,47 @@ +package bbgo + +import ( + "fmt" + "github.com/c9s/bbgo/pkg/fixedpoint" + "github.com/c9s/bbgo/pkg/strategy/schedule" + "github.com/c9s/bbgo/pkg/types" +) + +type MovingAverageSettings struct { + Type string `json:"type"` + Interval types.Interval `json:"interval"` + Window int `json:"window"` + + Side *types.SideType `json:"side"` + Quantity *fixedpoint.Value `json:"quantity"` + Amount *fixedpoint.Value `json:"amount"` +} + +func (settings MovingAverageSettings) IntervalWindow() types.IntervalWindow { + var window = 99 + if settings.Window > 0 { + window = settings.Window + } + + return types.IntervalWindow{ + Interval: settings.Interval, + Window: window, + } +} + +func (settings *MovingAverageSettings) Indicator(indicatorSet *StandardIndicatorSet) (inc schedule.Float64Indicator, err error) { + var iw = settings.IntervalWindow() + + switch settings.Type { + case "SMA": + inc = indicatorSet.SMA(iw) + + case "EWMA", "EMA": + inc = indicatorSet.EWMA(iw) + + default: + return nil, fmt.Errorf("unsupported moving average type: %s", settings.Type) + } + + return inc, nil +} diff --git a/pkg/strategy/schedule/strategy.go b/pkg/strategy/schedule/strategy.go index c15579a94..0ba01f1c8 100644 --- a/pkg/strategy/schedule/strategy.go +++ b/pkg/strategy/schedule/strategy.go @@ -2,8 +2,6 @@ package schedule import ( "context" - "fmt" - "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/pkg/errors" log "github.com/sirupsen/logrus" @@ -23,45 +21,6 @@ type Float64Indicator interface { Last() float64 } -type MovingAverageSettings struct { - Type string `json:"type"` - Interval types.Interval `json:"interval"` - Window int `json:"window"` - - Side *types.SideType `json:"side"` - Quantity *fixedpoint.Value `json:"quantity"` - Amount *fixedpoint.Value `json:"amount"` -} - -func (settings MovingAverageSettings) IntervalWindow() types.IntervalWindow { - var window = 99 - if settings.Window > 0 { - window = settings.Window - } - - return types.IntervalWindow{ - Interval: settings.Interval, - Window: window, - } -} - -func (settings *MovingAverageSettings) Indicator(indicatorSet *bbgo.StandardIndicatorSet) (inc Float64Indicator, err error) { - var iw = settings.IntervalWindow() - - switch settings.Type { - case "SMA": - inc = indicatorSet.SMA(iw) - - case "EWMA", "EMA": - inc = indicatorSet.EWMA(iw) - - default: - return nil, fmt.Errorf("unsupported moving average type: %s", settings.Type) - } - - return inc, nil -} - type Strategy struct { Market types.Market @@ -85,9 +44,9 @@ type Strategy struct { Amount fixedpoint.Value `json:"amount,omitempty"` - BelowMovingAverage *MovingAverageSettings `json:"belowMovingAverage,omitempty"` + BelowMovingAverage *bbgo.MovingAverageSettings `json:"belowMovingAverage,omitempty"` - AboveMovingAverage *MovingAverageSettings `json:"aboveMovingAverage,omitempty"` + AboveMovingAverage *bbgo.MovingAverageSettings `json:"aboveMovingAverage,omitempty"` } func (s *Strategy) ID() string {