Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions pkg/indicator/v2/max.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package indicatorv2

import (
"github.com/c9s/bbgo/pkg/types"
)

// MAXStream calculates the maximum value in a window from a float number stream.
type MAXStream struct {
*types.Float64Series

window int
rawValues *types.Queue
}

func MAX(source types.Float64Source, window int) *MAXStream {
s := &MAXStream{
Float64Series: types.NewFloat64Series(),
window: window,
rawValues: types.NewQueue(window),
}

s.Bind(source, s)
return s
}

func (s *MAXStream) Calculate(v float64) float64 {
s.rawValues.Update(v)
return types.Max(s.rawValues, s.window)
}

func (s *MAXStream) Truncate() {
s.Slice = types.ShrinkSlice(s.Slice, MaxSliceSize, TruncateSize)
}
37 changes: 37 additions & 0 deletions pkg/indicator/v2/max_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package indicatorv2

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/c9s/bbgo/pkg/types"
)

func TestMAX(t *testing.T) {
source := types.NewFloat64Series()
maxIndicator := MAX(source, 3)

// Test case 1: Fill the window
source.PushAndEmit(1.0)
assert.Equal(t, 1.0, maxIndicator.Last(0))

source.PushAndEmit(3.0)
assert.Equal(t, 3.0, maxIndicator.Last(0))

source.PushAndEmit(2.0)
assert.Equal(t, 3.0, maxIndicator.Last(0))

// Test case 2: Sliding window
source.PushAndEmit(1.0)
// Window is [3.0, 2.0, 1.0], max is 3.0
assert.Equal(t, 3.0, maxIndicator.Last(0))

source.PushAndEmit(0.5)
// Window is [2.0, 1.0, 0.5], max is 2.0
assert.Equal(t, 2.0, maxIndicator.Last(0))

source.PushAndEmit(4.0)
// Window is [1.0, 0.5, 4.0], max is 4.0
assert.Equal(t, 4.0, maxIndicator.Last(0))
}
Loading