backtrader.indicators.kama module

KAMA Indicator Module - Kaufman’s Adaptive Moving Average.

This module provides the KAMA (Kaufman’s Adaptive Moving Average) indicator developed by Perry Kaufman to adapt to market volatility and direction.

Classes:

AdaptiveMovingAverage: KAMA indicator (aliases: KAMA, MovingAverageAdaptive).

Example

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

self.kama = bt.indicators.KAMA(self.data.close, period=30, fast=2, slow=30)

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

self.buy()

class backtrader.indicators.kama.AdaptiveMovingAverage[source]

Bases: MovingAverageBase

Defined by Perry Kaufman in his book “Smarter Trading”.

It is A Moving Average with a continuously scaled smoothing factor by taking into account market direction and volatility. The smoothing factor is calculated from 2 ExponetialMovingAverage smoothing factors, a fast one and slow one.

If the market trends, the value will tend to the fast ema smoothing period. If the market doesn’t trend, it will move towards the slow EMA smoothing period.

It is a subclass of SmoothingMovingAverage, overriding once to account for the live nature of the smoothing factor

Formula:
  • direction = close - close_period

  • volatility = sumN(abs (close - close_n), period)

  • effiency_ratio = abs (direction / volatility)

  • fast = 2 / (fast_period + 1)

  • slow = 2 / (slow_period + 1)

  • Smfactor = squared(efficienty_ratio * (fast - slow) + slow)

  • smfactor1 = 1.0 - smfactor

  • The initial seed value is a SimpleMovingAverage

alias = ('KAMA', 'MovingAverageAdaptive')
__init__(*args, **kwargs)
nextstart()[source]

Seed KAMA calculation with SMA on first valid bar.

Calculates simple moving average for the initial seed value.

next()[source]

Calculate KAMA for the current bar.

KAMA = prev_KAMA + sc * (price - prev_KAMA) where sc is the adaptive smoothing constant.

once(start, end)[source]

Calculate KAMA in runonce mode.

Seeds with SMA and applies adaptive smoothing for each bar.

frompackages = ()
packages = ()