Skip to content

Commit

Permalink
indicator: truncate values if length exceeded
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jun 28, 2021
1 parent a804870 commit 4ccbb82
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/indicator/ewma.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"github.com/c9s/bbgo/pkg/types"
)

const MaxEWMAValues = 1_000
const EWMAValueTruncateSize = 500

//go:generate callbackgen -type EWMA
type EWMA struct {
types.IntervalWindow
Expand All @@ -24,6 +27,8 @@ func (inc *EWMA) Update(value float64) {
if len(inc.Values) == 0 {
inc.Values.Push(value)
return
} else if len(inc.Values) > MaxEWMAValues {
inc.Values = inc.Values[EWMAValueTruncateSize:]
}

ema := (1-multiplier)*inc.Last() + multiplier*value
Expand Down
8 changes: 8 additions & 0 deletions pkg/indicator/sma.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"github.com/c9s/bbgo/pkg/types"
)

const MaxSMAValues = 1_000
const SMAValueTruncateSize = 500

var zeroTime time.Time

//go:generate callbackgen -type SMA
Expand Down Expand Up @@ -44,6 +47,11 @@ func (inc *SMA) calculateAndUpdate(kLines []types.KLine) {
return
}
inc.Values.Push(sma)

if len(inc.Values) > MaxSMAValues {
inc.Values = inc.Values[SMAValueTruncateSize:]
}

inc.EndTime = kLines[index].EndTime

inc.EmitUpdate(sma)
Expand Down

0 comments on commit 4ccbb82

Please sign in to comment.