sma

Defined in fynance.features.momentums

sma(X, w=None, axis=0, dtype=None)[source]

Compute simple moving average(s) of size w for each X’ series.

Equally weighted average over a sliding window of length w. Reacts slowly to new information but is robust to noise and is the most common smoother in technical analysis. For a smoother that reacts faster to recent observations, see ema (exponential weighting) or wma (linear weighting).

The first w-1 values use a shrinking window (i.e. sma_t for t < w-1 averages only the available observations, never NaN).

\[sma^w_t(X) = \frac{1}{w} \sum^{w-1}_{i=0} X_{t-i}\]
Parameters:
Xnp.ndarray[dtype, ndim=1 or 2]

Elements to compute the moving average.

wint, optional

Size of the lagged window of the moving average, must be positive. If w is None or w=0, then w=X.shape[axis]. Default is None.

axis{0, 1}, optional

Axis along wich the computation is done. Default is 0.

dtypenp.dtype, optional

The type of the output array. If dtype is not given, infer the data type from X input.

Returns:
np.ndarray[dtype, ndim=1 or 2]

Simple moving average of each series.

See also

wma, ema, smstd

Examples

>>> X = np.array([60, 100, 80, 120, 160, 80])
>>> sma(X, w=3, dtype=np.float64, axis=0)
array([ 60.,  80.,  80., 100., 120., 120.])
>>> X = np.array([[60, 60], [100, 100], [80, 80],
...               [120, 120], [160, 160], [80, 80]])
>>> sma(X, w=3, dtype=np.float64, axis=0)
array([[ 60.,  60.],
       [ 80.,  80.],
       [ 80.,  80.],
       [100., 100.],
       [120., 120.],
       [120., 120.]])
>>> sma(X, w=3, dtype=np.float64, axis=1)
array([[ 60.,  60.],
       [100., 100.],
       [ 80.,  80.],
       [120., 120.],
       [160., 160.],
       [ 80.,  80.]])