mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-27 17:25:16 +00:00
47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
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,
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|