-
-
Notifications
You must be signed in to change notification settings - Fork 303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FEAT: Add SMMA indicator #1553
Merged
Merged
FEAT: Add SMMA indicator #1553
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package indicatorv2 | ||
|
||
import ( | ||
"github.com/c9s/bbgo/pkg/types" | ||
) | ||
|
||
type SMMAStream struct { | ||
*types.Float64Series | ||
window int | ||
rawValues *types.Queue | ||
source types.Float64Source | ||
} | ||
|
||
func SMMA2(source types.Float64Source, window int) *SMMAStream { | ||
s := &SMMAStream{ | ||
Float64Series: types.NewFloat64Series(), | ||
window: window, | ||
rawValues: types.NewQueue(window), | ||
source: source, | ||
} | ||
s.Bind(source, s) | ||
return s | ||
} | ||
|
||
func (s *SMMAStream) Calculate(v float64) float64 { | ||
var out float64 | ||
sourceLen := s.source.Length() | ||
|
||
if sourceLen < s.window { | ||
// Until we reach the end of the period, sum the prices. | ||
|
||
// First, calculate the sum, and it will be automatically saved too. | ||
s.rawValues.Sum(s.window) | ||
// Then save the input value to use it later on. | ||
s.rawValues.Update(v) | ||
} else if sourceLen == s.window { | ||
// We need the SMA for the first time. | ||
s.rawValues.Update(v) | ||
out = s.rawValues.Mean(s.window) | ||
} else { | ||
// For all the rest values, just use the formula. | ||
last := s.Slice.Last(0) | ||
out = (last*float64((s.window-1.0)) + v) / float64(s.window) | ||
} | ||
|
||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package indicatorv2 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/c9s/bbgo/pkg/types" | ||
) | ||
|
||
func TestSMMA(t *testing.T) { | ||
source := types.NewFloat64Series() | ||
smma := SMMA2(source, 3) | ||
|
||
data := []float64{10, 20, 30, 40, 50, 60, 70, 80, 90} | ||
for _, d := range data { | ||
source.PushAndEmit(d) | ||
} | ||
|
||
// Assert the first 3 and last 3 value outputs. | ||
assert.InDelta(t, 0, smma.Last(len(data)-1), 0.001) | ||
assert.InDelta(t, 0, smma.Last(len(data)-2), 0.001) | ||
assert.InDelta(t, 20, smma.Last(len(data)-3), 0.001) | ||
assert.InDelta(t, 51.97530864197531, smma.Last(2), 0.001) | ||
assert.InDelta(t, 61.31687242798355, smma.Last(1), 0.001) | ||
assert.InDelta(t, 70.87791495198904, smma.Last(0), 0.001) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could be just SMMA here :-)