Metrics module

#### Next topic

Financial models package

# Momentums module¶

Some tools to compute moving statistic momentums.

 fynance.tools.momentums.ema(series[, alpha, …]) Exponential moving average along k lags. fynance.tools.momentums.emstd(series[, …]) Exponential moving standard deviation along k lags. fynance.tools.momentums.sma(series[, lags]) Simple moving average along k lags. fynance.tools.momentums.smstd(series[, lags]) Simple moving standard deviation along k lags. fynance.tools.momentums.wma(series[, lags]) Weighted moving average along k lags. fynance.tools.momentums.wmstd(series[, lags]) Weighted moving standard deviation along k lags.

## Moving averages¶

fynance.tools.momentums.ema(series, alpha=0.94, lags=None)

Exponential moving average along k lags.

$ema_t = \alpha \times ema_{t-1} + (1-\alpha) \times series_t$
Parameters: series : np.ndarray[dtype=np.float64, ndim=1] Index or returns. alpha : float, optional Multiplier, default is 0.94 corresponding at 20 lags memory. lags : int, optional Number of days. np.ndarray[dtype=np.float64, ndim=1] Moving average of series.

Notes

If lags is specified $$\alpha = 1 - \frac{2}{1 + k}$$

Examples

>>> series = np.array([60, 100, 80, 120, 160, 80])
>>> ema(series, lags=3)
array([ 60.,  80.,  80., 100., 130., 105.])
>>> ema(series, alpha=0.5)
array([ 60.,  80.,  80., 100., 130., 105.])

fynance.tools.momentums.sma(series, lags=21)

Simple moving average along k lags.

$sma_t = \frac{1}{k} \sum^{k-1}_{i=0} series_{t-i}$
Parameters: series : np.ndarray[dtype=np.float64, ndim=1] Index or returns. lags : int, optional Number of lags for ma, default is 21. np.ndarray[dtype=np.float64, ndim=1] Vector of moving average of series.

Examples

>>> series = np.array([60, 100, 80, 120, 160, 80])
>>> sma(series, lags=3)
array([ 60.,  80.,  80., 100., 120., 120.])

fynance.tools.momentums.wma(series, lags=21)

Weighted moving average along k lags.

$wma_t = \frac{2}{k (k-1)} \sum^{k-1}_{i=0} (k-i) \times series_{t-i}$
Parameters: series : np.ndarray[dtype=np.float64, ndim=1] Index or returns. lags : int, optional Number of lags for ma, default is 21. np.ndarray[dtype=np.float64, ndim=1] Moving average of series.

Examples

>>> series = np.array([60, 100, 80, 120, 160, 80])
>>> wma(series, lags=3)
array([ 60.        ,  86.66666667,  83.33333333, 103.33333333,
133.33333333, 113.33333333])


## Moving standard deviation¶

fynance.tools.momentums.emstd(series, alpha=0.94, lags=None)

Exponential moving standard deviation along k lags.

$emstd_t = \sqrt{\alpha\times emstd_{t-1}^2+(1-\alpha)\times series_t^2}$
Parameters: series : np.ndarray[dtype=np.float64, ndim=1] Index or returns. alpha : float, optional Multiplier, default is 0.94 corresponding at 20 lags memory. lags : int, optional Number of days. np.ndarray[dtype=np.float64, ndim=1] Moving standard deviation of series.

Notes

If lags is specified $$\alpha = 1 - \frac{2}{1 + k}$$

Examples

>>> series = np.array([60, 100, 80, 120, 160, 80])
>>> emstd(series, lags=3)
array([ 0.        , 28.28427125, 20.        , 31.6227766 , 47.95831523,
48.98979486])

fynance.tools.momentums.smstd(series, lags=21)

Simple moving standard deviation along k lags.

$smstd_t = \sqrt{\frac{1}{k} \sum^{k-1}_{i=0} (p_{t-i} - sma_t)^2}$
Parameters: series : np.ndarray[dtype=np.float64, ndim=1] Index or returns. lags : int, optional Number of lags for ma, default is 21. np.ndarray[dtype=np.float64, ndim=1] Moving standard deviation of series.

Examples

>>> series = np.array([60, 100, 80, 120, 160, 80])
>>> smstd(series, lags=3)
array([ 0.        , 20.        , 16.32993162, 16.32993162, 32.65986324,
32.65986324])

fynance.tools.momentums.wmstd(series, lags=21)

Weighted moving standard deviation along k lags.

\begin{align}\begin{aligned}wma_t = \frac{2}{k (k-1)} \sum^{k-1}_{i=0} (k-i) \times series_{t-i}\\wmstd_t = \sqrt{\frac{2}{k(k-1)} \sum^{k-1}_{i=0} (k-i) \times (series_{t-i} - wma_t)^2}\end{aligned}\end{align}
Parameters: series : np.ndarray[dtype=np.float64, ndim=1] Series of prices, index or returns. lags : int, optional Number of days, default is 21. np.ndarray[dtype=np.float64, ndim=1] Moving standard deviation of series.

Examples

>>> series = np.array([60, 100, 80, 120, 160, 80])
>>> wmstd(series, lags=3)
array([ 0.        , 18.85618083, 13.74368542, 17.95054936, 29.8142397 ,
35.90109871])