backtrader.indicators.deviation module

Deviation Indicator Module - Standard deviation and mean deviation.

This module provides deviation indicators for measuring data dispersion.

Classes:

StandardDeviation: Standard deviation indicator (alias: StdDev). MeanDeviation: Mean absolute deviation (alias: MeanDev).

Example

class MyStrategy(bt.Strategy):
def __init__(self):

self.sma = bt.indicators.SMA(self.data.close, period=20) self.stddev = bt.indicators.StdDev(self.data.close, period=20) self.upper_band = self.sma + 2 * self.stddev self.lower_band = self.sma - 2 * self.stddev

def next(self):
if self.data.close[0] > self.upper_band[0]:

self.sell()

elif self.data.close[0] < self.lower_band[0]:

self.buy()

class backtrader.indicators.deviation.StandardDeviation[source]

Bases: Indicator

Calculates the standard deviation of the passed data for a given period

Note

  • If 2 datas are provided as parameters, the second is considered to be the mean of the first

  • safepow (default: False) If this parameter is True, the standard deviation will be calculated as pow (abs(meansq - sqmean), 0.5) to safeguard for possible negative results of meansq - sqmean caused by the floating point representation.

Formula:
  • meansquared = SimpleMovingAverage(pow (data, 2), period)

  • squaredmean = pow(SimpleMovingAverage(data, period), 2)

  • stddev = pow(meansquared - squaredmean, 0.5) # square root

See:
alias = ('StdDev',)
__init__(*args, **kwargs)
next()[source]

Calculate standard deviation for the current bar.

Uses the formula: sqrt(E[x^2] - E[x]^2)

once(start, end)[source]

Calculate standard deviation in runonce mode.

frompackages = ()
packages = ()
class backtrader.indicators.deviation.MeanDeviation[source]

Bases: Indicator

MeanDeviation (alias MeanDev)

Calculates the Mean Deviation of the passed data for a given period

Note

  • If 2 datas are provided as parameters, the second is considered to be the mean of the first

Formula:
  • mean = MovingAverage(data, period) (or provided mean)

  • absdeviation = abs (data - mean)

  • meandev = MovingAverage(absdeviation, period)

See:
alias = ('MeanDev',)
__init__(*args, **kwargs)
frompackages = ()
packages = ()
backtrader.indicators.deviation.StdDev

alias of StandardDeviation

backtrader.indicators.deviation.MeanDev

alias of MeanDeviation