Exponential Moving Average Helper
Track a smoothed moving average with a period-derived smoothing factor, plus a windowed simple average for comparison.
Code
from collections.abc import Iterable
from dataclasses import dataclass, field
@dataclass
class EmaTracker:
period: int
value: float | None = None
alpha: float = field(init=False, default=0.0)
def __post_init__(self) -> None:
if self.period < 1:
raise ValueError("period must be at least 1")
self.alpha = 2.0 / (self.period + 1)
def update(self, sample: float) -> float:
if self.value is None:
self.value = sample
else:
self.value = self.alpha * sample + (1 - self.alpha) * self.value
return self.value
def reset(self) -> None:
self.value = None
def exponential_moving_average(values: Iterable[float], period: int) -> list[float]:
tracker = EmaTracker(period)
return [tracker.update(value) for value in values]
def simple_moving_average(values: list[float], window: int) -> list[float | None]:
if window < 1:
raise ValueError("window must be at least 1")
smoothed: list[float | None] = []
for index in range(len(values)):
if index + 1 < window:
smoothed.append(None)
else:
chunk = values[index + 1 - window : index + 1]
smoothed.append(sum(chunk) / window)
return smoothed
def main() -> None:
samples = [10.0, 12.0, 11.0, 15.0, 14.0, 16.0]
print("ema:", [round(value, 3) for value in exponential_moving_average(samples, 3)])
print("sma:", simple_moving_average(samples, 3))
if __name__ == "__main__":
main()How the snippet works
Exponential Moving Average Helper is built around the class class and the functions __post_init__, update, reset and exponential_moving_average.
Repetition is confined to for index in range(len(values)) — the rest of the file is straight-line. ValueError is the guard’s signal — thrown at the branch, before any partial work happens.
The result is a fresh construction, so callers can keep their originals.
Points of care
- The guard
self.period < 1short-circuits the flow with an earlyraisewhen its condition holds.
Using it
Entry signature: def update(self, sample: float) -> float
Look for the ema:, sma: prefixes when the file runs; they tie each line to its step.
main sequences exponential_moving_average and simple_moving_average; executing the file runs that flow once.
Key statements
def __post_init__(self) -> None:
if self.period < 1:
raise ValueError("period must be at least 1")
self.alpha = 2.0 / (self.period + 1)
def reset(self) -> None:
self.value = None
def main() -> None:
samples = [10.0, 12.0, 11.0, 15.0, 14.0, 16.0]
print("ema:", [round(value, 3) for value in exponential_moving_average(samples, 3)])
print("sma:", simple_moving_average(samples, 3))
Interactive Tools for This Topic
FAQ
- Does `update` modify its input?
- No. Nothing passed in is written to, which makes the call safe on shared data.
- What does the snippet do with malformed data?
- It throws `ValueError` before any real work runs, via the guard `self.period < 1` — handle it at the call site or let it propagate deliberately.
- What does Exponential Moving Average Helper print when run?
- The script runs standalone; its 2 labeled outputs include `ema:`, `sma:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.