```
declare lower;
def AggregationPeriod = GetAggregationPeriod();
#hint: thinkScript adaptation of SuperTrend. The speed of the trend changes are controlled by adjusting the ATR input parameters.
#hint atrMult: Multiple to scale the Average True Range (ATR) value. (Smaller numbers will produce faster trend changes.)
input atrMult = 2.5;
#hint atrLength: ATR moving average period length.
input atrLength = 14;
#hint atrType: ATR moving average type
input atrType = AverageType.WILDERS;
#hint pivot/price: Bar pivot used for trend points. (Default is hl2)
input pivot = hl2;
def atr = MovingAverage(atrType, TrueRange(high, close, low), atrLength) * atrMult;
def prUp = pivot + atr;
def prDn = pivot - atr;
# rTrend stores the current trend value and direction. (The direction is currently up if rTrend[1] is positive and down if rTrend[1] is negative, therefore AbsValue is used to compare rTrend values when the trend is down.)
def rTrend = compoundValue(1,
# Trending Up
if rTrend[1] > 0 then
# If close is below trend then switch to downtrend (negative)
if close < Max(prDn[1], rTrend[1]) then -prUp
# ...else adjust new uptrend value.
else Max(prDn, rTrend[1])
# Trending Down
else if rTrend[1] < 0 then
# If close is above trend then switch to uptrend (positive)
if close > Min(prUp[1], AbsValue(rTrend[1])) then prDn
# ...else adjust new downtrend value.
else -Min(prUp, AbsValue(rTrend[1]))
else rTrend[1]
, pivot);
# The Trend plot skips a bar whenever the trend direction changes.
def Trend = if Sign(rTrend) == Sign(rTrend[1]) then absValue(rTrend) else Double.NaN;
###############################
##### Orders Conditions #####
###############################
def Bull = rTrend > 0 and rTrend[1] < 0;
def Bear = rTrend < 0 and rTrend[1] > 0;
plot BUY = Bull;
plot SELL = Bear;
```