Skip to content

Commit

Permalink
feature: alma indicator add test
Browse files Browse the repository at this point in the history
  • Loading branch information
zenixls2 committed Jun 14, 2022
1 parent 686d1dc commit f2c5ef2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 12 deletions.
24 changes: 12 additions & 12 deletions pkg/indicator/alma.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
// @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)
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
Expand All @@ -34,17 +34,17 @@ func (inc *ALMA) Update(value float64) {
inc.Sum = 0.
for i := 0; i < inc.Window; i++ {
diff := float64(i) - m
wt := math.Exp(-diff*diff/2./s/s)
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:]
inc.input = inc.input[len(inc.input)-inc.Window:]
for i := 0; i < inc.Window; i++ {
weightedSum += inc.Weight[i] * inc.input[i]
weightedSum += inc.Weight[inc.Window-i-1] * inc.input[i]
}
inc.Values.Push(weightedSum / inc.Sum)
if len(inc.Values) > MaxNumOfALMA {
Expand All @@ -54,7 +54,7 @@ func (inc *ALMA) Update(value float64) {
}

func (inc *ALMA) Last() float64 {
if len(inc.Values) == 0 {
if len(inc.Values) == 0 {
return 0
}
return inc.Values[len(inc.Values)-1]
Expand Down
61 changes: 61 additions & 0 deletions pkg/indicator/alma_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
}
}

0 comments on commit f2c5ef2

Please sign in to comment.