```
input price = close;
#AMA 2,30,10
input fastLength = 2;
input slowLength = 30;
input effRatioLength = 10;
input mode = {default AMA, KAMA};
Assert(fastLength > 0, "'fast length' must be positive: " + fastLength);
Assert(slowLength > 0, "'slow length' must be positive: " + slowLength);
def direction;
def volatility;
def ER;
switch (mode) {
case KAMA:
direction = AbsValue(price - price[effRatioLength]);
volatility = Sum(AbsValue(price - price[1]), 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 / (fastLength + 1);
def SlowSF = 2 / (slowLength + 1);
def ScaledSF = ER * (FastSF - SlowSF) + SlowSF;
def AMA = CompoundValue(1, AMA[1] + Sqr(ScaledSF) * (price - AMA[1]),
price);
plot MovAvgAdaptive = AMA;
plot MovAvgAdaptiveline = AMA;
MovAvgAdaptive.SetDefaultColor(CreateColor(153, 0, 153));
MovAvgAdaptive.SetLineWeight(4);
MovAvgAdaptive.SetStyle(Curve.SHORT_DASH);
MovAvgAdaptiveline.SetStyle(Curve.FIRM);
MovAvgAdaptiveline.SetDefaultColor(CreateColor(153, 0, 153));
MovAvgAdaptiveline.SetLineWeight(2);
#KAMA 2,30,10
input fastLength2 = 2;
input slowLength2 = 30;
input effRatioLength2 = 10;
input mode2 = {default KAMA, AMA};
Assert(fastLength2 > 0, "'fast length2' must be positive: " + fastLength2);
Assert(slowLength2 > 0, "'slow length2' must be positive: " + slowLength2);
def direction2;
def volatility2;
def ER2;
switch (mode2) {
case KAMA:
direction2 = AbsValue(price - price[effRatioLength2]);
volatility2 = Sum(AbsValue(price - price[1]), effRatioLength2);
ER2 = if volatility2 != 0 then direction2 / volatility2 else 0;
case AMA:
direction2 = Double.NaN;
volatility2 = Double.NaN;
ER2 = AbsValue((price - Lowest(low, effRatioLength2)) -
(Highest(high, effRatioLength2) - price)) / (Highest(high,
effRatioLength2) - Lowest(low, effRatioLength2));
}
def FastSF2 = 2 / (fastLength2 + 1);
def SlowSF2 = 2 / (slowLength2 + 1);
def ScaledSF2 = ER2 * (FastSF2 - SlowSF2) + SlowSF2;
def AMA2 = CompoundValue(1, AMA2[1] + Sqr(ScaledSF2) * (price - AMA2[1]),
price);
plot MovAvgAdaptive2 = AMA2;
MovAvgAdaptive2.SetDefaultColor(GetColor(1));
MovAvgAdaptive2.SetLineWeight(2);
MovAvgAdaptive2.SetStyle(Curve.FIRM);
#KAMA 5,30,10
input fastLength3 = 5;
input slowLength3 = 30;
input effRatioLength3 = 10;
input mode3 = {default KAMA, AMA};
Assert(fastLength3 > 0, "'fast length3' must be positive: " + fastLength3);
Assert(slowLength3 > 0, "'slow length3' must be positive: " + slowLength3);
def direction3;
def volatility3;
def ER3;
switch (mode3) {
case KAMA:
direction3 = AbsValue(price - price[effRatioLength3]);
volatility3 = Sum(AbsValue(price - price[1]), effRatioLength3);
ER3 = if volatility3 != 0 then direction3 / volatility3 else 0;
case AMA:
direction3 = Double.NaN;
volatility3 = Double.NaN;
ER3 = AbsValue((price - Lowest(low, effRatioLength3)) -
(Highest(high, effRatioLength3) - price)) / (Highest(high,
effRatioLength3) - Lowest(low, effRatioLength3));
}
def FastSF3 = 2 / (fastLength3 + 1);
def SlowSF3 = 2 / (slowLength3 + 1);
def ScaledSF3 = ER2 * (FastSF3 - SlowSF3) + SlowSF3;
def AMA3 = CompoundValue(1, AMA3[1] + Sqr(ScaledSF3) * (price - AMA3[1]),
price);
plot MovAvgAdaptive3 = AMA3;
MovAvgAdaptive3.SetDefaultColor(GetColor(9));
MovAvgAdaptive3.SetLineWeight(3);
MovAvgAdaptive3.SetStyle(Curve.FIRM);
#Chart Bubbles
def Upsignal = close crosses above MovAvgAdaptive;
def Downsignal = close crosses below MovAvgAdaptive;
input crossingType = {default above, below};
#plot signal = crosses(close, MovAvgAdaptive, crossingType == CrossingType.above);
#signal.DefineColor("Above", GetColor(6));
#signal.DefineColor("Below", GetColor(5));
#signal.AssignValueColor(if crossingType == CrossingType.above then signal.color("Above") else signal.color("Below"));
#signal.SetPaintingStrategy(if crossingType == CrossingType.above
# then PaintingStrategy.BOOLEAN_ARROW_UP
#else PaintingStrategy.BOOLEAN_ARROW_DOWN);
#AddChartBubble(Upsignal, high, "CxA", Color.DARK_GREEN);
#AddChartBubble(Downsignal, low, "CxA", Color.DARK_RED);
#Vertical Lines
def long = MovAvgAdaptive crosses above MovAvgAdaptive2;
def short = MovAvgAdaptive crosses below MovAvgAdaptive2;
AddVerticalLine(short, close, Color.RED, Curve.SHORT_DASH);
AddVerticalLine(long, close, Color.GREEN, Curve.SHORT_DASH);
#Alert
def alerttrigger1 = close crosses MovAvgAdaptive;
input alerttext = "!!!!! CLOSE crosses AMA !!!!!";
input UseAlerts = {false, default true};
input AlertType = {default "BAR", "ONCE", "TICK"};
def at = AlertType;
input AlertSound = {"Chimes", "Ring", default "Bell", "NoSound", "Ding"};
Alert (alerttrigger1 and UseAlerts, alerttext, if at == 1 then Alert.ONCE else if at == 2 then Alert.TICK else Alert.BAR, AlertSound);
def D2 = close > MovAvgAdaptive;
def D1 = close < MovAvgAdaptive;
def OB = D1;
def OS = D2;
def TLB = !D1 and !D2;
AddLabel(TLB, " (Close <> AMA) ", Color.YELLOW);
AddLabel(OS, " (Close > AMA) ", Color.UPTICK);
AddLabel(OB, " (Close < AMA) ", Color.DOWNTICK);
```