bbgo_origin/pkg/bbgo/moving_average_settings.go

47 lines
1.0 KiB
Go
Raw Normal View History

package bbgo
import (
"fmt"
"github.com/c9s/bbgo/pkg/fixedpoint"
"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,
}
}
2021-08-26 03:46:02 +00:00
func (settings *MovingAverageSettings) Indicator(indicatorSet *StandardIndicatorSet) (inc types.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
}