2022-05-10 08:15:26 +00:00
|
|
|
package indicator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-07-11 02:13:13 +00:00
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2022-05-10 08:15:26 +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])
|
|
|
|
cci = pd.Series((s - s.rolling(16).mean()) / (0.015 * s.rolling(16).std(ddof=0)), name="CCI")
|
|
|
|
print(cci)
|
|
|
|
*/
|
|
|
|
func Test_CCI(t *testing.T) {
|
|
|
|
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 []float64
|
2023-07-11 02:13:13 +00:00
|
|
|
var delta = 4.3e-2
|
2022-05-10 08:15:26 +00:00
|
|
|
if err := json.Unmarshal(randomPrices, &input); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
t.Run("random_case", func(t *testing.T) {
|
|
|
|
cci := CCI{IntervalWindow: types.IntervalWindow{Window: 16}}
|
|
|
|
for _, value := range input {
|
|
|
|
cci.Update(value)
|
|
|
|
}
|
|
|
|
|
2023-05-31 11:35:44 +00:00
|
|
|
last := cci.Last(0)
|
2023-07-11 02:13:13 +00:00
|
|
|
assert.InDelta(t, 93.250481, last, delta)
|
|
|
|
assert.InDelta(t, 81.813449, cci.Index(1), delta)
|
2022-05-10 08:15:26 +00:00
|
|
|
assert.Equal(t, 50-16+1, cci.Length())
|
|
|
|
})
|
|
|
|
}
|