Skip to content

Improvement: Add slice-based calculation methods for high-performance batch processing #371

@cinar

Description

@cinar

Description

Currently, all indicators in the library are tightly coupled to channels and stream-based processing. While channels are excellent for real-time streaming data, batch operations (such as running backtests over historical datasets loaded from CSVs) can incur scheduling, allocation, and context-switching overhead from spawning multiple goroutines and passing values through channels.

To improve performance for batch calculations and backtesting, we should introduce a way to compute indicators directly on slices with zero goroutines.

Proposed Changes

  1. Define a stateful calculation interface (e.g., Calculator or similar step-by-step interface) representing the state of an indicator:
    type Calculator[T Number] interface {
        Next(input T) (output T, ok bool)
    }
  2. Implement this interface on existing indicator structs (like Sma, Ema, etc.).
  3. Add a new ComputeSlice(inputs []T) []T method to the indicators, which processes the inputs sequentially in a simple loop without channels.
  4. Keep the existing channel-based Compute(<-chan T) <-chan T methods intact for backwards compatibility, but rewrite their internals to utilize the stateful calculator logic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions