bbgo_origin/pkg/indicator/ma/sma.go

58 lines
1017 B
Go
Raw Normal View History

2020-10-18 04:23:00 +00:00
package ma
2020-10-18 03:37:01 +00:00
import (
"math"
"time"
2020-10-18 04:23:00 +00:00
"github.com/c9s/bbgo/pkg/store"
2020-10-18 03:37:01 +00:00
"github.com/c9s/bbgo/pkg/types"
)
2020-10-18 04:23:00 +00:00
type SMA struct {
store *store.MarketDataStore
2020-10-18 04:23:00 +00:00
Window int
2020-10-18 03:37:01 +00:00
}
2020-10-18 04:23:00 +00:00
func NewSMA(window int) *SMA {
return &SMA{
Window: window,
2020-10-18 03:37:01 +00:00
}
}
2020-10-18 04:23:00 +00:00
func (i *SMA) handleUpdate(kline types.KLine) {
klines, ok := i.store.KLineWindows[types.Interval(kline.Interval)]
2020-10-18 03:37:01 +00:00
if !ok {
return
}
2020-10-18 04:23:00 +00:00
if len(klines) < i.Window {
2020-10-18 03:37:01 +00:00
return
}
// calculate ma
}
type IndicatorValue struct {
Value float64
Time time.Time
}
func calculateMovingAverage(klines types.KLineWindow, period int) (values []IndicatorValue) {
for idx := range klines[period:] {
offset := idx + period
sum := klines[offset-period : offset].ReduceClose()
values = append(values, IndicatorValue{
Time: klines[offset].GetEndTime(),
Value: math.Round(sum / float64(period)),
})
}
return values
}
func (i *SMA) SubscribeStore(store *store.MarketDataStore) {
2020-10-18 03:37:01 +00:00
i.store = store
// register kline update callback
// store.OnUpdate(i.handleUpdate)
2020-10-18 03:37:01 +00:00
}