From 86769d453507574ff3e8c9faa4e995e104e32f5b Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 18 Aug 2020 13:14:52 +0800 Subject: [PATCH] add kline window tail test --- bbgo/types/kline.go | 9 +++++++-- bbgo/types/kline_test.go | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 bbgo/types/kline_test.go 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) +}