diff --git a/pkg/indicator/v2/boll.go b/pkg/indicator/v2/boll.go index 260d9f4eb..9feda9f52 100644 --- a/pkg/indicator/v2/boll.go +++ b/pkg/indicator/v2/boll.go @@ -55,3 +55,7 @@ func (s *BOLLStream) Calculate(v float64) float64 { band := stdDev * s.k return band } + +func (s *BOLLStream) Truncate() { + s.Slice = generalTruncate(s.Slice) +} diff --git a/pkg/indicator/v2/rma.go b/pkg/indicator/v2/rma.go index b943cf6f8..828ee9224 100644 --- a/pkg/indicator/v2/rma.go +++ b/pkg/indicator/v2/rma.go @@ -5,7 +5,6 @@ import ( ) const MaxNumOfRMA = 1000 -const MaxNumOfRMATruncateSize = 500 type RMAStream struct { // embedded structs @@ -51,9 +50,7 @@ func (s *RMAStream) Calculate(x float64) float64 { } func (s *RMAStream) Truncate() { - if len(s.Slice) > MaxNumOfRMA { - s.Slice = s.Slice[MaxNumOfRMATruncateSize-1:] - } + s.Slice = generalTruncate(s.Slice) } func checkWindow(window int) { diff --git a/pkg/indicator/v2/sma.go b/pkg/indicator/v2/sma.go index 5afd4f825..0902db5a7 100644 --- a/pkg/indicator/v2/sma.go +++ b/pkg/indicator/v2/sma.go @@ -4,8 +4,6 @@ import ( "github.com/c9s/bbgo/pkg/types" ) -const MaxNumOfSMA = 5_000 - type SMAStream struct { *types.Float64Series window int @@ -29,5 +27,5 @@ func (s *SMAStream) Calculate(v float64) float64 { } func (s *SMAStream) Truncate() { - s.Slice = s.Slice.Truncate(MaxNumOfSMA) + s.Slice = generalTruncate(s.Slice) } diff --git a/pkg/indicator/v2/stddev.go b/pkg/indicator/v2/stddev.go index a9e94974d..223d8e7f2 100644 --- a/pkg/indicator/v2/stddev.go +++ b/pkg/indicator/v2/stddev.go @@ -26,3 +26,7 @@ func (s *StdDevStream) Calculate(x float64) float64 { var std = s.rawValues.Stdev() return std } + +func (s *StdDevStream) Truncate() { + s.Slice = generalTruncate(s.Slice) +} diff --git a/pkg/indicator/v2/truncate.go b/pkg/indicator/v2/truncate.go new file mode 100644 index 000000000..9178c6b09 --- /dev/null +++ b/pkg/indicator/v2/truncate.go @@ -0,0 +1,16 @@ +package indicatorv2 + +// MaxSliceSize is the maximum slice size +// byte size = 8 * 5000 = 40KB per slice +const MaxSliceSize = 5000 + +// TruncateSize is the truncate size for the slice per truncate call +const TruncateSize = 1000 + +func generalTruncate(slice []float64) []float64 { + if len(slice) < MaxSliceSize { + return slice + } + + return slice[TruncateSize-1:] +}