perf(ta): optimize ta.ema with rollback and gap rebuild#215
Open
yoonbae81 wants to merge 1 commit into
Open
Conversation
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Enhanced the core
ta.emaimplementation 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
Dynamic EMA Gap Rebuild:
emaindicator is initialized late or called inside conditional scopes (skipping bars), the state no longer fails or produces permanent NaNs.rebuildEmahelper to reverse-traverse available history (source.get(i)), calculate a temporary SMA warmup seed, and roll the EMA calculations forward to reconstruct the committedstate immediately.
Strict Commit & Rollback (Intrabar Recalculation Support):
prevInitCount,prevInitSum, andprevEma) at the end of the previous bar.source.get(0)) without state corruption orleakage.
Floating-point Drift Prevention:
context.precision()wrapping to maintain exact precision parity with the PineTS test suite.Testing & Verification
EMA - Rollback and Gap Rebuildinmoving-averages.test.tsto assert that: