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.
test: add test cases for dema, hull, tema, till, vidya and zlema indi…
…cators
- Loading branch information
Showing
10 changed files
with
323 additions
and
12 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,55 @@ | ||
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 | ||
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]) | ||
ma1 = s.ewm(span=16).mean() | ||
ma2 = ma1.ewm(span=16).mean() | ||
result = (2 * ma1 - ma2) | ||
print(result) | ||
*/ | ||
func Test_DEMA(t *testing.T) { | ||
var Delta = 4e-2 | ||
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) | ||
} | ||
tests := []struct { | ||
name string | ||
kLines []types.KLine | ||
want float64 | ||
next float64 | ||
all int | ||
}{ | ||
{ | ||
name: "random_case", | ||
kLines: buildKLines(input), | ||
want: 6.420838, | ||
next: 5.609367, | ||
all: 50, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
dema := DEMA{IntervalWindow: types.IntervalWindow{Window: 16}} | ||
dema.calculateAndUpdate(tt.kLines) | ||
last := dema.Last() | ||
assert.InDelta(t, tt.want, last, Delta) | ||
assert.InDelta(t, tt.next, dema.Index(1), Delta) | ||
assert.Equal(t, tt.all, dema.Length()) | ||
}) | ||
} | ||
} |
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
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,55 @@ | ||
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 | ||
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]) | ||
ma1 = s.ewm(span=8).mean() | ||
ma2 = s.ewm(span=16).mean() | ||
result = (2 * ma1 - ma2).ewm(span=4).mean() | ||
print(result) | ||
*/ | ||
func Test_HULL(t *testing.T) { | ||
var Delta = 1.5e-2 | ||
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) | ||
} | ||
tests := []struct { | ||
name string | ||
kLines []types.KLine | ||
want float64 | ||
next float64 | ||
all int | ||
}{ | ||
{ | ||
name: "random_case", | ||
kLines: buildKLines(input), | ||
want: 6.002935, | ||
next: 5.167056, | ||
all: 50, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
hull := HULL{IntervalWindow: types.IntervalWindow{Window: 16}} | ||
hull.calculateAndUpdate(tt.kLines) | ||
last := hull.Last() | ||
assert.InDelta(t, tt.want, last, Delta) | ||
assert.InDelta(t, tt.next, hull.Index(1), Delta) | ||
assert.Equal(t, tt.all, hull.Length()) | ||
}) | ||
} | ||
} |
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,56 @@ | ||
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 | ||
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]) | ||
ma1 = s.ewm(span=16).mean() | ||
ma2 = ma1.ewm(span=16).mean() | ||
ma3 = ma2.ewm(span=16).mean() | ||
result = (3 * ma1 - 3 * ma2 + ma3) | ||
print(result) | ||
*/ | ||
func Test_TEMA(t *testing.T) { | ||
var Delta = 4.3e-2 | ||
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) | ||
} | ||
tests := []struct { | ||
name string | ||
kLines []types.KLine | ||
want float64 | ||
next float64 | ||
all int | ||
}{ | ||
{ | ||
name: "random_case", | ||
kLines: buildKLines(input), | ||
want: 7.163145, | ||
next: 6.106229, | ||
all: 50, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tema := TEMA{IntervalWindow: types.IntervalWindow{Window: 16}} | ||
tema.calculateAndUpdate(tt.kLines) | ||
last := tema.Last() | ||
assert.InDelta(t, tt.want, last, Delta) | ||
assert.InDelta(t, tt.next, tema.Index(1), Delta) | ||
assert.Equal(t, tt.all, tema.Length()) | ||
}) | ||
} | ||
} |
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
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,65 @@ | ||
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 | ||
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]) | ||
ma1 = s.ewm(span=16).mean() | ||
ma2 = ma1.ewm(span=16).mean() | ||
ma3 = ma2.ewm(span=16).mean() | ||
ma4 = ma3.ewm(span=16).mean() | ||
ma5 = ma4.ewm(span=16).mean() | ||
ma6 = ma5.ewm(span=16).mean() | ||
square = 0.7 * 0.7 | ||
cube = 0.7 ** 3 | ||
c1 = -cube | ||
c2 = 3 * square + 3 * cube | ||
c3 = -6 * square - 3 * 0.7 - 3 * cube | ||
c4 = 1 + 3 * 0.7 + cube + 3 * square | ||
result = (c1 * ma6 + c2 * ma5 + c3 * ma4 + c4 * ma3) | ||
print(result) | ||
*/ | ||
func Test_TILL(t *testing.T) { | ||
var Delta = 0.18 | ||
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) | ||
} | ||
tests := []struct { | ||
name string | ||
kLines []types.KLine | ||
want float64 | ||
next float64 | ||
all int | ||
}{ | ||
{ | ||
name: "random_case", | ||
kLines: buildKLines(input), | ||
want: 4.528608, | ||
next: 4.457134, | ||
all: 50, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
till := TILL{IntervalWindow: types.IntervalWindow{Window: 16}} | ||
till.calculateAndUpdate(tt.kLines) | ||
last := till.Last() | ||
assert.InDelta(t, tt.want, last, Delta) | ||
assert.InDelta(t, tt.next, till.Index(1), Delta) | ||
assert.Equal(t, tt.all, till.Length()) | ||
}) | ||
} | ||
} |
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
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,19 @@ | ||
package indicator | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/c9s/bbgo/pkg/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_VIDYA(t *testing.T) { | ||
vidya := &VIDYA{IntervalWindow: types.IntervalWindow{Window: 16}} | ||
vidya.Update(1) | ||
assert.Equal(t, vidya.Last(), 1.) | ||
vidya.Update(2) | ||
newV := 2./17.*2. + 1.*(1.-2./17.) | ||
assert.Equal(t, vidya.Last(), newV) | ||
vidya.Update(1) | ||
assert.Equal(t, vidya.Last(), vidya.Index(1)) | ||
} |
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
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,55 @@ | ||
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 | ||
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]) | ||
lag = int((16-1)/2) | ||
emadata = s + (s - s.shift(lag)) | ||
result = emadata.ewm(span=16).mean() | ||
print(result) | ||
*/ | ||
func Test_ZLEMA(t *testing.T) { | ||
var Delta = 6.5e-2 | ||
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) | ||
} | ||
tests := []struct { | ||
name string | ||
kLines []types.KLine | ||
want float64 | ||
next float64 | ||
all int | ||
}{ | ||
{ | ||
name: "random_case", | ||
kLines: buildKLines(input), | ||
want: 6.622881, | ||
next: 5.231044, | ||
all: 42, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
zlema := ZLEMA{IntervalWindow: types.IntervalWindow{Window: 16}} | ||
zlema.calculateAndUpdate(tt.kLines) | ||
last := zlema.Last() | ||
assert.InDelta(t, tt.want, last, Delta) | ||
assert.InDelta(t, tt.next, zlema.Index(1), Delta) | ||
assert.Equal(t, tt.all, zlema.Length()) | ||
}) | ||
} | ||
} |