Indicators

Warning

All of qufilab’s technical indicators are implemented in c++ and a big part of the speed performance comes from the fact that no type conversion exist between python and c++. In order for this to work, numpy arrays of type numpy.dtype.float64 or numpy.dtype.float32 are preferably used. Observe that all other types of numpy arrays are accepted, however the returned numpy array will be converted into the type numpy.dtype.float64.

Trend

Double Exponential Moving Average

qufilab.dema(data, periods)
Parameters
datandarray

An array containing values.

periodsint

Number of periods to be used.

Returns
ndarray

An array containing calculated double exponential moving average values.

Notes

\[dema_K = 2 \cdot ema_K - ema(ema_K)\]

Examples

>>> import qufilab as ql
>>> import numpy as np
... 
>>> # Load a sample dataframe.
>>> df = ql.load_sample('MSFT')
>>> print(df['close'].dtype)
float64
>>> dema = ql.ema(df['close'], periods = 10)
>>> print(dema)
[nan nan nan ... 210.18599259 211.72078484 212.32702478]

Exponential Moving Average

qufilab.ema(data, periods)
Parameters
datandarray

An array containing values.

periodsint

Number of periods to be used.

Returns
ndarray

An array containing calculated exponential moving average values.

Notes

The calculation of ema can be written as a power series:

\[ema_K = \alpha[price_K + (1-\alpha)price_{K-1} + \ (1-\alpha)^2price_{K-2} + ... + (1-\alpha)^{K-(n-1)}price_{K-(n-1)},\]

which also can be written as:

\[ema_K = ema_{K-1} + \alpha[price_K - ema_{K-1}],\]

where \(\alpha = \frac{2}{n+1}\) is the multiplier and depends on the period n. Observe that for the first ema value, a simple moving average is used.

Examples

>>> import qufilab as ql
>>> import numpy as np
... 
>>> # Load a sample dataframe.
>>> df = ql.load_sample('MSFT')
>>> print(df['close'].dtype)
float64
>>> ema = ql.ema(df['close'], periods = 10)
>>> print(ema)
[nan nan nan ... 209.63323895 210.53265005 210.98489549]

Linear Weighted Moving Average

qufilab.lwma(data, periods)
Parameters
datandarray

An array containing values.

periodsint

Number of periods to be used.

Returns
ndarray

An array containing calculated linear weighted moving average values.

Notes

The linear weighted moving average is calculated similar to the simple moving average, except that the values aren’t equally weighted, but linearly weighted with the highest weight going first and then decreasing linearly. This implementation use the number of periods as the highest weight, and thereafter decreasing down to one.

\[lwma_K = \frac{price_K \cdot w_n + price_{K-1} \cdot w_{n-1} \ + ... + price_{K-(n-1)} \cdot w_1}{\sum_{i=1}^n w_i},\]

where \(w_1 = 1, w_2 = 2,... w_n = n\)

Examples

>>> import qufilab as ql
>>> import numpy as np
... 
>>> # Load a sample dataframe.
>>> df = ql.load_sample('MSFT')
>>> print(df['close'].dtype)
float64
>>> lwma = ql.lwma(df['close'], periods = 10)
>>> print(lwma)
[nan nan nan ... 209.50327273 210.35927273 210.96381818]

Simple Moving Average

qufilab.sma(data, periods)
Parameters
datandarray

An array containing values.

periodsint

Number of periods to be used.

Returns
ndarray

An array containing calculated simple moving average values.

Notes

The calculation of sma is a equally weighted mean for the last n days.

\[sma_K = \frac{price_K + price_{K-1} + ... + price_{K-(n-1)}}{n} = \frac{1}{n}\sum_{i=0}^{n-1} price_{K-i}\]

Examples

>>> import qufilab as ql
>>> import numpy as np
... 
>>> # Load a sample dataframe.
>>> df = ql.load_sample('MSFT')
>>> print(df['close'].dtype)
float64
>>> sma = ql.sma(df['close'], periods = 10)
>>> print(sma)
[nan nan nan ... 209.872 209.695 209.749]

Smoothed Moving Average

qufilab.smma(data, periods)
Parameters
datandarray

An array containing values.

periodsint

Number of periods to be used.

Returns
ndarray

An array containing calculated smoothed moving average values.

Examples

>>> import qufilab as ql
>>> import numpy as np
... 
>>> # Load a sample dataframe.
>>> df = ql.load_sample('MSFT')
>>> print(df['close'].dtype)
float64
>>> smma = ql.smma(df['close'], periods = 10)
>>> print(smma)
[nan nan nan ... 208.85810754 209.43029679 209.78926711]

T3 Moving Average

qufilab.t3(data, periods, volume_factor=0.7)
Parameters
datandarray

An array containing values.

periodsint

Number of periods to be used.

volume_factorfloat, optional

What volume factor to be used when calculating the constants. See Notes below for implementation.

Returns
ndarray

An array containing calculated t3 moving average values.

Notes

The t3 moving average indicator utilize many different exponential moving averages and is calculated with:

\[ \begin{align}\begin{aligned}t3 = c_1e_6 + c_2e_5 + c_3e_4 + c_4e_3\\e_1 = ema(data)\\e_2 = ema(e_1)\\e_3 = ema(e_2)\\e_4 = ema(e_3)\\e_5 = ema(e_4)\\e_6 = ema(e_5)\\c_1 = -a^3\\c_2 = 3a^2 + 3a^3\\c_3 = –6a^2–3a–3a^3\\c_4 = 1 + 3a + a^3 + 3a^2\end{aligned}\end{align} \]

where a is the volume factor and is typically set to 0.7.

Examples

>>> import qufilab as ql
>>> import numpy as np
... 
>>> # Load a sample dataframe.
>>> df = ql.load_sample('MSFT')
>>> print(df['close'].dtype)
float64
>>> t3 = ql.t3(df['close'], periods = 10)
>>> print(t3)
[nan nan nan ... 210.08668472 210.2348457 210.47802463]

Triangular Moving Average

qufilab.tma(data, periods)
Parameters
datandarray

An array containing values.

periodsint

Number of periods to be used.

Returns
ndarray

An array containing calculated triangular moving average values.

Notes

The triangular moving average is calculated by taking an sma of an sma.

\[tma = sma(sma(price, n_1), n_2)\]

As seen above, two different periods are used in this implementation. If the parameter periods is even

\[ \begin{align}\begin{aligned}n_1 = \frac{periods}{2}\\n_2 = \frac{periods}{2} + 1\end{aligned}\end{align} \]

otherwise they are rounded up after the following calculation

\[n_1 = n_2 = \frac{periods + 1}{2}\]

Examples

>>> import qufilab as ql
>>> import numpy as np
... 
>>> # Load a sample dataframe.
>>> df = ql.load_sample('MSFT')
>>> print(df['close'].dtype)
float64
>>> tma = ql.tma(df['close'], periods = 10)
>>> print(tma)
[nan nan nan ... 208.93833333 209.115 209.684]

Weighted Close

qufilab.wc(high, low, close)
Parameters
highndarray

An array containing high prices.

lowndarray

An array containing low prices.

closendarray

An array containing closing prices.

Returns
ndarray

An array containing calculated weighted close values.

Notes

The weighted close is defined as:

\[wc_K = \frac{2 \cdot close_K + high_K + low_K}{4}\]

Examples

>>> import qufilab as ql
>>> import numpy as np
... 
>>> # Load a sample dataframe.
>>> df = ql.load_sample('MSFT')
>>> wc = ql.wc(df['high'], df['low'], df['close'])
>>> print(wc)
[153.1975 154.09 154.3075 ... 210.1875 213.2675 213.785]

Momentum

Absolute Price Oscillator

qufilab.apo(price, period_slow=26, period_fast=12, ma='sma')
Parameters
pricendarray

Array of type float64 or float32 containing price values.

period_slowint, optional

Number of periods for the slow moving average. Default to 26.

period_fastint, optional

Number of fast periods for the fast moving average. Default to 12.

ma{‘sma’, ‘ema’}, optional

Type of moving average to be used. Default to ‘sma’

Returns
ndarray

Array of type float64 or float32 containing the calculated absolute price oscillator values.

Aroon Indicator

qufilab.aroon(high, low, period=20)
Parameters
highndarray

Array of type float64 or float32 containing high prices.

lowndarray

Array of type float64 or float32 containing low prices.

periodint, optional

Number of periods to be used. Defaults to 20.

Returns
ndarray

Array of type float64 or float32 containing the calculated aroon indicator values.

Balance of Power

qufilab.bop(high, low, open_, close)
Parameters
highndarray

Array of type float64 or float32 containing high prices.

lowndarray

Array of type float64 or float32 containing low prices.

open_ndarray

Array of type float64 or float32 containing open prices.

closendarray

Array of type float64 or float32 containing closing prices.

Returns
ndarray

Array of type float64 or float32 containing the calculated balance of power values.

Commodity Channel Index

qufilab.cci(close, high, low, period=20)
Parameters
closendarray

Array of type float64 or float32 containing closing prices.

highndarray

Array of type float64 or float32 containing high prices.

lowndarray

Array of type float64 or float32 containing low prices.

periodint, optional

Number of periods to be used. Defaults to 20.

Returns
ndarray

Array of type float64 or float32 containing the calculated commodity channel index values.

Chande Momentum Indicator

qufilab.cmo(close, period)
Parameters
closendarray

Array of type float64 or float32 containing closing prices.

periodint

Number of periods to be used.

Returns
ndarray

Array of type float64 or float32 containing the calculated chande momentum values.

MACD

qufilab.macd(price)
Parameters
pricendarray

Array of type float64 or float32 containing the prices to calculate macd from.

Returns
macdndarray

Array of type float64 or float32 containing the macd values.

signalndarray

Array of type float64 or float32 containing the signal values.

Money Flow Index

qufilab.mfi(high, low, close, volume, period)
Parameters
highndarray

Array of type float64 or float32 containing high prices.

lowndarray

Array of type float64 or float32 containing low prices.

closendarray

Array of type float64 or float32 containing closing prices.

volumendarray

Array of type float64 or float32 containing volume values.

periodint

Number of periods to be used.

Returns
ndarray

Array of type float64 or float32 containing the calculated money flow index values.

Momentum Indicator

qufilab.mi(price, period)
Parameters
pricendarray

Array of type float64 or float32 containing price values.

periodint

Number of periods to be used.

Returns
ndarray

Array of type float64 or float32 containing the calculated momentum indicator values.

Percentage Price Oscillator

qufilab.ppo(price, period_fast=12, period_slow=26, ma='ema')
Parameters
pricendarray

Array of type float64 or float32 containing price values.

period_fastint, optional

Number of fast periods for the fast moving average. Default to 12.

period_slowint, optional

Number of periods for the slow moving average. Default to 26.

ma{‘ema’, ‘sma’}, optional

Type of moving average to be used. Default to ‘ema’

Returns
ndarray

Array of type float64 or float32 containing the calculated percentage price values.

Relative Strength Index

qufilab.rsi(price, period, rsi_type='smoothed')
Parameters
pricendarray

Array of type float64 or float32 containing the data to calculate rsi from.

periodint

Number of periods to be used.

rsi_type{‘smoothed’, ‘standard’}, optional

Specify what kind of averaging should be used for calculating the average gain/ average loss. Standard is the Wilder’s smoothing.

Returns
ndarray

Returns a numpy ndarray with type float64 or float32.

Price Rate of Change

qufilab.roc(price, period)
Parameters
pricendarray

Array of type float64 or float32 containing price values.

periodint

Number of periods to be used.

Returns
ndarray

Array of type float64 or float32 containing the calculated rate of change values.

Volume Price Trend

qufilab.vpt(price, volume)
Parameters
pricendarray

Array of type float64 or float32 containing price values.

volumendarray

Array of type float64 or float32 containing volume values.

Returns
ndarray

Array of type float64 or float32 containing the calculated volume price trend values.

William’s R

qufilab.willr(close, high, low, period)
Parameters
closendarray

Array of type float64 or float32 containing the closing prices.

highndarray

Array of type float64 or float32 containing the high prices.

lowndarray

Array of type float64 or float32 containing the low prices.

periodint

Number of periods to be used.

Returns
ndarray

Array of type float64 or float32 containing the william’s r values.

Volatility

Average True Range

qufilab.atr(close, high, low, period)
Parameters
closendarray

Array of type float64 or float32 containing the closing prices.

highndarray

Array of type float64 or float32 containing the high prices.

lowndarray

Array of type float64 or float32 containing the low prices.

periodint

Number of periods to be used.

Returns
ndarray

Array of type float64 or float32 containing the calculated average true range values.

Bollinger Bands

qufilab.bbands(price, period, deviation=2)
Parameters
pricendarray

Array of type float64 or float32 containing the prices.

periodint

Number of periods to be used.

deviationint, optional

Number of standard deviations from the mean. Defaults to 20.

Returns
upperndarray

Upper bollinger band.

middlendarray

middle bollinger band.

lowerndarray

lower bollinger band.

Chaikin Volatility

qufilab.cv(high, low, period=10, smoothing_period=10)
Parameters
highndarray

Array of type float64 or float32 containing the high prices.

lowndarray

Array of type float64 or float32 containing the low prices.

periodint, optional

Number of periods to be used. Defaults to 10.

smooting_periodint, optional

Number of periods to be used for smoothing chaikin volatility values. Defaults to 10.

Returns
ndarray

Array of type float64 or float32 containing the calculated chaikin volatility values.

Keltner Channels

qufilab.kc(close, high, low, period=20, period_atr=20, deviation=2)
Parameters
closendarray

Array of type float64 or float32 containing the closing prices.

highndarray

Array of type float64 or float32 containing the high prices.

lowndarray

Array of type float64 or float32 containing the low prices.

periodint, optional

Number of periods to be used. Defaults to 20.

period_atrint, optional

Number of periods to be used for the average true range calculations. Defaults to 20.

deviationint, optional

Number of deviations from the mean. Defaults to 2.

Returns
upperndarray

Upper keltner band.

middlendarray

middle keltner band.

lowerndarray

lower keltner band.

Volume

Accumulation Distribution

qufilab.acdi(close, high, low, volume)
Parameters
closendarray

Array of type float64 or float32 containing closing prices.

highndarray

Array of type float64 or float32 containing high prices.

lowndarray

Array of type float64 or float32 containing low prices.

volumendarray

Array of type float64 or float32 containing volume values.

Returns
ndarray

Array of type float64 or float32 containing the calculated accumulation distribution values.

Chaikin Indicator

qufilab.ci(close, high, low, volume)
Parameters
closendarray

Array of type float64 or float32 containing closing prices.

highndarray

Array of type float64 or float32 containing high prices.

lowndarray

Array of type float64 or float32 containing low prices.

volumendarray

Array of type float64 or float32 containing volume values.

Returns
ndarray

Array of type float64 or float32 containing the calculated chaikin indicator values.

Chaikin Money Flow

qufilab.cmf(close, high, low, volume, period=21)
Parameters
closendarray

Array of type float64 or float32 containing closing prices.

highndarray

Array of type float64 or float32 containing high prices.

lowndarray

Array of type float64 or float32 containing low prices.

volumendarray

Array of type float64 or float32 containing volume values.

periodint, optional

Number of periods to use. Defaults to 21.

Returns
ndarray

Array of type float64 or float32 containing the calculated chaikin money flow values.

Negative Volume Index

qufilab.nvi(price, volume)
Parameters
pricendarray

Array of type float64 or float32 containing prices.

volumendarray

Array of type float64 or float32 containing volume values.

Returns
ndarray

Array of type float64 or float32 containing the calculated negative volume index values.

On Balance Volume

qufilab.obv(price, volume)
Parameters
pricendarray

Array of type float64 or float32 containing prices.

volumendarray

Array of type float64 or float32 containing volume values.

Returns
ndarray

Array of type float64 or float32 containing the calculated on balance volume values.

Positive Volume Index

qufilab.pvi(price, volume)
Parameters
pricendarray

Array of type float64 or float32 containing prices.

volumendarray

Array of type float64 or float32 containing volume values.

Returns
ndarray

Array of type float64 or float32 containing the calculated positive volume index values.

Statistics

Beta

qufilab.beta(data, market, periods, normalize=False)
Parameters
datandarray

An array containing values.

marketndarray

An array containing market values to be used as the comparison when calculating beta.

periodsint

Number of periods to be used.

normalizebool, optional

Specify whether to normalize the standard deviation calculation within the beta calculation with n - 1 instead of n. Defaults to False.

Returns
ndarray

An array containing beta values.

Examples

>>> import qufilab as ql
>>> import numpy as np
...
>>> # Load sample dataframe.
>>> df = ql.load_sample('MSFT')
>>> df_market = ql.load_sample('DJI')
>>> beta = ql.beta(df['close'], df_market['close'], periods = 10)
>>> print(beta)
[nan nan nan ... 0.67027616 0.45641977 0.3169785]

Covariance

qufilab.cov(data, market, periods, normalize=True)
Parameters
datandarray

An array containing values.

marketndarray

An array containing market values to be used as the comparison when calculating beta.

periodsint

Number of periods to be used.

normalizebool, optional

Specify whether to normalize covariance with n - 1 instead of n. Defaults to True.

Returns
ndarray

An array containing covariance values.

Examples

>>> import qufilab as ql
>>> import numpy as np
...
>>> # Load sample dataframe.
>>> df = ql.load_sample('MSFT')
>>> df_market = ql.load_sample('DJI')
>>> cov = ql.cov(df['close'], df_market['close'], periods = 10)
>>> print(cov)
[nan nan nan ... -360.37842558  -99.1077715 60.84627274]

Percentage Change

qufilab.pct_change(data, periods)
Parameters
datandarray

An array containing values.

periodsint

Number of periods to be used.

Returns
ndarray

An array containing percentage change for the specified periods.

Examples

>>> import qufilab as ql
>>> import numpy as np
...
>>> # Load sample dataframe.
>>> df = ql.load_sample('MSFT')
>>> pct_change = ql.pct_change(df['close'], periods = 10)
>>> print(pct_change)
[nan nan nan ... -1.52155537 -0.81811879 0.25414157]

Standard Deviation

qufilab.std(data, periods, normalize=True)
Parameters
datandarray

An array containing values.

periodsint

Number of periods to be used.

normalizebool, optional

Specify whether to normalize the standard deviation with n - 1 instead of n. Defaults to True.

Returns
ndarray

An array containing standard deviation values for the specified periods.

Examples

>>> import qufilab as ql
>>> import numpy as np
...
>>> # Load a sample dataframe.
>>> df = ql.load_sample('MSFT')
>>> print(df['close'].dtype)
float64
>>> sma = ql.std(df['close'], periods = 10)
>>> print(sma)
[nan nan nan ... 3.31897842 2.9632574  3.02394683]

Variance

qufilab.var(data, periods, normalize=True)
Parameters
datandarray

An array containing values.

periodsint

Number of periods to be used.

normalizebool, optional

Specify whether to normalize the standard deviation with n - 1 instead of n. Defaults to True.

Returns
ndarray

An array containing variance values.

Examples

>>> import qufilab as ql
>>> import numpy as np
...
>>> # Load a sample dataframe.
>>> df = ql.load_sample('MSFT')
>>> print(df['close'].dtype)
float64
>>> var = ql.var(df['close'], periods = 10)
>>> print(var)
[nan nan nan ... 11.01561778  8.78089444 9.14425444]