2021-05-08 16:56:44 +00:00
|
|
|
package indicator
|
|
|
|
|
|
|
|
import (
|
2022-02-15 05:55:19 +00:00
|
|
|
"encoding/json"
|
2021-05-12 06:39:10 +00:00
|
|
|
"math"
|
2021-05-08 16:56:44 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-02-08 04:41:24 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
2022-02-15 05:55:19 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2021-05-08 16:56:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
python:
|
|
|
|
|
|
|
|
import pandas as pd
|
|
|
|
s = 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,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9])
|
|
|
|
slow = s.ewm(span=26, adjust=False).mean()
|
|
|
|
fast = s.ewm(span=12, adjust=False).mean()
|
|
|
|
print(fast - slow)
|
|
|
|
*/
|
|
|
|
|
|
|
|
func Test_calculateMACD(t *testing.T) {
|
2022-02-08 04:41:24 +00:00
|
|
|
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, 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)
|
|
|
|
}
|
2021-05-08 16:56:44 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
kLines []types.KLine
|
|
|
|
want float64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "random_case",
|
2022-02-08 04:41:24 +00:00
|
|
|
kLines: buildKLines(input),
|
2021-05-08 16:56:44 +00:00
|
|
|
want: 0.7967670223776384,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
iw := types.IntervalWindow{Window: 9}
|
|
|
|
macd := MACD{IntervalWindow: iw, ShortPeriod: 12, LongPeriod: 26}
|
2022-07-26 10:59:00 +00:00
|
|
|
for _, k := range tt.kLines {
|
|
|
|
macd.PushK(k)
|
|
|
|
}
|
2022-07-14 02:36:16 +00:00
|
|
|
|
|
|
|
got := macd.Last()
|
2021-05-12 06:39:10 +00:00
|
|
|
diff := math.Trunc((got-tt.want)*100) / 100
|
|
|
|
if diff != 0 {
|
2021-05-08 16:56:44 +00:00
|
|
|
t.Errorf("calculateMACD() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|