 # Kaufman's Adaptive Moving Average (KAMA)

#### BillMurray

##### Member
A buddy of mine wrote this KAMA indicator for ThinkorSwim and a few of us in a circle of friends use it. Austin (who write it) has been trying to massage the code so that it will throw alerts when the trend changes, to be able to scan for trend changes on multiple time frames, and to perhaps put a watchlist column in place to be able to easily pick off tickers changing polarity.

Code:
``````declare lower;
## KAMA (Kaufman Adaptive Moving Average) Settings
input price = close;
input KAMAfastLength = 2;
input KAMAslowLength = 30;
input effRatioLength = 10;
input mode = {default KAMA, AMA};

Assert(KAMAfastLength > 0, "'fast length' must be positive: " + KAMAfastLength);
Assert(KAMAslowLength > 0, "'slow length' must be positive: " + KAMAslowLength);

def direction;
def volatility;
def ER;

switch (mode) {
case KAMA:
direction = AbsValue(price - price[effRatioLength]);
volatility = Sum(AbsValue(price - price), effRatioLength);
ER = if volatility != 0 then direction / volatility else 0;
case AMA:
direction = Double.NaN;
volatility = Double.NaN;
ER = AbsValue((price - Lowest(low, effRatioLength)) -
(Highest(high, effRatioLength) - price)) / (Highest(high,
effRatioLength) - Lowest(low, effRatioLength));
}

def FastSF = 2 / (KAMAfastLength + 1);
def SlowSF = 2 / (KAMAslowLength + 1);
def ScaledSF = ER * (FastSF - SlowSF) + SlowSF;
def AMA = CompoundValue(1, AMA + Sqr(ScaledSF) * (price - AMA),
price);

## EMA Settings
input length = 34;
input displace = 0;

def AvgExp = ExpAverage(price[-displace], length);

##MACD

input fastLength = 12;
input slowLength = 26;
input macdLength = 9;
input averageType = AverageType.EXPONENTIAL;

def diff = reference MACD(fastLength, slowLength, macdLength, averageType).Diff;

## Volume
def Vol = volume(period = AggregationPeriod.DAY);

def avg30dayVol = Average(volume, 30);

def today = volume;
def avg30periodVol = Average(volume, 30);

def BuyVolumeSignal = (today / avg30periodVol) * 100;
def SellVolumeSignal = (today / avg30periodVol) * 100;

def condition1 = price > MovAvgAdaptive and price > AvgExp and BuyVolumeSignal > 15 is true;

def condition2 = price < MovAvgAdaptive and price < AvgExp and BuyVolumeSignal > 15;
;

plot trend = if condition1 is true then 1 else if condition2 >0 then -1 else 0;
trend.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
trend.DefineColor("Up", Color.UPTICK);
trend.DefineColor("Down", Color.DOWNTICK);
trend.AssignValueColor(if trend > 0 then trend.Color("Up") else if trend < 0 then trend.Color("Down") else GetColor(1));
#Zero.SetDefaultColor(GetColor(5));

Any suggestions would be greatly appreciated, and of course you get to use this. We use it to trade /ES and /NQ futures quite successfully.

• Jonas99, scott69 and samoya

#### barbaros

##### Well-known member
VIP
Can you explain which timeframe this is most effective? Is it intended to buy and sell on every signal?

VIP

#### Lauri

##### New member
VIP
Is there a code for the Kaufman Efficiency Ratio Indicator? I read on Google there was, but I can't seem to find it.

Edit: found it, shared by @tomsk. Developed by Mobius.

Code:
``````# Kaufman Efficiency Ratio
# Mobius
# 1.29.2017

# Here's the Kaufman Efficiency Ratio that the Kaufman AMA is based on.
# Notes: Price at extremes are entry / exit signals. Price in zone between extremes and equity should be avoided.

declare lower;

input n = 10;

def netchg = close - close[n];
def sumchg = sum(AbsValue(close - close), n);
plot KER = (netchg / sumchg);
KER.SetStyle(Curve.Firm);
KER.SetDefaultColor(Color.Cyan);
plot zero = if isNaN(close) then double.nan else 0;
zero.SetDefaultColor(Color.gray);
plot OB = HighestAll(KER) * .8;
OB.SetDefaultColor(Color.red);
plot OS = LowestAll(KER) * .8;
OS.SetDefaultColor(Color.green);
# End Code Kaufman Efficiency Ratio``````

• Jonas99

#### WayneG

##### New member
VIP
I've found numerous references to this indicator, including one about translating from the TOS version to TradeStation. I know that we have the lower half of the indicator, but I'm looking for the full version that plots on the main chart too.]

Thanks for the help, Wayneg

Last edited: