mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
Merge pull request #707 from zenixls2/feature/alma
feature: add basic implementation of alma indicator
This commit is contained in:
commit
92b21e8fe6
95
pkg/indicator/alma.go
Normal file
95
pkg/indicator/alma.go
Normal file
|
@ -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 // required
|
||||
Offset float64 // required: recommend to be 5
|
||||
Sigma int // required: recommend to be 0.5
|
||||
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[inc.Window-i-1] * 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)
|
||||
}
|
15
pkg/indicator/alma_callbacks.go
Normal file
15
pkg/indicator/alma_callbacks.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
// Code generated by "callbackgen -type ALMA"; DO NOT EDIT.
|
||||
|
||||
package indicator
|
||||
|
||||
import ()
|
||||
|
||||
func (inc *ALMA) OnUpdate(cb func(value float64)) {
|
||||
inc.UpdateCallbacks = append(inc.UpdateCallbacks, cb)
|
||||
}
|
||||
|
||||
func (inc *ALMA) EmitUpdate(value float64) {
|
||||
for _, cb := range inc.UpdateCallbacks {
|
||||
cb(value)
|
||||
}
|
||||
}
|
61
pkg/indicator/alma_test.go
Normal file
61
pkg/indicator/alma_test.go
Normal file
|
@ -0,0 +1,61 @@
|
|||
package indicator
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
/*
|
||||
python:
|
||||
|
||||
import pandas as pd
|
||||
import pandas_ta as ta
|
||||
|
||||
data = pd.Series([0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9])
|
||||
sigma = 6
|
||||
offset = 0.9
|
||||
size = 5
|
||||
|
||||
result = ta.alma(data, size, sigma, offset)
|
||||
print(result)
|
||||
*/
|
||||
func Test_ALMA(t *testing.T) {
|
||||
var Delta = 0.01
|
||||
var randomPrices = []byte(`[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]`)
|
||||
var input []fixedpoint.Value
|
||||
if err := json.Unmarshal(randomPrices, &input); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
kLines []types.KLine
|
||||
want float64
|
||||
next float64
|
||||
all int
|
||||
}{
|
||||
{
|
||||
name: "random_case",
|
||||
kLines: buildKLines(input),
|
||||
want: 5.60785,
|
||||
next: 4.60785,
|
||||
all: 26,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
alma := ALMA{
|
||||
IntervalWindow: types.IntervalWindow{Window: 5},
|
||||
Offset: 0.9,
|
||||
Sigma: 6,
|
||||
}
|
||||
alma.calculateAndUpdate(tt.kLines)
|
||||
assert.InDelta(t, tt.want, alma.Last(), Delta)
|
||||
assert.InDelta(t, tt.next, alma.Index(1), Delta)
|
||||
assert.Equal(t, tt.all, alma.Length())
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user