Merge pull request #251 from narumiruna/fix/kline-window

fix: KLineWindow
This commit is contained in:
Yo-An Lin 2021-05-22 16:51:53 +08:00 committed by GitHub
commit 890323c87b
2 changed files with 8 additions and 5 deletions

View File

@ -256,7 +256,7 @@ func (k KLineWindow) GetClose() float64 {
}
func (k KLineWindow) GetHigh() float64 {
high := k.GetOpen()
high := k.First().GetHigh()
for _, line := range k {
high = math.Max(high, line.GetHigh())
}
@ -265,7 +265,7 @@ func (k KLineWindow) GetHigh() float64 {
}
func (k KLineWindow) GetLow() float64 {
low := k.GetOpen()
low := k.First().GetLow()
for _, line := range k {
low = math.Min(low, line.GetLow())
}
@ -322,7 +322,7 @@ func (k KLineWindow) Color() string {
// Mid price
func (k KLineWindow) Mid() float64 {
return k.GetHigh() - k.GetLow()/2
return (k.GetHigh() + k.GetLow()) / 2.0
}
// BounceUp returns true if it's green candle with open and close near high price
@ -356,7 +356,7 @@ func (k KLineWindow) Tail(size int) KLineWindow {
}
win := make(KLineWindow, size)
copy(win, k[length-1-size:])
copy(win, k[length-size:])
return win
}

View File

@ -9,17 +9,20 @@ import (
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},
{Open: 11700.0, Close: 11700.0, High: 11700.0, Low: 11700.0},
}
var win2 = win.Tail(1)
assert.Len(t, win2, 1)
assert.ElementsMatch(t, win2, win[1:])
var win3 = win.Tail(2)
assert.Len(t, win3, 2)
assert.ElementsMatch(t, win3, win)
var win4 = win.Tail(3)
assert.Len(t, win4, 2)
assert.ElementsMatch(t, win4, win)
}
func TestKLineWindow_Truncate(t *testing.T) {