2020-10-28 01:13:57 +00:00
|
|
|
package indicator
|
|
|
|
|
|
|
|
import (
|
2020-12-05 05:04:32 +00:00
|
|
|
"fmt"
|
2020-10-28 01:13:57 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2021-11-21 14:18:07 +00:00
|
|
|
const MaxNumOfSMA = 5_000
|
|
|
|
const MaxNumOfSMATruncateSize = 100
|
2021-06-28 06:28:44 +00:00
|
|
|
|
2020-12-03 08:46:02 +00:00
|
|
|
//go:generate callbackgen -type SMA
|
2020-10-28 01:13:57 +00:00
|
|
|
type SMA struct {
|
2022-06-29 12:49:02 +00:00
|
|
|
types.SeriesBase
|
2020-10-28 23:51:23 +00:00
|
|
|
types.IntervalWindow
|
2022-07-14 03:34:53 +00:00
|
|
|
Values types.Float64Slice
|
|
|
|
rawValues *types.Queue
|
|
|
|
EndTime time.Time
|
2020-12-03 08:46:02 +00:00
|
|
|
|
|
|
|
UpdateCallbacks []func(value float64)
|
2020-10-28 01:13:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 08:27:25 +00:00
|
|
|
func (inc *SMA) Last() float64 {
|
2022-07-13 03:28:41 +00:00
|
|
|
if inc.Values.Length() == 0 {
|
2021-10-14 06:22:07 +00:00
|
|
|
return 0.0
|
|
|
|
}
|
2022-07-13 03:28:41 +00:00
|
|
|
return inc.Values.Last()
|
2020-10-28 08:27:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 04:14:17 +00:00
|
|
|
func (inc *SMA) Index(i int) float64 {
|
2022-07-13 03:28:41 +00:00
|
|
|
if i >= inc.Values.Length() {
|
2022-04-04 04:14:17 +00:00
|
|
|
return 0.0
|
|
|
|
}
|
|
|
|
|
2022-07-13 03:28:41 +00:00
|
|
|
return inc.Values.Index(i)
|
2022-04-04 04:14:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *SMA) Length() int {
|
2022-07-13 03:28:41 +00:00
|
|
|
return inc.Values.Length()
|
2022-04-04 04:14:17 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 12:49:02 +00:00
|
|
|
var _ types.SeriesExtend = &SMA{}
|
2022-04-04 04:14:17 +00:00
|
|
|
|
2022-04-08 09:48:33 +00:00
|
|
|
func (inc *SMA) Update(value float64) {
|
2022-07-14 03:34:53 +00:00
|
|
|
if inc.rawValues == nil {
|
|
|
|
inc.rawValues = types.NewQueue(inc.Window)
|
2022-07-13 03:28:41 +00:00
|
|
|
inc.SeriesBase.Series = inc
|
|
|
|
}
|
2022-07-14 03:34:53 +00:00
|
|
|
|
|
|
|
inc.rawValues.Update(value)
|
|
|
|
if inc.rawValues.Length() < inc.Window {
|
2022-04-08 09:48:33 +00:00
|
|
|
return
|
|
|
|
}
|
2022-07-14 03:34:53 +00:00
|
|
|
|
|
|
|
inc.Values.Push(types.Mean(inc.rawValues))
|
2022-04-08 09:48:33 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 17:12:36 +00:00
|
|
|
func (inc *SMA) PushK(k types.KLine) {
|
|
|
|
inc.Update(k.Close.Float64())
|
2022-07-14 03:34:53 +00:00
|
|
|
inc.EndTime = k.EndTime.Time()
|
2022-07-13 17:12:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 03:34:53 +00:00
|
|
|
func (inc *SMA) CalculateAndUpdate(allKLines []types.KLine) {
|
|
|
|
var last = allKLines[len(allKLines)-1]
|
|
|
|
|
|
|
|
if inc.rawValues == nil {
|
|
|
|
for _, k := range allKLines {
|
|
|
|
if inc.EndTime != zeroTime && k.EndTime.Before(inc.EndTime) {
|
|
|
|
continue
|
|
|
|
}
|
2022-07-13 17:12:36 +00:00
|
|
|
inc.PushK(k)
|
2022-07-13 03:28:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-07-14 03:34:53 +00:00
|
|
|
inc.PushK(last)
|
2021-06-28 06:28:44 +00:00
|
|
|
}
|
2022-07-14 03:36:34 +00:00
|
|
|
|
|
|
|
inc.EmitUpdate(inc.Values.Last())
|
2020-10-28 01:13:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 23:40:02 +00:00
|
|
|
func (inc *SMA) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
|
|
|
|
if inc.Interval != interval {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-13 17:12:36 +00:00
|
|
|
inc.CalculateAndUpdate(window)
|
2020-10-28 23:40:02 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 07:57:17 +00:00
|
|
|
func (inc *SMA) BindK(target KLineClosedEmitter, symbol string, interval types.Interval) {
|
|
|
|
target.OnKLineClosed(types.KLineWith(symbol, interval, inc.PushK))
|
|
|
|
}
|
|
|
|
|
2020-10-28 23:40:02 +00:00
|
|
|
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
|
|
|
}
|
2022-07-13 03:33:57 +00:00
|
|
|
if length != window {
|
|
|
|
return 0.0, fmt.Errorf("too much klines passed in, requires only %d klines", 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
|
|
|
}
|