EWMA¶
quantflow.ta.ewma.EWMA
pydantic-model
¶
Bases: BaseModel
Exponentially Weighted Moving Average filter for time series data.
This implementation uses the standard EWMA formula:
where \(\alpha\) is the smoothing factor derived from the period parameter. The period represents the effective averaging window of the exponential decay, defined as the half-life \(h\) divided by \(\ln 2\):
For an exponential decay with half-life \(h\), the sum of all weights equals \(h / \ln 2\), making the period the continuous-time equivalent of the number of observations in a simple moving average. The smoothing factor is:
where \(p\) is the period. This definition makes the period directly comparable to the period used in SuperSmoother.
If tau is provided, EWMA becomes asymmetric: for up-moves the update uses \(\alpha \cdot \tau\), while for down-moves it uses \(\alpha \cdot (1-\tau)\). This is useful when you want different reaction speeds to rising and falling values.
Example¶
import pandas as pd
ewma = EWMA(period=10)
df = pd.DataFrame({"value": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]})
df["ewma"] = df["value"].apply(EWMA(period=10).update)
For online updates:
Fields:
tau
pydantic-field
¶
Optional asymmetry control. For increasing values use alphatau; for decreasing values use alpha(1-tau)
half_life
property
¶
Get the half-life corresponding to the current period.
from_half_life
classmethod
¶
Create an EWMA using half-life semantics instead of period.
The half-life represents the time for weight to decay to 0.5.
Source code in quantflow/ta/ewma.py
from_alpha
classmethod
¶
Create an EWMA directly from a specified alpha value.
The period is computed as the inverse of:
Source code in quantflow/ta/ewma.py
update
¶
Update the filter with a new value and return the smoothed result.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
New data point to add to the filter
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Smoothed value using the EWMA algorithm |