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).
示例
- 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[源代码]¶
基类:
IndicatorCalculates the standard deviation of the passed data for a given period
备注
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 ofmeansq - sqmeancaused 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()[源代码]¶
Calculate standard deviation for the current bar.
Uses the formula: sqrt(E[x^2] - E[x]^2)
- frompackages = ()¶
- packages = ()¶
- class backtrader.indicators.deviation.MeanDeviation[源代码]¶
基类:
IndicatorMeanDeviation (alias MeanDev)
Calculates the Mean Deviation of the passed data for a given period
备注
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¶
- backtrader.indicators.deviation.MeanDev¶
MeanDeviation的别名