From 8938478d93d496e331483d8aff0c95905b1030bd Mon Sep 17 00:00:00 2001 From: Kakashi Liu Date: Tue, 19 Oct 2021 21:38:12 +0800 Subject: [PATCH] Truncate emwa slice to be the same size as given kLines --- pkg/indicator/ewma.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/indicator/ewma.go b/pkg/indicator/ewma.go index 6cdf378ffe..e6ea445366 100644 --- a/pkg/indicator/ewma.go +++ b/pkg/indicator/ewma.go @@ -9,8 +9,9 @@ import ( "github.com/c9s/bbgo/pkg/types" ) -const MaxNumOfEWMA = 1_000 -const MaxNumOfEWMATruncateSize = 500 +// These numbers should be aligned with bbgo MaxNumOfKLines and MaxNumOfKLinesTruncate +const MaxNumOfEWMA = 5_000 +const MaxNumOfEWMATruncateSize = 1000 //go:generate callbackgen -type EWMA type EWMA struct { @@ -72,6 +73,11 @@ func (inc *EWMA) calculateAndUpdate(allKLines []types.KLine) { } } + if len(inc.Values) >= MaxNumOfEWMA { + TruncateSize := MaxNumOfEWMATruncateSize + inc.Values = inc.Values[TruncateSize:] + } + for i := from; i < dataLen; i++ { var k = allKLines[i] var ewma = priceF(k)*multiplier + (1-multiplier)*inc.Values[i-1]