forked from c9s/bbgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add basic implementation of alma indicator
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package indicator | ||
|
||
import ( | ||
"math" | ||
|
||
"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 | ||
// @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 { | ||
types.IntervalWindow | ||
Offset float64 | ||
Sigma int | ||
Weight []float64 | ||
Sum float64 | ||
input []float64 | ||
Values types.Float64Slice | ||
UpdateCallbacks []func(value float64) | ||
} | ||
|
||
const MaxNumOfALMA = 5_000 | ||
const MaxNumOfALMATruncateSize = 100 | ||
|
||
func (inc *ALMA) Update(value float64) { | ||
if inc.Weight == nil { | ||
inc.Weight = make([]float64, inc.Window) | ||
m := inc.Offset * (float64(inc.Window) - 1.) | ||
s := float64(inc.Window) / float64(inc.Sigma) | ||
inc.Sum = 0. | ||
for i := 0; i < inc.Window; i++ { | ||
diff := float64(i) - m | ||
wt := math.Exp(-diff*diff/2./s/s) | ||
inc.Sum += wt | ||
inc.Weight[i] = wt | ||
} | ||
} | ||
inc.input = append(inc.input, value) | ||
if len(inc.input) >= inc.Window { | ||
weightedSum := 0.0 | ||
inc.input = inc.input[len(inc.input) - inc.Window:] | ||
for i := 0; i < inc.Window; i++ { | ||
weightedSum += inc.Weight[i] * inc.input[i] | ||
} | ||
inc.Values.Push(weightedSum / inc.Sum) | ||
if len(inc.Values) > MaxNumOfALMA { | ||
inc.Values = inc.Values[MaxNumOfALMATruncateSize-1:] | ||
} | ||
} | ||
} | ||
|
||
func (inc *ALMA) Last() float64 { | ||
if len(inc.Values) == 0 { | ||
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) | ||
} | ||
|
||
func (inc *ALMA) calculateAndUpdate(allKLines []types.KLine) { | ||
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 | ||
} | ||
inc.calculateAndUpdate(window) | ||
} | ||
|
||
func (inc *ALMA) Bind(updater KLineWindowUpdater) { | ||
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.