indicator: implement Truncate method on the indicators

This commit is contained in:
c9s 2024-10-16 16:04:15 +08:00
parent 4f1b216fbf
commit 8ce587f5f5
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
5 changed files with 26 additions and 7 deletions

View File

@ -55,3 +55,7 @@ func (s *BOLLStream) Calculate(v float64) float64 {
band := stdDev * s.k band := stdDev * s.k
return band return band
} }
func (s *BOLLStream) Truncate() {
s.Slice = generalTruncate(s.Slice)
}

View File

@ -5,7 +5,6 @@ import (
) )
const MaxNumOfRMA = 1000 const MaxNumOfRMA = 1000
const MaxNumOfRMATruncateSize = 500
type RMAStream struct { type RMAStream struct {
// embedded structs // embedded structs
@ -51,9 +50,7 @@ func (s *RMAStream) Calculate(x float64) float64 {
} }
func (s *RMAStream) Truncate() { func (s *RMAStream) Truncate() {
if len(s.Slice) > MaxNumOfRMA { s.Slice = generalTruncate(s.Slice)
s.Slice = s.Slice[MaxNumOfRMATruncateSize-1:]
}
} }
func checkWindow(window int) { func checkWindow(window int) {

View File

@ -4,8 +4,6 @@ import (
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
) )
const MaxNumOfSMA = 5_000
type SMAStream struct { type SMAStream struct {
*types.Float64Series *types.Float64Series
window int window int
@ -29,5 +27,5 @@ func (s *SMAStream) Calculate(v float64) float64 {
} }
func (s *SMAStream) Truncate() { func (s *SMAStream) Truncate() {
s.Slice = s.Slice.Truncate(MaxNumOfSMA) s.Slice = generalTruncate(s.Slice)
} }

View File

@ -26,3 +26,7 @@ func (s *StdDevStream) Calculate(x float64) float64 {
var std = s.rawValues.Stdev() var std = s.rawValues.Stdev()
return std return std
} }
func (s *StdDevStream) Truncate() {
s.Slice = generalTruncate(s.Slice)
}

View File

@ -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:]
}