-
-
Notifications
You must be signed in to change notification settings - Fork 110
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
Smoothed Moving Average (SMMA) Strategy added. #249
Merged
Merged
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
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
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,136 @@ | ||
// Copyright (c) 2021-2024 Onur Cinar. | ||
// The source code is provided under GNU AGPLv3 License. | ||
// https://github.com/cinar/indicator | ||
|
||
package trend | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cinar/indicator/v2/asset" | ||
"github.com/cinar/indicator/v2/helper" | ||
"github.com/cinar/indicator/v2/strategy" | ||
"github.com/cinar/indicator/v2/trend" | ||
) | ||
|
||
const ( | ||
// DefaultSmmaStrategyShortPeriod is the default short-term SMMA period of 20. | ||
DefaultSmmaStrategyShortPeriod = 20 | ||
|
||
// DefaultSmmaStrategyLongPeriod is the default short-term SMMA period of 50. | ||
DefaultSmmaStrategyLongPeriod = 50 | ||
) | ||
|
||
// SmmaStrategy represents the configuration parameters for calculating the | ||
// Smooted Moving Averge (SMMA) strategy. A short-term SMMA crossing above | ||
// the long-term SMMA suggests a bullish trend, while crossing below the | ||
// long-term SMMA indicates a bearish trend. | ||
type SmmaStrategy struct { | ||
// ShortSmma represents the configuration parameters for calculating the | ||
// short-term Smooted Moving Averge (SMMA). | ||
ShortSmma *trend.Smma[float64] | ||
|
||
// LongSmma represents the configuration parameters for calculating the | ||
// long-term Smooted Moving Averge (SMMA). | ||
LongSmma *trend.Smma[float64] | ||
} | ||
|
||
// NewSmmaStrategy function initializes a new SMMA strategy instance. | ||
func NewSmmaStrategy() *SmmaStrategy { | ||
return NewSmmaStrategyWith( | ||
DefaultSmmaStrategyShortPeriod, | ||
DefaultSmmaStrategyLongPeriod, | ||
) | ||
} | ||
|
||
// NewSmmaStrategyWith function initializes a new SMMA strategy instance with the given parameters. | ||
func NewSmmaStrategyWith(shortPeriod, longPeriod int) *SmmaStrategy { | ||
return &SmmaStrategy{ | ||
ShortSmma: trend.NewSmmaWithPeriod[float64](shortPeriod), | ||
LongSmma: trend.NewSmmaWithPeriod[float64](longPeriod), | ||
} | ||
} | ||
|
||
// Name returns the name of the strategy. | ||
func (s *SmmaStrategy) Name() string { | ||
return fmt.Sprintf("SMMA Strategy (%d,%d)", | ||
s.ShortSmma.Period, | ||
s.LongSmma.Period, | ||
) | ||
} | ||
|
||
// Compute processes the provided asset snapshots and generates a stream of actionable recommendations. | ||
func (s *SmmaStrategy) Compute(snapshots <-chan *asset.Snapshot) <-chan strategy.Action { | ||
closingsSplice := helper.Duplicate(asset.SnapshotsAsClosings(snapshots), 2) | ||
|
||
shortSmmas := s.ShortSmma.Compute(closingsSplice[0]) | ||
longSmmas := s.LongSmma.Compute(closingsSplice[1]) | ||
|
||
commonPeriod := helper.CommonPeriod(s.ShortSmma.Period, s.LongSmma.Period) | ||
shortSmmas = helper.SyncPeriod(commonPeriod, s.ShortSmma.Period, shortSmmas) | ||
longSmmas = helper.SyncPeriod(commonPeriod, s.LongSmma.Period, longSmmas) | ||
|
||
actions := helper.Operate(shortSmmas, longSmmas, func(shortSmma, longSmma float64) strategy.Action { | ||
// A short-perios SMMA value crossing above long-period SMMA suggests a bullish trend. | ||
if shortSmma > longSmma { | ||
return strategy.Buy | ||
} | ||
|
||
// A short-period SMMA value crossing below long-period SMMA suggests a bearish trend. | ||
if longSmma > shortSmma { | ||
return strategy.Sell | ||
} | ||
|
||
return strategy.Hold | ||
}) | ||
|
||
// SMMA strategy starts only after a full period. | ||
actions = helper.Shift(actions, commonPeriod, strategy.Hold) | ||
|
||
return actions | ||
} | ||
|
||
// Report processes the provided asset snapshots and generates a | ||
// report annotated with the recommended actions. | ||
func (s *SmmaStrategy) Report(c <-chan *asset.Snapshot) *helper.Report { | ||
// | ||
// snapshots[0] -> dates | ||
// snapshots[1] -> closings[0] -> closings | ||
// closings[1] -> short-period SMMA | ||
// closings[2] -> long-period SMMA | ||
// snapshots[2] -> actions -> annotations | ||
// -> outcomes | ||
// | ||
snapshots := helper.Duplicate(c, 3) | ||
|
||
dates := asset.SnapshotsAsDates(snapshots[0]) | ||
closings := helper.Duplicate(asset.SnapshotsAsClosings(snapshots[1]), 3) | ||
|
||
shortSmmas := s.ShortSmma.Compute(closings[1]) | ||
longSmmas := s.LongSmma.Compute(closings[2]) | ||
|
||
actions, outcomes := strategy.ComputeWithOutcome(s, snapshots[2]) | ||
annotations := strategy.ActionsToAnnotations(actions) | ||
outcomes = helper.MultiplyBy(outcomes, 100) | ||
|
||
commonPeriod := helper.CommonPeriod(s.ShortSmma.Period, s.LongSmma.Period) | ||
dates = helper.SyncPeriod(commonPeriod, 0, dates) | ||
closings[0] = helper.Skip(closings[0], commonPeriod) | ||
shortSmmas = helper.SyncPeriod(commonPeriod, s.ShortSmma.Period, shortSmmas) | ||
longSmmas = helper.SyncPeriod(commonPeriod, s.LongSmma.Period, longSmmas) | ||
annotations = helper.Skip(annotations, commonPeriod) | ||
outcomes = helper.Skip(outcomes, commonPeriod) | ||
|
||
report := helper.NewReport(s.Name(), dates) | ||
report.AddChart() | ||
report.AddChart() | ||
|
||
report.AddColumn(helper.NewNumericReportColumn("Close", closings[0])) | ||
report.AddColumn(helper.NewNumericReportColumn("MACD", shortSmmas), 1) | ||
report.AddColumn(helper.NewNumericReportColumn("Signal", longSmmas), 1) | ||
report.AddColumn(helper.NewAnnotationReportColumn(annotations), 0, 1) | ||
|
||
report.AddColumn(helper.NewNumericReportColumn("Outcome", outcomes), 2) | ||
|
||
return report | ||
} |
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,56 @@ | ||
// Copyright (c) 2021-2024 Onur Cinar. | ||
// The source code is provided under GNU AGPLv3 License. | ||
// https://github.com/cinar/indicator | ||
|
||
package trend_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/cinar/indicator/v2/asset" | ||
"github.com/cinar/indicator/v2/helper" | ||
"github.com/cinar/indicator/v2/strategy" | ||
"github.com/cinar/indicator/v2/strategy/trend" | ||
) | ||
|
||
func TestSmmaStrategy(t *testing.T) { | ||
snapshots, err := helper.ReadFromCsvFile[asset.Snapshot]("testdata/brk-b.csv", true) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
results, err := helper.ReadFromCsvFile[strategy.Result]("testdata/smma_strategy.csv", true) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
expected := helper.Map(results, func(r *strategy.Result) strategy.Action { return r.Action }) | ||
|
||
smma := trend.NewSmmaStrategy() | ||
actual := smma.Compute(snapshots) | ||
|
||
err = helper.CheckEquals(actual, expected) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestSmmaStrategyReport(t *testing.T) { | ||
snapshots, err := helper.ReadFromCsvFile[asset.Snapshot]("testdata/brk-b.csv", true) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
smma := trend.NewSmmaStrategy() | ||
|
||
report := smma.Report(snapshots) | ||
|
||
fileName := "smma_strategy.html" | ||
defer os.Remove(fileName) | ||
|
||
err = report.WriteToFile(fileName) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
Oops, something went wrong.
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.
💡 Codebase verification
Test data files are present but paths need to be updated
The test is using incorrect relative paths. The actual files are located at:
The test needs to be updated to use the correct paths relative to the test file location.
🔗 Analysis chain
Solid test coverage for SMMA strategy computation.
• The CSV-based testing approach provides strong validation.
• Good use of helper functions for reading input and verifying outputs.
• Consider verifying that “testdata/smma_strategy.csv” is present and correctly named in the repository.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 307
Script:
Length of output: 3922