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-10-19 13:38:12 +00:00
|
|
|
// These numbers should be aligned with bbgo MaxNumOfKLines and MaxNumOfKLinesTruncate
|
|
|
|
const MaxNumOfEWMA = 5_000
|
2021-11-21 14:18:07 +00:00
|
|
|
const MaxNumOfEWMATruncateSize = 100
|
2021-06-28 06:28:44 +00:00
|
|
|
|
2020-12-03 08:46:02 +00:00
|
|
|
//go:generate callbackgen -type EWMA
|
2020-10-28 01:13:57 +00:00
|
|
|
type EWMA struct {
|
2020-10-28 23:51:23 +00:00
|
|
|
types.IntervalWindow
|
2022-06-29 12:49:02 +00:00
|
|
|
types.SeriesBase
|
2022-07-14 01:29:54 +00:00
|
|
|
|
2022-08-25 09:31:42 +00:00
|
|
|
Values floats.Slice
|
2022-07-20 17:04:49 +00:00
|
|
|
EndTime time.Time
|
2020-12-03 08:46:02 +00:00
|
|
|
|
2022-07-13 17:16:39 +00:00
|
|
|
updateCallbacks []func(value float64)
|
2020-10-28 01:13:57 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 01:29:54 +00:00
|
|
|
var _ types.SeriesExtend = &EWMA{}
|
|
|
|
|
2022-07-12 10:14:57 +00:00
|
|
|
func (inc *EWMA) Clone() *EWMA {
|
|
|
|
out := &EWMA{
|
|
|
|
IntervalWindow: inc.IntervalWindow,
|
|
|
|
Values: inc.Values[:],
|
|
|
|
}
|
|
|
|
out.SeriesBase.Series = out
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *EWMA) TestUpdate(value float64) *EWMA {
|
|
|
|
out := inc.Clone()
|
|
|
|
out.Update(value)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-05-08 16:56:44 +00:00
|
|
|
func (inc *EWMA) Update(value float64) {
|
|
|
|
var multiplier = 2.0 / float64(1+inc.Window)
|
|
|
|
|
|
|
|
if len(inc.Values) == 0 {
|
2022-06-29 12:49:02 +00:00
|
|
|
inc.SeriesBase.Series = inc
|
2021-05-08 16:56:44 +00:00
|
|
|
inc.Values.Push(value)
|
|
|
|
return
|
2021-06-28 06:32:28 +00:00
|
|
|
} else if len(inc.Values) > MaxNumOfEWMA {
|
2021-11-21 18:14:44 +00:00
|
|
|
inc.Values = inc.Values[MaxNumOfEWMATruncateSize-1:]
|
2021-05-08 16:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ema := (1-multiplier)*inc.Last() + multiplier*value
|
|
|
|
inc.Values.Push(ema)
|
|
|
|
}
|
|
|
|
|
2020-10-28 08:27:25 +00:00
|
|
|
func (inc *EWMA) Last() float64 {
|
2020-12-08 02:26:20 +00:00
|
|
|
if len(inc.Values) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-10-28 08:27:25 +00:00
|
|
|
return inc.Values[len(inc.Values)-1]
|
|
|
|
}
|
|
|
|
|
2022-03-31 10:03:26 +00:00
|
|
|
func (inc *EWMA) Index(i int) float64 {
|
|
|
|
if i >= len(inc.Values) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return inc.Values[len(inc.Values)-1-i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *EWMA) Length() int {
|
|
|
|
return len(inc.Values)
|
|
|
|
}
|
|
|
|
|
2022-07-20 17:04:49 +00:00
|
|
|
func (inc *EWMA) PushK(k types.KLine) {
|
|
|
|
if inc.EndTime != zeroTime && k.EndTime.Before(inc.EndTime) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
inc.Update(k.Close.Float64())
|
|
|
|
inc.EndTime = k.EndTime.Time()
|
|
|
|
inc.EmitUpdate(inc.Last())
|
|
|
|
}
|
|
|
|
|
2022-08-24 09:53:22 +00:00
|
|
|
func CalculateKLinesEMA(allKLines []types.KLine, priceF KLineValueMapper, window int) float64 {
|
2020-12-05 05:04:32 +00:00
|
|
|
var multiplier = 2.0 / (float64(window) + 1)
|
|
|
|
return ewma(MapKLinePrice(allKLines, priceF), multiplier)
|
2020-12-04 11:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// see https://www.investopedia.com/ask/answers/122314/what-exponential-moving-average-ema-formula-and-how-ema-calculated.asp
|
2020-12-05 05:04:32 +00:00
|
|
|
func ewma(prices []float64, multiplier float64) float64 {
|
|
|
|
var end = len(prices) - 1
|
2020-12-04 11:40:05 +00:00
|
|
|
if end == 0 {
|
2020-12-05 05:04:32 +00:00
|
|
|
return prices[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return prices[end]*multiplier + (1-multiplier)*ewma(prices[:end], multiplier)
|
|
|
|
}
|