Skip to content

feat(Delta): add average delta line (SMA/EMA) over the delta histogram - #175

Open
AlbertoAmadorBelchistim wants to merge 2 commits into
AtasPlatform:Developfrom
AlbertoAmadorBelchistim:feat/delta-average-line
Open

AlbertoAmadorBelchistim wants to merge 2 commits into
AtasPlatform:Developfrom
AlbertoAmadorBelchistim:feat/delta-average-line

Conversation

@AlbertoAmadorBelchistim

Copy link
Copy Markdown
Contributor

Summary

Adds an optional moving average overlay on the Delta histogram, hidden by default.

  • Average mode: SMA or EMA over the per-bar delta value.
  • Color mode: Fixed (base color), Zero cross (sign-based), or Slope (direction-based) per-bar line coloring.
  • New Average group on the Visualization tab: ShowAverage, AveragePeriod, AvgMode, AvgColorMode, AverageColor, AvgSlopeUpColor, AvgSlopeDownColor, AverageWidth. All properties are tagged with [Tab] following the settings categorization.

Implementation notes

  • O(1) rolling SMA (queue + running sum) and standard EMA; state resets at bar 0, no look-ahead.
  • The average series is appended at the end of DataSeries and stays hidden unless enabled, so existing templates and workspaces are unaffected.
  • The diff is additive-only (237 added lines, no reformatting) on top of current Develop.

Localization

Property names, group and tab reuse existing resources: Strings.ShowAverage, Strings.Average, Strings.AveragePeriod, Strings.CalculationMode, Strings.SMA, Strings.EMA, Strings.Width.

The color-mode strings have no matching keys in OFT.Localization yet, so they are declared inline following the same transitional pattern as the Half Gap settings in DailyLines, ready to be swapped once keys exist:

Text Where
Color mode AvgColorMode display name
Base color AverageColor display name
Slope up color / Slope down color slope color display names
Fixed / Zero cross / Slope AverageColorMode enum members

Testing

Built against Alpha configuration with 0 errors. Smoke-tested on chart: toggle on/off, SMA↔EMA switch, all three color modes, period bounds (1 and 1000).

image

@Stig4all

Stig4all commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

The moving-average calculation needs correction before merge.
OnCalculate can run repeatedly for the same forming bar. The SMA currently enqueues each call as another sample, while the EMA advances its accumulated state on every call. Both results therefore depend on how many updates a bar receives, rather than only on the per-bar delta values.
For example, with SMA(3), bars containing 10, 20, 30, and the last bar subsequently changing to 60, the implementation produces approximately 36.67 instead of 30.
Please make both calculations handle repeated updates to the same bar and verify that live results match a full historical recalculation. The remaining inline UI strings also need localization.

Adds a configurable moving average overlay on the delta series:

- AverageMode enum (SMA / EMA), localized via existing Strings keys.
- AverageColorMode enum (Fixed / Zero cross / Slope) controlling the
  per-bar line color: fixed base color, sign-based, or slope-based.
- Properties in a new Average group on the Visualization tab, tagged
  with [Tab] to follow the settings categorization: ShowAverage,
  AveragePeriod, AvgMode, AvgColorMode, AverageColor, AvgSlopeUpColor,
  AvgSlopeDownColor, AverageWidth.
- Property names, group and tab reuse existing localization resources
  (Strings.ShowAverage, Strings.Average, Strings.AveragePeriod,
  Strings.CalculationMode, Strings.SMA, Strings.EMA, Strings.Width).
  The four color-mode strings and three enum member names have no
  matching keys in OFT.Localization yet, so they are declared inline,
  ready to be swapped to resource keys once they exist.
- O(1) rolling SMA (queue + running sum) and standard EMA with state
  reset at bar 0; no look-ahead.
- Hidden by default; the series is appended at the end of DataSeries,
  so existing templates are unaffected.
OnCalculate runs repeatedly for the forming bar. The SMA enqueued every call as
a new sample and the EMA advanced its state on every call, so the live values
depended on the number of updates a bar received.

Both averages are now computed from per-bar values only: the input is stored by
bar index, the SMA keeps a running sum per bar and the EMA starts from the stored
value of the previous bar. Recalculating the forming bar any number of times gives
the same result as a full historical recalculation.

The inline UI strings are replaced with existing localization keys. The color
options follow the built-in SMA/EMA indicators (Colored direction with bullish
and bearish colors); the line color and width are set on the series itself.
@AlbertoAmadorBelchistim

Copy link
Copy Markdown
Contributor Author

Thanks, fixed:

  • Both averages now depend only on per-bar values. The input is stored by bar index, the SMA keeps a running sum per bar (sum[bar] = sum[bar-1] + x[bar] - x[bar-N]), and the EMA starts from the stored value of the previous bar. Repeated OnCalculate calls for the forming bar overwrite the sample instead of adding one. Your example (SMA(3), 10, 20, 30 → 60) now gives 30.
  • Verified with a harness that runs the actual CalculateAverage with 1–30 random updates per bar over 1,500 bars and compares it with a single-pass historical calculation and a textbook SMA/EMA, for periods 1, 3, 20 and 200. Live and historical values (and colors) are identical.
  • Localization: the inline strings are gone. The coloring options now follow the built-in SMA/EMA indicators (ColoredDirection, BullishColor, BearishColor, all existing keys); the line color and width are configured on the series itself.

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.

2 participants