We can create the code for this however the values computed in the custom watchlist column do not exactly match those computed for the chart. So be sure to spend a lot of time testing this out before using it. Screenshot below shows the result.
Here is the code:
input price = close;
input fastLength = 2;
input slowLength = 30;
input effRatioLength = 10;
input mode = {default KAMA, AMA};
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;
def crossAbove = close[1] < MovAvgAdaptive[1] and close > MovAvgAdaptive;
def crossBelow = close[1] > MovAvgAdaptive[1] and close < MovAvgAdaptive;
MovAvgAdaptive.AssignValueColor(if crossAbove or crossBelow then Color.BLACK else color.CURRENT);
AssignBackgroundColor(if crossAbove then Color.GREEN else if crossBelow then Color.RED else Color.CURRENT);
Ok, so in reality what you are asking for is to set the color based on the position of price and AMA. You do not want the colors based on crossovers, which are signal bar events. You actually want the color based on price being above or below the AMA.
In order to modify the code you will delete these two lines:
def crossAbove = close[1] < MovAvgAdaptive[1] and close > MovAvgAdaptive;
def crossBelow = close[1] > MovAvgAdaptive[1] and close < MovAvgAdaptive;
Then you will change the last two lines of code as to this:
MovAvgAdaptive.AssignValueColor(Color.BLACK);
AssignBackgroundColor(if close > MovAvgAdaptive then Color.GREEN else Color.RED);