Skip to content

perf(ta): optimize ta.ema with rollback and gap rebuild#215

Open
yoonbae81 wants to merge 1 commit into
LuxAlgo:mainfrom
yoonbae81:perf/optimize-ta-ema
Open

perf(ta): optimize ta.ema with rollback and gap rebuild#215
yoonbae81 wants to merge 1 commit into
LuxAlgo:mainfrom
yoonbae81:perf/optimize-ta-ema

Conversation

@yoonbae81

Copy link
Copy Markdown
Contributor

Description

Enhanced the core ta.ema implementation to fully support strict Commit & Rollback semantics and a dynamic Gap Rebuild mechanism.

This guarantees correctness, stability, and high performance in both historical backtesting and live streaming environments, resolving potential live-trading bugs where indicators would freeze or fail
to warm up when executed conditionally.

Key Optimizations & Technical Details

  1. Dynamic EMA Gap Rebuild:

    • If the ema indicator is initialized late or called inside conditional scopes (skipping bars), the state no longer fails or produces permanent NaNs.
    • Added the rebuildEma helper to reverse-traverse available history (source.get(i)), calculate a temporary SMA warmup seed, and roll the EMA calculations forward to reconstruct the committed
      state immediately.
  2. Strict Commit & Rollback (Intrabar Recalculation Support):

    • Migrated to store committed states (prevInitCount, prevInitSum, and prevEma) at the end of the previous bar.
    • When an intrabar recalculation occurs, the state rolls back to the committed values, allowing it to correctly recalculate EMA on every tick update (source.get(0)) without state corruption or
      leakage.
  3. Floating-point Drift Prevention:

    • Preserved context.precision() wrapping to maintain exact precision parity with the PineTS test suite.

Testing & Verification

  • Regression Testing: All 15 existing technical analysis moving average tests successfully passed without any regression.
  • New Verification Test: Added a comprehensive regression test EMA - Rollback and Gap Rebuild in moving-averages.test.ts to assert that:
  1. The EMA calculates correct values on SMA warmup.
  2. Intrabar recalculations rollback correctly when tick values update on the same bar.
  3. Gaps and late starts are recovered automatically using history.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant