2020-10-28 01:13:57 +00:00
|
|
|
package indicator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2022-08-25 09:31:42 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/datatype/floats"
|
2020-10-28 01:13:57 +00:00
|
|
|
"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-08-25 09:31:42 +00:00
|
|
|
Values floats.Slice
|
2022-07-14 03:34:53 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-05-31 11:35:44 +00:00
|
|
|
func (inc *SMA) Last(i int) float64 {
|
|
|
|
return inc.Values.Last(i)
|
2020-10-28 08:27:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 04:14:17 +00:00
|
|
|
func (inc *SMA) Index(i int) float64 {
|
2023-05-31 11:35:44 +00:00
|
|
|
return inc.Last(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-07-12 10:14:57 +00:00
|
|
|
func (inc *SMA) Clone() types.UpdatableSeriesExtend {
|
|
|
|
out := &SMA{
|
|
|
|
Values: inc.Values[:],
|
2022-07-13 05:31:49 +00:00
|
|
|
rawValues: inc.rawValues.Clone(),
|
2022-07-12 10:14:57 +00:00
|
|
|
EndTime: inc.EndTime,
|
|
|
|
}
|
|
|
|
out.SeriesBase.Series = out
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
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-10-27 08:31:39 +00:00
|
|
|
if len(inc.Values) > MaxNumOfSMA {
|
|
|
|
inc.Values = inc.Values[MaxNumOfSMATruncateSize-1:]
|
|
|
|
}
|
2022-04-08 09:48:33 +00:00
|
|
|
}
|
|
|
|
|
2022-07-20 17:04:49 +00:00
|
|
|
func (inc *SMA) BindK(target KLineClosedEmitter, symbol string, interval types.Interval) {
|
|
|
|
target.OnKLineClosed(types.KLineWith(symbol, interval, inc.PushK))
|
|
|
|
}
|
|
|
|
|
2022-07-13 17:12:36 +00:00
|
|
|
func (inc *SMA) PushK(k types.KLine) {
|
2022-07-20 17:04:49 +00:00
|
|
|
if inc.EndTime != zeroTime && k.EndTime.Before(inc.EndTime) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-13 17:12:36 +00:00
|
|
|
inc.Update(k.Close.Float64())
|
2022-07-14 03:34:53 +00:00
|
|
|
inc.EndTime = k.EndTime.Time()
|
2023-05-31 11:35:44 +00:00
|
|
|
inc.EmitUpdate(inc.Values.Last(0))
|
2022-07-13 17:12:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-20 17:04:49 +00:00
|
|
|
func (inc *SMA) LoadK(allKLines []types.KLine) {
|
|
|
|
for _, k := range allKLines {
|
|
|
|
inc.PushK(k)
|
|
|
|
}
|
|
|
|
}
|