Table of Contents

Previous topic

Financial tools package

Next topic

Metrics module

This Page

Indicators module

Some tools to compute indicators.

fynance.tools.indicators.bollinger_band(series) Compute the bollinger bands.
fynance.tools.indicators.cci(series[, high, …]) Compute Commodity Channel Index _[1].
fynance.tools.indicators.hma(series[, lags, …]) Compute Hull Moving Average.
fynance.tools.indicators.macd_hist(series[, …]) Compute Moving Average Convergence Divergence Histogram.
fynance.tools.indicators.macd_line(series[, …]) Compute Moving Average Convergence Divergence Line.
fynance.tools.indicators.rsi(series[, …]) Compute Relative Strenght Index.
fynance.tools.indicators.signal_line(series) Signal Line for k lags with slow and fast lenght.

Finance indicators

fynance.tools.indicators.bollinger_band(series, lags=21, n_std=2, kind_ma='sma')

Compute the bollinger bands.

Parameters:
series : np.ndarray[dtype=np.float64, ndim=1]

Series.

lags : int, optional

Number of lags for ma, default is 21.

n_std : float (default 1)

Number of standard deviation, default is 1.

kind_ma : {‘ema’, ‘sma’, ‘wma’}

Kind of moving average/standard deviation, default is ‘sma’. - Exponential moving average if ‘ema’. - Simple moving average if ‘sma’. - Weighted moving average if ‘wma’.

Returns:
ma : np.ndarray[dtype=np.float64, ndim=1]

Moving average of series.

std : np.ndarray[dtype=np.float64, ndim=1]

n_std moving standard deviation of series.

See also

z_score, rsi, hma, macd_hist, cci

Examples

>>> series = np.array([60, 100, 80, 120, 160, 80])
>>> mean_vect, std_vect = bollinger_band(series, lags=3)
>>> mean_vect
array([ 60.,  80.,  80., 100., 120., 120.])
>>> std_vect
array([ 0.        , 40.        , 32.65986324, 32.65986324, 65.31972647,
       65.31972647])
fynance.tools.indicators.cci(series, high=None, low=None, lags=20)

Compute Commodity Channel Index _[1].

Parameters:
series : np.ndarray[dtype=np.float64, ndim=1]

Series of close prices.

high, low : np.ndarray[dtype=np.float64, ndim=1], optional

Series of high and low prices, if None then p_t is computed with only closed prices.

lags : int, optional

Number of lags to compute the simple moving average and the mean absolute deviation.

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

Series of commodity channal index.

Notes

CCI is an oscillator introduced by Donald Lamber in 1980. It is calculated as the difference between the typical price of a commodity and its simple moving average, divided by the moving mean absolute deviation of the typical price. The index is usually scaled by an inverse factor of 0.015 to provide more readable numbers:

\[cci = \frac{1}{0.015} \frac{p_t - sma(p_t)}{mad(p_t)} \text{where }p_t = \frac{p_{close} + p_{high} + p_{low}}{3}\]

References

[1]https://en.wikipedia.org/wiki/Commodity_channel_index

Examples

>>> series = np.array([60, 100, 80, 120, 160, 80])
>>> cci(series, lags=3)
array([   0.        ,   66.66666667,    0.        ,  100.        ,
        100.        , -100.        ])
fynance.tools.indicators.hma(series, lags=21, kind_ma='wma')

Compute Hull Moving Average.

Parameters:
series : np.ndarray[dtype=np.float64, ndim=1]

Series of prices or returns.

lags : int, optional

Number of lags for ma, default is 21.

kind_ma : {‘ema’, ‘sma’, ‘wma’}

Kind of moving average, default is ‘wma’. - Exponential moving average if ‘ema’. - Simple moving average if ‘sma’. - Weighted moving average if ‘wma’.

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

Hull moving average of index or returns.

See also

z_score, bollinger_band, rsi, macd_hist, cci

Notes

\[hma = wma(2 \times wma(x, \frac{k}{2}) - wma(x, k), \sqrt{k})\]

Examples

>>> series = np.array([60, 100, 80, 120, 160, 80])
>>> hma(series, lags=3)
array([ 60.        , 113.33333333,  76.66666667, 136.66666667,
       186.66666667,  46.66666667])
fynance.tools.indicators.macd_hist(series, lags=9, fast_ma=12, slow_ma=26, kind_ma='ema')

Compute Moving Average Convergence Divergence Histogram.

Parameters:
series : np.ndarray[dtype=np.float64, ndim=1]

Series of index or returns.

lags : int, optional

Number of lags, default is 9.

fast_ma : int, optional

Number of lags for short ma, default is 12.

slow_ma : int, optional

Number of lags for long ma, default is 26.

kind_ma : {‘ema’, ‘sma’, ‘wma’}

Kind of moving average, default is ‘ema’. - Exponential moving average if ‘ema’. - Simple moving average if ‘sma’. - Weighted moving average if ‘wma’.

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

Moving avg convergence/divergence histogram of index or returns.

See also

z_score, bollinger_band, hma, macd_line, signal_line, cci

Examples

>>> series = np.array([60, 100, 80, 120, 160, 80])
>>> macd_hist(series, lags=3, fast_ma=2, slow_ma=4)
array([ 0.        ,  5.33333361, -0.35555539,  3.9348151 ,  6.41027212,
       -9.47070965])
fynance.tools.indicators.macd_line(series, fast_ma=12, slow_ma=26, kind_ma='ema')

Compute Moving Average Convergence Divergence Line.

Parameters:
series : np.ndarray[dtype=np.float64, ndim=1]

Series of index or returns.

fast_ma : int, optional

Number of lags for short ma, default is 12.

slow_ma : int, optional

Number of lags for long ma, default is 26.

kind_ma : {‘ema’, ‘sma’, ‘wma’}

Kind of moving average, default is ‘ema’. - Exponential moving average if ‘ema’. - Simple moving average if ‘sma’. - Weighted moving average if ‘wma’.

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

Moving avg convergence/divergence line of index or returns.

See also

z_score, bollinger_band, hma, macd_hist, signal_line, cci

Examples

>>> series = np.array([60, 100, 80, 120, 160, 80])
>>> macd_line(series, fast_ma=2, slow_ma=4)
array([ 0.        , 10.66666722,  4.62222282, 12.84740842, 21.73313755,
       -3.61855386])
fynance.tools.indicators.rsi(series, kind_ma='ema', lags=21, alpha=None)

Compute Relative Strenght Index.

Parameters:
series : np.ndarray[dtype=np.float64, ndim=1]

Index series.

kind_ma : {‘ema’, ‘sma’, ‘wma’}

Kind of moving average, default is ‘ema’. - Exponential moving average if ‘ema’. - Simple moving average if ‘sma’. - Weighted moving average if ‘wma’.

lags : int, optional

Number of lagged period, default is 21.

alpha : float, optional

Coefficiant, default is 0.94 corresponding at 20 lags days (only for ‘ema’).

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

Value of RSI for each period.

See also

z_score, bollinger_band, hma, macd_hist, cci

Notes

It is the average gain of upward periods (noted U) divided by the average loss of downward (noted D) periods during the specified time frame, such that :

\[RSI = 100 - \frac{100}{1 + \frac{U}{D}}\]

Examples

>>> series = np.array([60, 100, 80, 120, 160, 80])
>>> rsi(series, lags=3)
array([ 0.        , 99.99999804, 69.59769254, 85.55610891, 91.72201613,
       30.00294321])
fynance.tools.indicators.signal_line(series, lags=9, fast_ma=12, slow_ma=26, kind_ma='ema')

Signal Line for k lags with slow and fast lenght.

Parameters:
series : np.ndarray[dtype=np.float64, ndim=1]

Index or returns ?

lags : int, optional

Number of lags, default is 9.

fast_ma : int, optional

Number of lags for short ma, default is 12.

slow_ma : int, optional

Number of lags for long ma, default is 26.

kind_ma : {‘ema’, ‘sma’, ‘wma’}

Kind of moving average, default is ‘ema’. - Exponential moving average if ‘ema’. - Simple moving average if ‘sma’. - Weighted moving average if ‘wma’.

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

Signal line of index or returns.

See also

z_score, bollinger_band, hma, macd_hist, macd_line, cci

Examples

>>> series = np.array([60, 100, 80, 120, 160, 80])
>>> signal_line(series, lags=3, fast_ma=2, slow_ma=4)
array([ 0.        ,  5.33333361,  4.97777822,  8.91259332, 15.32286544,
        5.85215579])