2020-10-28 01:13:57 +00:00
|
|
|
package indicator
|
|
|
|
|
|
|
|
import (
|
2020-12-05 05:04:32 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
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
|
2020-12-05 05:04:32 +00:00
|
|
|
Values Float64Slice
|
|
|
|
LastOpenTime 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 *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]
|
|
|
|
}
|
|
|
|
|
2020-12-05 05:04:32 +00:00
|
|
|
func (inc *EWMA) calculateAndUpdate(allKLines []types.KLine) {
|
|
|
|
if len(allKLines) < inc.Window {
|
2020-10-28 01:13:57 +00:00
|
|
|
// we can't calculate
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-05 05:04:32 +00:00
|
|
|
var priceF = KLineClosePriceMapper
|
|
|
|
var dataLen = len(allKLines)
|
|
|
|
var multiplier = 2.0 / (float64(inc.Window) + 1)
|
|
|
|
|
|
|
|
// init the values from the kline data
|
|
|
|
var from = 1
|
|
|
|
if len(inc.Values) == 0 {
|
|
|
|
// for the first value, we should use the close price
|
|
|
|
inc.Values = []float64{priceF(allKLines[0])}
|
|
|
|
} else {
|
2020-12-05 05:32:16 +00:00
|
|
|
// from = len(inc.Values)
|
|
|
|
|
2020-12-05 05:04:32 +00:00
|
|
|
// update ewma with the existing values
|
|
|
|
for i := dataLen - 1; i > 0; i-- {
|
2020-12-05 05:32:16 +00:00
|
|
|
var k = allKLines[i]
|
|
|
|
if k.StartTime.After(inc.LastOpenTime) {
|
2020-12-05 05:04:32 +00:00
|
|
|
from = i
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := from; i < dataLen; i++ {
|
|
|
|
var k = allKLines[i]
|
|
|
|
var ewma = priceF(k)*multiplier + (1-multiplier)*inc.Values[i-1]
|
|
|
|
inc.Values.Push(ewma)
|
|
|
|
inc.LastOpenTime = k.StartTime
|
|
|
|
inc.EmitUpdate(ewma)
|
2020-10-28 01:13:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 05:04:32 +00:00
|
|
|
if len(inc.Values) != dataLen {
|
2020-12-05 05:32:16 +00:00
|
|
|
log.Warnf("%s EMA (%d) value length (%d) != all kline data length (%d)", inc.Interval, inc.Window, len(inc.Values), dataLen)
|
2020-12-05 05:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
v1 := math.Floor(inc.Values[len(inc.Values)-1]*100.0) / 100.0
|
2020-12-07 15:03:06 +00:00
|
|
|
v2 := math.Floor(CalculateKLinesEMA(allKLines, priceF, inc.Window)*100.0) / 100.0
|
2020-12-05 05:04:32 +00:00
|
|
|
if v1 != v2 {
|
2020-12-05 05:32:16 +00:00
|
|
|
log.Warnf("ACCUMULATED %s EMA (%d) %f != EMA %f", inc.Interval, inc.Window, v1, v2)
|
2020-12-05 05:04:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 15:03:06 +00:00
|
|
|
func CalculateKLinesEMA(allKLines []types.KLine, priceF KLinePriceMapper, 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
type KLinePriceMapper func(k types.KLine) float64
|
|
|
|
|
|
|
|
func KLineOpenPriceMapper(k types.KLine) float64 {
|
|
|
|
return k.Open
|
|
|
|
}
|
|
|
|
|
|
|
|
func KLineClosePriceMapper(k types.KLine) float64 {
|
|
|
|
return k.Close
|
|
|
|
}
|
|
|
|
|
|
|
|
func MapKLinePrice(kLines []types.KLine, f KLinePriceMapper) (prices []float64) {
|
|
|
|
for _, k := range kLines {
|
|
|
|
prices = append(prices, f(k))
|
2020-10-28 01:13:57 +00:00
|
|
|
}
|
2020-12-04 11:40:05 +00:00
|
|
|
|
2020-12-05 05:04:32 +00:00
|
|
|
return prices
|
2020-10-28 01:13:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 08:27:25 +00:00
|
|
|
type KLineWindowUpdater interface {
|
|
|
|
OnKLineWindowUpdate(func(interval types.Interval, window types.KLineWindow))
|
|
|
|
}
|
|
|
|
|
2020-10-28 23:40:02 +00:00
|
|
|
func (inc *EWMA) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
|
|
|
|
if inc.Interval != interval {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
inc.calculateAndUpdate(window)
|
|
|
|
}
|
2020-10-28 01:13:57 +00:00
|
|
|
|
2020-10-28 23:40:02 +00:00
|
|
|
func (inc *EWMA) Bind(updater KLineWindowUpdater) {
|
|
|
|
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
|
2020-10-28 01:13:57 +00:00
|
|
|
}
|