fix kline window truncate

This commit is contained in:
c9s 2020-08-18 13:22:27 +08:00
parent 86769d4535
commit 31be04cc74
2 changed files with 30 additions and 2 deletions

View File

@ -214,6 +214,10 @@ func (k KLineWindow) First() KLine {
return k[0]
}
func (k KLineWindow) Last() KLine {
return k[len(k)-1]
}
func (k KLineWindow) GetInterval() string {
return k.First().Interval
}
@ -330,17 +334,19 @@ func (k KLineWindow) Tail(size int) KLineWindow {
return win
}
// Truncate removes the old klines from the window
func (k *KLineWindow) Truncate(size int) {
if len(*k) <= size {
return
}
end := len(*k) - 1
end := len(*k)
start := end - size
if start < 0 {
start = 0
}
*k = (*k)[start:end]
kn := (*k)[start:]
*k = kn
}
func (k KLineWindow) GetBody() float64 {

View File

@ -20,3 +20,25 @@ func TestKLineWindow_Tail(t *testing.T) {
var win4 = win.Tail(3)
assert.Len(t, win4, 2)
}
func TestKLineWindow_Truncate(t *testing.T) {
var win = KLineWindow{
{ Open: "11600.0", Close: "11600.0", High: "11600.0", Low: "11600.0"},
{ Open: "11601.0", Close: "11600.0", High: "11600.0", Low: "11600.0"},
{ Open: "11602.0", Close: "11600.0", High: "11600.0", Low: "11600.0"},
{ Open: "11603.0", Close: "11600.0", High: "11600.0", Low: "11600.0"},
}
win.Truncate(5)
assert.Len(t, win, 4)
assert.Equal(t, "11603.0", win.Last().Open)
win.Truncate(3)
assert.Len(t, win, 3)
assert.Equal(t, "11603.0", win.Last().Open)
win.Truncate(1)
assert.Len(t, win, 1)
assert.Equal(t, "11603.0", win.Last().Open)
}