bbgo: move moving average settings struct into bbgo

This commit is contained in:
c9s 2021-08-26 11:32:39 +08:00
parent e8f0cbcff8
commit 1f94ae1c19
2 changed files with 49 additions and 43 deletions

View File

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

View File

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