mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 16:25:16 +00:00
bbgo: move moving average settings struct into bbgo
This commit is contained in:
parent
e8f0cbcff8
commit
1f94ae1c19
47
pkg/bbgo/moving_average_settings.go
Normal file
47
pkg/bbgo/moving_average_settings.go
Normal 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
|
||||||
|
}
|
|
@ -2,8 +2,6 @@ package schedule
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
@ -23,45 +21,6 @@ type Float64Indicator interface {
|
||||||
Last() float64
|
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 {
|
type Strategy struct {
|
||||||
Market types.Market
|
Market types.Market
|
||||||
|
|
||||||
|
@ -85,9 +44,9 @@ type Strategy struct {
|
||||||
|
|
||||||
Amount fixedpoint.Value `json:"amount,omitempty"`
|
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 {
|
func (s *Strategy) ID() string {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user