Skip to content
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. #248

Closed
wants to merge 5 commits into from
Closed

Conversation

cinar
Copy link
Owner

@cinar cinar commented Dec 24, 2024

Describe Request

Smoothed Moving Average (SMMA) Strategy added.

Change Type

New Strategy.

Summary by CodeRabbit

  • New Features

    • Introduced the "Smoothed Moving Average (SMMA)" indicator and strategy to enhance trend analysis options.
    • Added new methods for initializing and computing the SMMA strategy.
    • Implemented a new type for SMMA calculations with default parameters.
  • Bug Fixes

    • Removed unnecessary test data file to streamline testing processes.
  • Tests

    • Established unit tests for the SMMA strategy and its reporting functionalities.
  • Documentation

    • Updated README files to reflect the new SMMA indicator and strategy, including usage instructions.

Copy link

coderabbitai bot commented Dec 24, 2024

Walkthrough

This pull request introduces the Smoothed Moving Average (SMMA) as a new technical indicator and strategy to the trend analysis module. The implementation spans multiple files across the trend and strategy/trend packages, adding new types, methods, and test cases for the SMMA calculation. The changes include creating a generic SMMA implementation, developing a corresponding trading strategy, and updating documentation to reflect the new functionality.

Changes

File Change Summary
README.md Added SMMA indicator and strategy to documentation
strategy/trend/README.md Added documentation for SmmaStrategy
strategy/trend/smma_strategy.go New implementation of SMMA trading strategy
strategy/trend/smma_strategy_test.go Added unit tests for SMMA strategy
strategy/trend/trend.go Registered new SMMA strategy in AllStrategies
trend/README.md Added SMMA type documentation
trend/smma.go Implemented generic SMMA calculation
trend/smma_test.go Added unit tests for SMMA implementation
strategy/testdata/x Removed placeholder test data file

Sequence Diagram

sequenceDiagram
    participant Asset as Asset Snapshots
    participant SMMA as SMMA Indicator
    participant Strategy as SMMA Strategy
    participant Actions as Trading Actions

    Asset ->> SMMA: Provide price data
    SMMA -->> SMMA: Calculate Smoothed Moving Average
    SMMA ->> Strategy: Pass SMMA values
    Strategy ->> Strategy: Compare short and long periods
    Strategy ->> Actions: Generate Buy/Sell/Hold recommendation
Loading

Possibly related PRs

Poem

🐰 A Rabbit's Ode to SMMA 🚀

Smoothed averages dance and sway,
Trends whisper secrets day by day,
With periods short and long we'll play,
Trading wisdom finds its way!
SMMA strategy leads the fray! 📈

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (7)
trend/smma_test.go (2)

14-19: Check for potential CSV reading failure scenarios.

The test is straightforward, but consider negative or edge test cases with missing values, no data, or malformed CSV rows. This ensures robust handling of real-world data.


29-40: Ensure correct idle-period offset validations.

You've correctly skipped values corresponding to the SMMA’s idle period before comparing with expected results. Consider adding a small test to confirm the skip logic if the idle period is changed.

strategy/trend/smma_strategy_test.go (1)

38-55: Comprehensive reporting test.

Generating an HTML report is a nice approach. If feasible, include an automated check or golden file comparison for the generated report to catch formatting regressions.

strategy/trend/smma_strategy.go (3)

25-27: Fix typos in “Smooted Moving Averge” references.
There are spelling errors in the comments: “Smooted” → “Smoothed” and “Averge” → “Average.”

- // Smooted Moving Averge (SMMA).
+ // Smoothed Moving Average (SMMA).

Also applies to: 29-31, 34-35


74-74: Fix minor spelling error in the comment.
“short-perios” should be corrected to “short-period.”

- // A short-perios SMMA value crossing above ...
+ // A short-period SMMA value crossing above ...

128-130: Rename misleading column labels.
"MACD" and "Signal" columns should match SMMA terminology to avoid confusion.

-report.AddColumn(helper.NewNumericReportColumn("MACD", shortSmmas), 1)
-report.AddColumn(helper.NewNumericReportColumn("Signal", longSmmas), 1)
+report.AddColumn(helper.NewNumericReportColumn("Short SMMA", shortSmmas), 1)
+report.AddColumn(helper.NewNumericReportColumn("Long SMMA", longSmmas), 1)
trend/README.md (1)

1452-1459: Add language spec to fenced code blocks.
Markdownlint flagged code blocks lacking a language identifier. Specify a language to comply with style guidelines (e.g., ```go).

Example fix:

- ```
+ ```go

Also applies to: 1474-1475

🧰 Tools
🪛 Markdownlint (0.37.0)

1454-1454: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ee386c8 and 42242cc.

⛔ Files ignored due to path filters (2)
  • strategy/trend/testdata/smma_strategy.csv is excluded by !**/*.csv
  • trend/testdata/smma.csv is excluded by !**/*.csv
📒 Files selected for processing (9)
  • README.md (2 hunks)
  • strategy/testdata/x (0 hunks)
  • strategy/trend/README.md (3 hunks)
  • strategy/trend/smma_strategy.go (1 hunks)
  • strategy/trend/smma_strategy_test.go (1 hunks)
  • strategy/trend/trend.go (1 hunks)
  • trend/README.md (3 hunks)
  • trend/smma.go (1 hunks)
  • trend/smma_test.go (1 hunks)
💤 Files with no reviewable changes (1)
  • strategy/testdata/x
🧰 Additional context used
🪛 Markdownlint (0.37.0)
trend/README.md

1447-1447: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)


1454-1454: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)

🔇 Additional comments (12)
strategy/trend/trend.go (1)

36-36: Add an accompanying test for the new SMMA strategy inclusion.

This newly added call to NewSmmaStrategy is good; however, please ensure test coverage by verifying this new strategy is invoked in integration or e2e tests (if available) to confirm it behaves as expected among existing strategies.

trend/smma_test.go (3)

1-4: Retain a clear license header and project metadata.

These lines properly declare the licensing terms and reference the project repository. No issues detected.


20-28: Verify file path correctness & potential test data issues.

Reading from “testdata/smma.csv” is fine. Ensure the CSV truly has enough columns for both close and SMMA columns, especially across multiple environments.


42-49: Maintain naming consistency in SMMA string representation tests.

The test method name and logic are clear. The same approach for verifying string representation could be extended to other indicators for consistency.

strategy/trend/smma_strategy_test.go (2)

1-4: License & package declaration look good.

No concerns with license format or package naming.


16-36: Validate data completeness in CSV files for strategy checks.

The approach to reading snapshots and result data from CSV is adequate. Validate that the CSV files contain consistent and comprehensive data sets for both snapshots and expected results, especially for varied market scenarios (e.g., bullish, bearish, or flat).

trend/smma.go (3)

1-4: License header alignment.

Maintains the project’s license and documentation approach. Looks consistent with other files.


13-16: Confirm default period alignment with domain expectations.

The default SMMA period of 7 is acceptable. Ensure it aligns with common usage or domain references, as some systems might prefer different defaults (e.g., 9, 10, or 14).


68-76: Idle period & string representation look correct.

The computed idle period (Period - 1) and the SMMA’s string format appear correct.

README.md (1)

51-51: Documentation additions look good.
The new SMMA indicator and strategy references appear consistent and properly hyperlinked.

Also applies to: 125-125

strategy/trend/README.md (1)

88-93: SMMA strategy documentation is well-structured.
No issues found with the added lines—naming, references, and cross-links appear consistent.

trend/README.md (1)

309-316: DefaultSmmaPeriod usage is correct.
The default SMMA period of 7 aligns with the codebase patterns, no improvements needed here.

Comment on lines +46 to +66
// Compute function takes a channel of numbers and computes the SMMA over the specified period.
func (s *Smma[T]) Compute(c <-chan T) <-chan T {
result := make(chan T, cap(c))

go func() {
defer close(result)

// Initial SMMA value is the SMA.
sma := NewSmaWithPeriod[T](s.Period)

before := <-sma.Compute(helper.Head(c, s.Period))
result <- before

for n := range c {
before = ((before * (T(s.Period) - 1)) + n) / T(s.Period)
result <- before
}
}()

return result
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider boundary checks on period and concurrency aspects.

  1. If period is less than 1, the formula might break. Add a small check or note to prevent negative or zero period values.
  2. The channel-based approach is sound, but document concurrency usage or let users know the channel is consumed.

Comment on lines +20 to +21
// DefaultSmmaStrategyLongPeriod is the default short-term SMMA period of 50.
DefaultSmmaStrategyLongPeriod = 50
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Correct the mismatch in documentation for DefaultSmmaStrategyLongPeriod.
The comment incorrectly states that 50 is the default “short-term” period, but this constant is intended for the “long-term” period.

- // DefaultSmmaStrategyLongPeriod is the default short-term SMMA period of 50.
+ // DefaultSmmaStrategyLongPeriod is the default long-term SMMA period of 50.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// DefaultSmmaStrategyLongPeriod is the default short-term SMMA period of 50.
DefaultSmmaStrategyLongPeriod = 50
// DefaultSmmaStrategyLongPeriod is the default long-term SMMA period of 50.
DefaultSmmaStrategyLongPeriod = 50

@cinar cinar closed this Dec 24, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Dec 24, 2024
@cinar cinar deleted the smma-strategy branch December 24, 2024 04:17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant