# 3 + Super Trend
# 06/2022
#3 Super Trend - Mod by SAM4COK
declare lower;
input ATR1 = 1.0; # Between 1 - 100
input Period1 = 10; #Between 1 - 100
input ATR2 = 2.0; # Between 1 - 100
input Period2 = 11; #Between 1 - 100
input ATR3 = 3.0; # Between 1 - 100
input Period3 = 12; #Between 1 - 100
input changeATR = no;
input ShowLabel = yes;
input AvgType = AverageType.SIMPLE;
Assert(ATR1 > 0 and ATR2 > 0 and ATR3 > 0, "'atr factor' must be positive");
def Agg = GetAggregationPeriod();
def closePrice = close(period = Agg);
def highPrice = high(period = Agg);
def lowPrice = low(period = Agg);
def PriceST = hl2(period = Agg);
def Na = Double.NaN;
def iATR1 = if changeATR then ATR(Period1) else MovingAverage(AvgType, Max(Max(highPrice - lowPrice, AbsValue(highPrice - closePrice[1])), AbsValue(lowPrice - closePrice[1])), Period1);
def iATR2 = if changeATR then ATR(Period2) else MovingAverage(AvgType, Max(Max(highPrice - lowPrice, AbsValue(highPrice - closePrice[1])), AbsValue(lowPrice - closePrice[1])), Period2);
def iATR3 = if changeATR then ATR(Period3) else MovingAverage(AvgType, Max(Max(highPrice - lowPrice, AbsValue(highPrice - closePrice[1])), AbsValue(lowPrice - closePrice[1])), Period3);
def UP_Band_Basic1 = PriceST + (ATR1 * iATR1);
def LW_Band_Basic1 = PriceST - (ATR1 * iATR1);
def UP_Band_Basic2 = PriceST + (ATR2 * iATR2);
def LW_Band_Basic2 = PriceST - (ATR2 * iATR2);
def UP_Band_Basic3 = PriceST + (ATR3 * iATR3);
def LW_Band_Basic3 = PriceST - (ATR3 * iATR3);
def UP_Band1 = if ((UP_Band_Basic1 < UP_Band1[1]) or (closePrice[1] > UP_Band1[1])) then UP_Band_Basic1 else UP_Band1[1];
def LW_Band1 = if ((LW_Band_Basic1 > LW_Band1[1]) or (closePrice[1] < LW_Band1[1])) then LW_Band_Basic1 else LW_Band1[1];
def UP_Band2 = if ((UP_Band_Basic2 < UP_Band2[1]) or (closePrice[1] > UP_Band2[1])) then UP_Band_Basic2 else UP_Band2[1];
def LW_Band2 = if ((LW_Band_Basic2 > LW_Band2[1]) or (closePrice[1] < LW_Band2[1])) then LW_Band_Basic2 else LW_Band2[1];
def UP_Band3 = if ((UP_Band_Basic3 < UP_Band3[1]) or (closePrice[1] > UP_Band3[1])) then UP_Band_Basic3 else UP_Band3[1];
def LW_Band3 = if ((LW_Band_Basic3 > LW_Band3[1]) or (closePrice[1] < LW_Band3[1])) then LW_Band_Basic3 else LW_Band3[1];
def ST1 = if ((ST1[1] == UP_Band1[1]) and (closePrice < UP_Band1)) then UP_Band1 else
if ((ST1[1] == UP_Band1[1]) and (closePrice > UP_Band1)) then LW_Band1 else
if ((ST1[1] == LW_Band1[1]) and (closePrice > LW_Band1)) then LW_Band1 else
if ((ST1[1] == LW_Band1) and (closePrice < LW_Band1)) then UP_Band1 else LW_Band1;
def ST2 = if ((ST2[1] == UP_Band2[1]) and (closePrice < UP_Band2)) then UP_Band2 else
if ((ST2[1] == UP_Band2[1]) and (closePrice > UP_Band2)) then LW_Band2 else
if ((ST2[1] == LW_Band2[1]) and (closePrice > LW_Band2)) then LW_Band2 else
if ((ST2[1] == LW_Band2) and (closePrice < LW_Band2)) then UP_Band2 else LW_Band2;
def ST3 = if ((ST3[1] == UP_Band3[1]) and (closePrice < UP_Band3)) then UP_Band3 else
if ((ST3[1] == UP_Band3[1]) and (closePrice > UP_Band3)) then LW_Band3 else
if ((ST3[1] == LW_Band3[1]) and (closePrice > LW_Band3)) then LW_Band3 else
if ((ST3[1] == LW_Band3) and (closePrice < LW_Band3)) then UP_Band3 else LW_Band3;
Plot Long1 = if closePrice > ST1 then 1 else 0;
long1.SetPaintingStrategy(PaintingStrategy.POINTS);
Long1.AssignValueColor(if Long1 == 0 then color.black else CreateColor(100, 181, 246));
long1.SetLineWeight(1);
Plot Short1 = if closePrice < ST1 then 1 else 0;
Short1.SetPaintingStrategy(PaintingStrategy.POINTS);
short1.AssignValueColor(if short1 == 0 then color.black else CreateColor(239, 83, 80));
Short1.SetLineWeight(1);
Plot Long2 = if closePrice > ST2 then 2 else 0;
long2.SetPaintingStrategy(PaintingStrategy.POINTS);
Long2.AssignValueColor(if Long2 == 0 then color.black else CreateColor(100, 181, 246));
long2.SetLineWeight(1);
Plot Short2 = if closePrice < ST2 then 2 else 0;
Short2.SetPaintingStrategy(PaintingStrategy.POINTS);
short2.AssignValueColor(if short2 == 0 then color.black else CreateColor(239, 83, 80));
Short2.SetLineWeight(1);
Plot Long3 = if closePrice > ST3 then 3 else 0;
long3.SetPaintingStrategy(PaintingStrategy.POINTS);
Long3.AssignValueColor(if Long3 == 0 then color.black else CreateColor(100, 181, 246));
long3.SetLineWeight(1);
Plot Short3 = if closePrice < ST3 then 3 else 0;
Short3.SetPaintingStrategy(PaintingStrategy.POINTS);
short3.AssignValueColor(if short3 == 0 then color.black else CreateColor(239, 83, 80));
Short3.SetLineWeight(1);
Plot trendUP = if (Long1 + long2 + long3) > 1 then 4 else Na;
Plot trendDN = if (Short1 + Short2 + Short3) > 1 then 4 else Na;
trendUP.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
trendUP.AssignValueColor(if long1 > 0 and long2 > 0 and long3 > 0 then color.GREEN else color.orange);
trendUP.SetLineWeight(1);
trendDN.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
trendDN.AssignValueColor(if short1 > 0 and short2 > 0 and short3 > 0 then color.RED else color.orange);
trendDN.SetLineWeight(1);
addlabel (ShowLabel, "Trend", if long1 > 0 and long2 > 0 and long3 > 0 then color.GREEN else if short1 > 0 and short2 > 0 and short3 > 0 then color.RED else color.orange);