2022-06-10 11:02:38 +00:00
|
|
|
package indicator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
|
2022-08-25 09:31:42 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/datatype/floats"
|
2022-06-10 11:02:38 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Refer: Arnaud Legoux Moving Average
|
|
|
|
// Refer: https://capital.com/arnaud-legoux-moving-average
|
|
|
|
// Also check https://github.com/DaveSkender/Stock.Indicators/blob/main/src/a-d/Alma/Alma.cs
|
2022-12-13 05:02:38 +00:00
|
|
|
//
|
|
|
|
// The Arnaud Legoux Moving Average (ALMA) is a technical analysis indicator that is used to smooth price data and reduce the lag associated
|
|
|
|
// with traditional moving averages. It was developed by Arnaud Legoux and is based on the weighted moving average, with the weighting factors
|
|
|
|
// determined using a Gaussian function. The ALMA is calculated by taking the weighted moving average of the input data using weighting factors
|
|
|
|
// that are based on the standard deviation of the data and the specified length of the moving average. This resulting average is then plotted
|
|
|
|
// on the price chart as a line, which can be used to make predictions about future price movements. The ALMA is typically more responsive to
|
|
|
|
// changes in the underlying data than a simple moving average, but may be less reliable in trending markets.
|
|
|
|
//
|
2022-06-10 11:02:38 +00:00
|
|
|
// @param offset: Gaussian applied to the combo line. 1->ema, 0->sma
|
|
|
|
// @param sigma: the standard deviation applied to the combo line. This makes the combo line sharper
|
|
|
|
//go:generate callbackgen -type ALMA
|
|
|
|
type ALMA struct {
|
2022-06-29 12:49:02 +00:00
|
|
|
types.SeriesBase
|
2022-06-14 08:42:49 +00:00
|
|
|
types.IntervalWindow // required
|
2022-07-13 10:37:42 +00:00
|
|
|
Offset float64 // required: recommend to be 0.5
|
|
|
|
Sigma int // required: recommend to be 5
|
2022-06-29 12:49:02 +00:00
|
|
|
weight []float64
|
|
|
|
sum float64
|
2022-12-13 05:02:38 +00:00
|
|
|
input []float64
|
|
|
|
Values floats.Slice
|
|
|
|
UpdateCallbacks []func(value float64)
|
2022-06-10 11:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const MaxNumOfALMA = 5_000
|
|
|
|
const MaxNumOfALMATruncateSize = 100
|
|
|
|
|
|
|
|
func (inc *ALMA) Update(value float64) {
|
2022-06-29 12:49:02 +00:00
|
|
|
if inc.weight == nil {
|
|
|
|
inc.SeriesBase.Series = inc
|
|
|
|
inc.weight = make([]float64, inc.Window)
|
2022-06-10 11:02:38 +00:00
|
|
|
m := inc.Offset * (float64(inc.Window) - 1.)
|
|
|
|
s := float64(inc.Window) / float64(inc.Sigma)
|
2022-06-29 12:49:02 +00:00
|
|
|
inc.sum = 0.
|
2022-06-10 11:02:38 +00:00
|
|
|
for i := 0; i < inc.Window; i++ {
|
|
|
|
diff := float64(i) - m
|
2022-06-14 08:42:49 +00:00
|
|
|
wt := math.Exp(-diff * diff / 2. / s / s)
|
2022-06-29 12:49:02 +00:00
|
|
|
inc.sum += wt
|
|
|
|
inc.weight[i] = wt
|
2022-06-10 11:02:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
inc.input = append(inc.input, value)
|
|
|
|
if len(inc.input) >= inc.Window {
|
|
|
|
weightedSum := 0.0
|
2022-06-14 08:42:49 +00:00
|
|
|
inc.input = inc.input[len(inc.input)-inc.Window:]
|
2022-06-10 11:02:38 +00:00
|
|
|
for i := 0; i < inc.Window; i++ {
|
2022-06-29 12:49:02 +00:00
|
|
|
weightedSum += inc.weight[inc.Window-i-1] * inc.input[i]
|
2022-06-10 11:02:38 +00:00
|
|
|
}
|
2022-06-29 12:49:02 +00:00
|
|
|
inc.Values.Push(weightedSum / inc.sum)
|
2022-06-10 11:02:38 +00:00
|
|
|
if len(inc.Values) > MaxNumOfALMA {
|
|
|
|
inc.Values = inc.Values[MaxNumOfALMATruncateSize-1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *ALMA) Last() float64 {
|
2022-06-14 08:42:49 +00:00
|
|
|
if len(inc.Values) == 0 {
|
2022-06-10 11:02:38 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return inc.Values[len(inc.Values)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *ALMA) Index(i int) float64 {
|
|
|
|
if i >= len(inc.Values) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return inc.Values[len(inc.Values)-i-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *ALMA) Length() int {
|
|
|
|
return len(inc.Values)
|
|
|
|
}
|
|
|
|
|
2022-06-29 12:49:02 +00:00
|
|
|
var _ types.SeriesExtend = &ALMA{}
|
|
|
|
|
2022-07-13 16:41:20 +00:00
|
|
|
func (inc *ALMA) CalculateAndUpdate(allKLines []types.KLine) {
|
2022-06-10 11:02:38 +00:00
|
|
|
if inc.input == nil {
|
|
|
|
for _, k := range allKLines {
|
|
|
|
inc.Update(k.Close.Float64())
|
|
|
|
inc.EmitUpdate(inc.Last())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
inc.Update(allKLines[len(allKLines)-1].Close.Float64())
|
|
|
|
inc.EmitUpdate(inc.Last())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *ALMA) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
|
|
|
|
if inc.Interval != interval {
|
|
|
|
return
|
|
|
|
}
|
2022-07-13 16:41:20 +00:00
|
|
|
inc.CalculateAndUpdate(window)
|
2022-06-10 11:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *ALMA) Bind(updater KLineWindowUpdater) {
|
|
|
|
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
|
|
|
|
}
|