Author states:
Aroon Trend Trail is a trend overlay that uses the relative recency of recent highs and lows to determine direction, then plots an ATR-based trailing rail whose width changes with the strength of the Aroon bias.
The indicator has two separate parts:
- Aroon state: determines whether the trend is bullish or bearish.
- Trend rail: provides a ratcheting lower or upper boundary based on ATR.
original Tradingview script: https://www.tradingview.com/script/30BBpKLx-Aroon-Trend-Trail-BackQuant/
Code:
# Aroon Trend Trail [BackQuant]
# Converted to ThinkScript = W. Kalvin
# Aroon Trend Trail is a trend overlay that uses the relative recency of recent highs and lows to determine direction, then plots an ATR-based trailing rail whose width changes with the strength of the Aroon bias.
# The indicator has two separate parts:
# • Aroon state: determines whether the trend is bullish or bearish.
# • Trend rail: provides a ratcheting lower or upper boundary based on ATR.
# The rail itself does not determine trend changes. Bullish and bearish flips come from the Aroon bias crossing the selected Bias Threshold.
# Inputs
input aroonLen = 25;
input flipThreshold = 0.15;
input atrLen = 14;
input atrFactor = 1.8;
input strongWidth = 0.65;
input weakWidth = 1.25;
input breakOnFlip = yes;
input lineW = 3;
input showFill = yes;
input showGlow = yes;
input showAroon = no;
input paintCandles = yes;
# Aroon Calculations using explicit type casting
def sinceHigh = (GetMaxValueOffset(high, aroonLen));
def sinceLow = (GetMinValueOffset(low, aroonLen));
def aroonUp = 100.0 * (aroonLen - AbsValue(sinceHigh)) / aroonLen;
def aroonDown = 100.0 * (aroonLen - AbsValue(sinceLow)) / aroonLen;
def aroonBias = (aroonUp - aroonDown) / 100.0;
def aroonStrength = Min(AbsValue(aroonBias), 1.0);
# Trend State Logic
rec trend = if IsNaN(trend[1]) then (if aroonBias >= 0.0 then 1 else -1)
else if trend[1] == 0 then (if aroonBias >= 0.0 then 1 else -1)
else if trend[1] != 1 and aroonBias > flipThreshold then 1
else if trend[1] != -1 and aroonBias < -flipThreshold then -1
else trend[1];
def bullFlip = (trend == 1) and (trend[1] == -1);
def bearFlip = (trend == -1) and (trend[1] == 1);
def trendFlip = bullFlip or bearFlip;
def isBull = trend == 1;
# Adaptive Rail Width
def atr = WildersAverage(TrueRange(high, close, low), atrLen);
def widthScale = weakWidth + (strongWidth - weakWidth) * aroonStrength;
def railWidth = atr * atrFactor * widthScale;
def rawLower = hl2 - railWidth;
def rawUpper = hl2 + railWidth;
# Ratcheting Rail
rec lowerRail = if IsNaN(lowerRail[1]) then rawLower
else if trend == 1 then (if bullFlip then rawLower else Max(lowerRail[1], rawLower))
else rawLower;
rec upperRail = if IsNaN(upperRail[1]) then rawUpper
else if trend == -1 then (if bearFlip then rawUpper else Min(upperRail[1], rawUpper))
else rawUpper;
def activeRail = if isBull then lowerRail else upperRail;
def plottedRail = if breakOnFlip and trendFlip then Double.NaN else activeRail;
# Plots
plot TrendRail = plottedRail;
TrendRail.SetLineWeight(lineW);
TrendRail.AssignValueColor(if isBull then Color.GREEN else Color.RED);
# Optional Fill Clouds
plot UpperRef = if showFill then activeRail + (railWidth * 0.5) else Double.NaN;
plot LowerRef = if showFill then activeRail - (railWidth * 0.5) else Double.NaN;
plot BullUpper = if showFill and isBull then UpperRef else Double.NaN;
plot BullLower = if showFill and isBull then LowerRef else Double.NaN;
AddCloud(BullUpper, BullLower, Color.DARK_GREEN, Color.DARK_GREEN);
plot BearUpper = if showFill and !isBull then UpperRef else Double.NaN;
plot BearLower = if showFill and !isBull then LowerRef else Double.NaN;
AddCloud(BearUpper, BearLower, Color.DARK_RED, Color.DARK_RED);
# Candle Coloring
AssignPriceColor(if paintCandles then (if isBull then Color.GREEN else Color.RED) else Color.CURRENT);
# Alerts
Alert(bullFlip, "Aroon Trend Rail turned bullish", Alert.BAR, Sound.Chimes);
Alert(bearFlip, "Aroon Trend Rail turned bearish", Alert.BAR, Sound.Bell);
Last edited by a moderator: