2022-04-19 10:22:22 +00:00
|
|
|
package indicator
|
|
|
|
|
|
|
|
import (
|
2022-08-25 09:31:42 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/datatype/floats"
|
2022-04-19 10:22:22 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Refer: Double Exponential Moving Average
|
|
|
|
// Refer URL: https://investopedia.com/terms/d/double-exponential-moving-average.asp
|
2022-12-13 05:02:38 +00:00
|
|
|
//
|
|
|
|
// The Double Exponential Moving Average (DEMA) is a technical analysis indicator that is used to smooth price data and reduce the lag
|
|
|
|
// associated with traditional moving averages. It is calculated by taking the exponentially weighted moving average of the input data,
|
|
|
|
// and then taking the exponentially weighted moving average of that result. This double-smoothing process helps to eliminate much of the noise
|
|
|
|
// in the original data and provides a more accurate representation of the underlying trend. The DEMA line is then plotted on the price chart,
|
|
|
|
// which can be used to make predictions about future price movements. The DEMA is typically more responsive to changes in the underlying data
|
|
|
|
// than a simple moving average, but may be less reliable in trending markets.
|
2022-04-19 10:22:22 +00:00
|
|
|
|
|
|
|
//go:generate callbackgen -type DEMA
|
|
|
|
type DEMA struct {
|
|
|
|
types.IntervalWindow
|
2022-06-29 12:49:02 +00:00
|
|
|
types.SeriesBase
|
2022-08-25 09:31:42 +00:00
|
|
|
Values floats.Slice
|
2022-04-19 10:22:22 +00:00
|
|
|
a1 *EWMA
|
|
|
|
a2 *EWMA
|
|
|
|
|
|
|
|
UpdateCallbacks []func(value float64)
|
|
|
|
}
|
|
|
|
|
2022-07-12 10:14:57 +00:00
|
|
|
func (inc *DEMA) Clone() *DEMA {
|
|
|
|
out := &DEMA{
|
|
|
|
IntervalWindow: inc.IntervalWindow,
|
|
|
|
Values: inc.Values[:],
|
|
|
|
a1: inc.a1.Clone(),
|
|
|
|
a2: inc.a2.Clone(),
|
|
|
|
}
|
|
|
|
out.SeriesBase.Series = out
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *DEMA) TestUpdate(value float64) *DEMA {
|
|
|
|
out := inc.Clone()
|
|
|
|
out.Update(value)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2022-04-19 10:22:22 +00:00
|
|
|
func (inc *DEMA) Update(value float64) {
|
|
|
|
if len(inc.Values) == 0 {
|
2022-06-29 12:49:02 +00:00
|
|
|
inc.SeriesBase.Series = inc
|
2022-08-24 10:17:37 +00:00
|
|
|
inc.a1 = &EWMA{IntervalWindow: inc.IntervalWindow}
|
|
|
|
inc.a2 = &EWMA{IntervalWindow: inc.IntervalWindow}
|
2022-04-19 10:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inc.a1.Update(value)
|
2023-05-31 11:35:44 +00:00
|
|
|
inc.a2.Update(inc.a1.Last(0))
|
|
|
|
inc.Values.Push(2*inc.a1.Last(0) - inc.a2.Last(0))
|
2022-04-19 10:22:22 +00:00
|
|
|
if len(inc.Values) > MaxNumOfEWMA {
|
|
|
|
inc.Values = inc.Values[MaxNumOfEWMATruncateSize-1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 11:35:44 +00:00
|
|
|
func (inc *DEMA) Last(i int) float64 {
|
|
|
|
return inc.Values.Last(i)
|
2022-04-19 10:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *DEMA) Index(i int) float64 {
|
2023-05-31 11:35:44 +00:00
|
|
|
return inc.Last(i)
|
2022-04-19 10:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *DEMA) Length() int {
|
|
|
|
return len(inc.Values)
|
|
|
|
}
|
|
|
|
|
2022-06-29 12:49:02 +00:00
|
|
|
var _ types.SeriesExtend = &DEMA{}
|
2022-04-19 10:22:22 +00:00
|
|
|
|
2022-07-13 17:12:36 +00:00
|
|
|
func (inc *DEMA) PushK(k types.KLine) {
|
|
|
|
inc.Update(k.Close.Float64())
|
|
|
|
}
|
|
|
|
|
2022-07-14 02:28:53 +00:00
|
|
|
func (inc *DEMA) CalculateAndUpdate(allKLines []types.KLine) {
|
2022-04-26 08:32:31 +00:00
|
|
|
if inc.a1 == nil {
|
|
|
|
for _, k := range allKLines {
|
2022-07-13 17:12:36 +00:00
|
|
|
inc.PushK(k)
|
2023-05-31 11:35:44 +00:00
|
|
|
inc.EmitUpdate(inc.Last(0))
|
2022-04-26 08:32:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-07-13 17:12:36 +00:00
|
|
|
// last k
|
|
|
|
k := allKLines[len(allKLines)-1]
|
|
|
|
inc.PushK(k)
|
2023-05-31 11:35:44 +00:00
|
|
|
inc.EmitUpdate(inc.Last(0))
|
2022-04-19 10:22:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *DEMA) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
|
|
|
|
if inc.Interval != interval {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-14 02:28:53 +00:00
|
|
|
inc.CalculateAndUpdate(window)
|
2022-04-19 10:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *DEMA) Bind(updater KLineWindowUpdater) {
|
|
|
|
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
|
|
|
|
}
|