bbgo_origin/pkg/indicator/sma.go

131 lines
2.4 KiB
Go
Raw Normal View History

2020-10-28 01:13:57 +00:00
package indicator
import (
2020-12-05 05:04:32 +00:00
"fmt"
"math"
2020-10-28 01:13:57 +00:00
"time"
2020-12-05 05:04:32 +00:00
log "github.com/sirupsen/logrus"
2020-10-28 01:13:57 +00:00
"github.com/c9s/bbgo/pkg/types"
)
type Float64Slice []float64
func (s *Float64Slice) Push(v float64) {
*s = append(*s, v)
}
func (s *Float64Slice) Pop(i int64) (v float64) {
v = (*s)[i]
*s = append((*s)[:i], (*s)[i+1:]...)
return v
}
func (s Float64Slice) Max() float64 {
m := -math.MaxFloat64
for _, v := range s {
m = math.Max(m, v)
}
return m
}
func (s Float64Slice) Min() float64 {
m := math.MaxFloat64
for _, v := range s {
m = math.Min(m, v)
}
return m
}
func (s Float64Slice) Sum() (sum float64) {
for _, v := range s {
sum += v
}
return sum
}
func (s Float64Slice) Mean() (mean float64) {
return s.Sum() / float64(len(s))
}
func (s Float64Slice) Tail(size int) Float64Slice {
length := len(s)
if length <= size {
win := make(Float64Slice, length)
copy(win, s)
return win
}
win := make(Float64Slice, size)
2021-05-21 21:00:27 +00:00
copy(win, s[length-size:])
return win
}
2020-10-28 01:13:57 +00:00
var zeroTime time.Time
2020-12-03 08:46:02 +00:00
//go:generate callbackgen -type SMA
2020-10-28 01:13:57 +00:00
type SMA struct {
types.IntervalWindow
2020-10-29 09:51:20 +00:00
Values Float64Slice
EndTime time.Time
2020-12-03 08:46:02 +00:00
UpdateCallbacks []func(value float64)
2020-10-28 01:13:57 +00:00
}
func (inc *SMA) Last() float64 {
return inc.Values[len(inc.Values)-1]
}
2020-10-28 01:13:57 +00:00
func (inc *SMA) calculateAndUpdate(kLines []types.KLine) {
if len(kLines) < inc.Window {
return
}
var index = len(kLines) - 1
var kline = kLines[index]
if inc.EndTime != zeroTime && kline.EndTime.Before(inc.EndTime) {
return
}
var recentK = kLines[index-(inc.Window-1) : index+1]
2020-12-05 05:04:32 +00:00
2020-12-05 05:32:16 +00:00
sma, err := calculateSMA(recentK, inc.Window, KLineClosePriceMapper)
2020-12-05 05:04:32 +00:00
if err != nil {
log.WithError(err).Error("SMA error")
return
}
2020-10-28 01:43:19 +00:00
inc.Values.Push(sma)
2020-10-28 01:13:57 +00:00
inc.EndTime = kLines[index].EndTime
2020-12-03 08:46:02 +00:00
inc.EmitUpdate(sma)
2020-10-28 01:13:57 +00:00
}
func (inc *SMA) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
if inc.Interval != interval {
return
}
inc.calculateAndUpdate(window)
}
func (inc *SMA) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
2020-10-28 01:13:57 +00:00
}
2020-12-05 05:32:16 +00:00
func calculateSMA(kLines []types.KLine, window int, priceF KLinePriceMapper) (float64, error) {
2020-10-28 01:13:57 +00:00
length := len(kLines)
2020-12-05 05:04:32 +00:00
if length == 0 || length < window {
return 0.0, fmt.Errorf("insufficient elements for calculating SMA with window = %d", window)
2020-10-28 09:47:43 +00:00
}
sum := 0.0
2020-10-28 01:13:57 +00:00
for _, k := range kLines {
2020-12-05 05:32:16 +00:00
sum += priceF(k)
2020-10-28 01:13:57 +00:00
}
2020-12-05 05:04:32 +00:00
avg := sum / float64(window)
return avg, nil
2020-10-28 01:13:57 +00:00
}