diff --git a/bbgo/types/kline.go b/bbgo/types/kline.go index 570328fe9..1af945f5b 100644 --- a/bbgo/types/kline.go +++ b/bbgo/types/kline.go @@ -320,9 +320,14 @@ func (k KLineWindow) Take(size int) KLineWindow { func (k KLineWindow) Tail(size int) KLineWindow { if len(k) <= size { - return k[:] + win := make(KLineWindow, len(k)) + copy(win, k) + return win } - return k[len(k)-size:] + + win := make(KLineWindow, size) + copy(win, k[len(k)-size:]) + return win } func (k *KLineWindow) Truncate(size int) { diff --git a/bbgo/types/kline_test.go b/bbgo/types/kline_test.go new file mode 100644 index 000000000..b550d560b --- /dev/null +++ b/bbgo/types/kline_test.go @@ -0,0 +1,22 @@ +package types + +import ("testing" + + "github.com/stretchr/testify/assert" +) + +func TestKLineWindow_Tail(t *testing.T) { + var win = KLineWindow{ + { Open: "11600.0", Close: "11600.0", High: "11600.0", Low: "11600.0"}, + { Open: "11600.0", Close: "11600.0", High: "11600.0", Low: "11600.0"}, + } + + var win2 = win.Tail(1) + assert.Len(t, win2, 1) + + var win3 = win.Tail(2) + assert.Len(t, win3, 2) + + var win4 = win.Tail(3) + assert.Len(t, win4, 2) +}