Repaints SuperBollingerTrend, SushiTrend, SuperTrend All-in-One for ThinkOrSwim

Repaints

samer800

Moderator - Expert
VIP
Lifetime
P74fqgF.png

SuperBollingerTrend:
https://www.tradingview.com/script/AjWfiZpw-SuperBollingerTrend-Expo/

SushiTrend:
https://www.tradingview.com/v/dfVzbU6r/

CODE: Fix Sushi Tend Code

CSS:
#--- SuperTrend, Bollinger Trend, Sushi Trrend indicator
# Converted and modifiedTrueRange by Sam4Cok@Samer800    - 05/2023
# Sushi Trend Code Fix - 11/2023
input BarsColor       = no;
input SupertrendCloud = no;
input ShowTrendLine = yes;
input ShowZigZag = no;
input ShowMovAvgLine = no;
input ShowSignals = yes;
input UseMovAvgFilter = no;
input CalcMethod = {"ATR Trend", default "Bollinger Trend", "Sushi Trend"};
input supertrendSource = hl2;
input Period = 12;        # "Length"
input factor = 2.0;       # "Factor"
input FactorForSushiTrend = 5;#, title = 'Engulfing Factor')
input averageType = {default "ZLSMA","TRAMA","VAWMA","eVWMA","SMMA","EMA","TEMA","WMA","VWMA","SMA","HMA", "McGinley"};
input MovAvgLength = 100;
input MovAvgSrc    = close;
#input useChartTimeFrame = yes;
#input manualAggPeriod = AggregationPeriod.FIFTEEN_MIN;

def na = Double.NaN;
def src = supertrendSource;
def bodyMiddle = ohlc4;
def current = GetAggregationPeriod();
#def tf = if useChartTimeFrame then current else manualAggPeriod;
#def zlsmaSrc = if useChartTimeFrame then MovAvgSrc else close(Period=tf);
#---color
DefineGlobalColor("Blue" , CreateColor(33, 150, 243));
DefineGlobalColor("up", CreateColor(76, 175, 80));
DefineGlobalColor("dn", CreateColor(255, 82, 82));
def atrTrend = CalcMethod == CalcMethod."ATR Trend";
def SushTrend = CalcMethod == CalcMethod."Sushi Trend";

#vwma(source, length)
script VWMA {
    input x = close;
    input y = 15;
    def VWMA = Average(x * volume, y) / Average(volume, y);
    plot result = VWMA;
}
# vawma(src, len) =>
script vawma {
    input src = hlc3;
    input len = 50;
    def sum;
    def vol;
    def v;
    def  s   = fold i = 0 to len with p do
           src[i];
    def volm = if IsNaN(volm[1]) then 0 else
           fold j = 0 to len with u do
           volume[j];
    def ma   = if !IsNaN(s) and !IsNaN(volm) then
           fold k = 0 to len with w do
           len - k  else 0;
    v = fold l = 1 to len with q do
           if IsNaN(v[1]) then volm else
           volume * ma;
    vol = fold m = 0 to len with r do
           r + v[m];
    sum = fold n = 0 to len with t do
           t + src[n] * v[n];
    plot vawma = sum / vol;
}
#ma(mode, len,  src) =>
script ma {
    input type = "EMA";
    input src = close;
    input len = 100;
    def lsma  = Inertia(src, len);
    def lsma2 = Inertia(lsma, len);
    def eq    = lsma - lsma2;
    def zlsma = lsma + eq;
    def volumeSum = Sum(volume, len);
    def evwma = ((volumeSum - volume) * evwma[1] + volume * src) / volumeSum;
    def e = ExpAverage(src, len);
    def Mcg = if IsNaN(Mcg[1]) then Average(src, len) else
              CompoundValue(1, Mcg[1] + ((src - Mcg[1]) / (0.6 * len * Power(src / Mcg[1], 4))), src);
    def H = Highest(high, len);
    def L = Lowest(low, len);
    def HH = Max(Sign(H - H[1]), 0);
    def LL = Max(Sign(L - L[1]) * -1, 0);
    def TC = Power(Average(if HH or LL then 1 else 0, len), 2);
    def ama = CompoundValue(1, ama[1] + TC * (src - ama[1]), src);
    def ma;
    ma = if type == "SMA"   then Average(src, len) else
     if type == "ZLSMA" then zlsma else   
     if type == "VAWMA" then vawma(src, len) else
     if type == "EMA"   then ExpAverage(src, len) else
     if type == "TEMA"  then TEMA(src, len) else
     if type == "WMA"   then WMA(src, len) else
     if type == "VWMA"  then vwma(src, len) else
     if type == "SMMA"  then WildersSmoothing(src, len) else
     if type == "HMA"   then HullMovingAvg(src, len) else
     if type == "eVWMA" then evwma else
     if type == "TRAMA" then ama else
     if type == "McGinley" then Mcg else Average(src, len);
    plot result = ma;
}
#pine_supertrend(src, factor, atrPeriod) =>
script supertrend {
    input src = hl2;
    input factor = 3;
    input Period = 10;
    input atrTrend = yes;
    input MovAvgType = AverageType.SIMPLE;
    def lowerBand;
    def upperBand;
    def nATR = ATR(LENGTH = Period);
    def bbup = ma(MovAvgType, high, Period) + StDev(high, Period) * factor;
    def bbdn = ma(MovAvgType, low, Period)  - StDev(low, Period) * factor;
    def atrup = src + factor * nATR;
    def atrdn = src - factor * nATR;
    def up = if atrTrend then atrup else bbup;
    def dn = if atrTrend then atrdn else bbdn;
    def up1 = if (IsNaN(upperBand[1]) or upperBand[1] == 0) then up else If(upperBand[1] == 0, up, upperBand[1]);
    def dn1 = if (IsNaN(lowerBand[1]) or lowerBand[1] == 0) then dn else If(lowerBand[1] == 0, dn, lowerBand[1]);
    upperBand = if (up < up1) or (close[1] > up1) then up else up1;
    lowerBand = if (dn > dn1) or (close[1] < dn1) then dn else dn1;
    def trend;# = na
    def superTrend;# = na
    def prevSuperTrend = If(superTrend[1] == 0, up1, superTrend[1]);
    if IsNaN(nATR[1]) or IsNaN(StDev(src, Period)) {
        trend = 1;
    } else
    if prevSuperTrend == up1 {
        trend = if close > upperBand then -1 else 1;
    } else {
        trend = if close < lowerBand then  1 else -1;
    }
    superTrend = if trend == -1 then lowerBand else upperBand;
    plot ST = superTrend;
    plot dir = trend;
}
#-- Moving Avg
def MovAvg = ma(averageType, MovAvgSrc, MovAvgLength);
plot MovAvgLine = if ShowMovAvgLine then MovAvg else na;
MovAvgLine.SetDefaultColor(Color.CYAN);

#-- ST
def supertrend = if SushTrend then na else supertrend(src, factor, Period, atrTrend, averageType).ST;
def direction  = if SushTrend then na else supertrend(src, factor, Period, atrTrend, averageType).DIR;

plot upTrend = if ShowTrendLine and (direction < 0) then supertrend else na;#, "Up Trend"
upTrend.SetDefaultColor(GlobalColor("up"));
plot downTrend = if ShowTrendLine and (direction > 0) then supertrend else na;#, "Down Trend"
downTrend.SetDefaultColor(GlobalColor("dn"));

def LongTrigger  = direction != direction[1] and direction < 0;
def ShortTrigger = direction != direction[1] and direction > 0;

plot LongDot = if ShowTrendLine and LongTrigger then upTrend else na;
LongDot.SetPaintingStrategy(PaintingStrategy.POINTS);
LongDot.AssignValueColor(GlobalColor("up"));
LongDot.SetLineWeight(4);
plot ShortDot = if ShowTrendLine and ShortTrigger then downTrend else na;
ShortDot.SetPaintingStrategy(PaintingStrategy.POINTS);
ShortDot.AssignValueColor(GlobalColor("dn"));
ShortDot.SetLineWeight(4);

AddChartBubble(ShowSignals and LongTrigger and If(UseMovAvgFilter, src > MovAvg, 1),
              If(ShowTrendLine, supertrend, low), "Bull", Color.GREEN, no);
AddChartBubble(ShowSignals and ShortTrigger and If(UseMovAvgFilter, src < MovAvg, 1),
              If(ShowTrendLine, supertrend, high), "Bear",  Color.RED, yes);


AddCloud(bodyMiddle, upTrend, Color.DARK_GREEN);
AddCloud(downTrend, bodyMiddle, Color.DARK_RED);

#-- END CODE--
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © HoanGhetti
#indicator("Sushi Trend [HG]", overlay = true)
def last = IsNaN(close);

def hh = Highest(high, FactorForSushiTrend);
def ll = Lowest(low, FactorForSushiTrend);
def stMatrixMax = Highest(high, FactorForSushiTrend * 2);
def stMatrixMin = Lowest(low, FactorForSushiTrend * 2);
def max = hh == stMatrixMax;
def min = ll == stMatrixMin;
def valid = max and min and ((stMatrixMin == low[FactorForSushiTrend - 1])
                          or (stMatrixMax == high[FactorForSushiTrend - 1]));
def barssince = if valid[1] then 0 else barssince[1] + 1;
def sushi = SushTrend and valid and barssince >= FactorForSushiTrend and !last[1];#!IsNaN(close[1]);
def diff = (sushi-sushi[1]) != 0;
def lastMax = (if diff and sushi and max then stMatrixMax else lastMax[1]);
def lastMin = (if diff and sushi then stMatrixMin else lastMin[1]);
def SushicrossUp = Crosses(close, lastMax, CrossingDirection.ABOVE);
def SushicrossDn = Crosses(close, lastMin, CrossingDirection.BELOW);
def Sushidirection;
if SushicrossUp {
    Sushidirection = 1;
    } else
if SushicrossDn {
    Sushidirection = -1;
    } else {
    Sushidirection = Sushidirection[1];
}
def sushiTrend = if Sushidirection > 0 then lastMin else lastMax;
def sushiChange = sushiTrend and Sushidirection!=Sushidirection[1];
def sushiLongTrigger  = SushTrend and Sushidirection == 1 and Sushidirection[1] != 1;
def sushiShortTrigger = SushTrend and Sushidirection ==-1 and Sushidirection[1] !=-1;

plot stUp = if last or !ShowTrendLine then na else
            if Sushidirection > 0 then sushiTrend else na;    # 'Sushi Trend'
stUp.SetDefaultColor(GlobalColor("up"));

plot stDn = if last or !ShowTrendLine then na else
            if Sushidirection < 0 then sushiTrend else na;    # 'Sushi Trend'
stDn.SetDefaultColor(GlobalColor("dn"));

plot LongSushiDot = if ShowTrendLine and sushiLongTrigger then sushiTrend else na;
LongSushiDot.SetPaintingStrategy(PaintingStrategy.POINTS);
LongSushiDot.AssignValueColor(GlobalColor("up"));
LongSushiDot.SetLineWeight(3);
plot ShortSushiDot = if ShowTrendLine and sushiShortTrigger then sushiTrend else na;
ShortSushiDot.SetPaintingStrategy(PaintingStrategy.POINTS);
ShortSushiDot.AssignValueColor(GlobalColor("dn"));
ShortSushiDot.SetLineWeight(3);

def stPlot = if last or !ShowTrendLine then na else if !sushiChange then sushiTrend else na;

AddCloud(bodyMiddle, stPlot, Color.DARK_GREEN, Color.DARK_RED);

AddChartBubble(ShowSignals and sushiLongTrigger and If(UseMovAvgFilter, src > MovAvg, 1),
               If(ShowTrendLine, sushiTrend, low), "Bull", Color.GREEN, no);
AddChartBubble(ShowSignals and sushiShortTrigger and If(UseMovAvgFilter, src < MovAvg, 1),
               If(ShowTrendLine, sushiTrend, high), "Bear",  Color.RED, yes);

#--- ZigZag
def triggerUp = if SushTrend then sushiLongTrigger else LongTrigger;
def triggerDn = if SushTrend then sushiShortTrigger else ShortTrigger;
def zigzagDir = if SushTrend then If(Sushidirection == 1, -1, 1) else direction;
def ZigZagSrc = if SushTrend then sushiTrend else supertrend;

plot zigzag = if !ShowZigZag then na else
              if triggerUp then ZigZagSrc else if triggerDn then ZigZagSrc else
              if IsNaN(close[-1]) then
              if  zigzagDir > 0 then Highest(src, Period) else Lowest(src, Period) else na;
zigzag.AssignValueColor(if zigzagDir[1] < 0 then Color.WHITE else Color.GRAY);
zigzag.SetStyle(Curve.SHORT_DASH);
zigzag.EnableApproximation();

#--- END CODE
def lastUp = if triggerUp then ZigZagSrc else If(lastUp[1] == 0, ZigZagSrc, lastUp[1]);
def lastDn = if triggerDn then ZigZagSrc else If(lastDn[1] == 0, ZigZagSrc, lastDn[1]);

def centerUp;
def centerDn;

if (lastUp != lastUp[1]) {
    centerUp = (If(centerUp[1] == 0, lastUp, centerUp[1]) + lastUp) / 2;
} else {
    centerUp =  If(centerUp[1] == 0, lastUp, centerUp[1]);
}
if (lastDn != lastDn[1]) {
    centerDn = (If(centerDn[1] == 0, lastDn, centerDn[1]) + lastDn) / 2;
} else {
    centerDn =  If(centerDn[1] == 0, lastDn, centerDn[1]);
}
def midSupUp = ma(averageType, centerUp , Period);
def midSupDn = ma(averageType, centerDn , Period);
def midSup = (midSupUp + midSupDn) / 2;

plot MidLine = if IsNaN(close) or midSup == 0 then na else midSup;
MidLine.SetHiding(!SupertrendCloud);
MidLine.AssignValueColor(if midSup < src then GlobalColor("blue") else Color.PLUM);

def MidLineUp = midSupUp;
def MidLineDn = midSupDn;

def trendUp = SupertrendCloud and midSup < src and midSupUp < src and  midSupDn < src;
def trendDn = SupertrendCloud and midSup >= src and midSupUp >= src and  midSupDn >= src;

AddCloud(if trendUp then midSupDn else na, midSupUp, GlobalColor("blue"));
AddCloud(if trendDn then midSupDn else na, midSupUp, Color.PLUM);
AddCloud(if (!trendDn and !trendUp and SupertrendCloud) then midSupDn else na, midSupUp, Color.DARK_GRAY);

#--- Bar Color

def BarUpEx = midSup < src and midSupUp < src and  midSupDn < src;
def BarDnEx = midSup >= src and midSupUp >= src and  midSupDn >= src;
def BarUp = midSup < src and midSupDn < src;
def BarDn = midSup >= src and midSupUp >= src;

AssignPriceColor(if !BarsColor then Color.CURRENT else
                 if BarUpEx then Color.GREEN else
                 if BarUp   then Color.DARK_GREEN else
                 if BarDnEx then Color.RED else
                 if BarDn   then Color.DARK_RED else Color.GRAY);
    
#--- END of CODE
 
Last edited:
P74fqgF.png

SuperBollingerTrend:
https://www.tradingview.com/script/AjWfiZpw-SuperBollingerTrend-Expo/

SushiTrend:
https://www.tradingview.com/v/dfVzbU6r/

CODE:
CSS:
#--- SuperTrend, Bollinger Trend, Sushi Trrend indicator
# Converted and modifiedTrueRange by Sam4Cok@Samer800    - 05/2023
input BarsColor       = no;    
input SupertrendCloud = no;
input ShowTrendLine = yes;
input ShowZigZag = no;
input ShowMovAvgLine = no;
input ShowSignals = yes;
input UseMovAvgFilter = no;
input supertrendSource = hl2;
input Period = 12;        # "Length"
input factor = 2.0;       # "Factor"
input FactorForSushiTrend = 5;#, title = 'Engulfing Factor')
input CalcMethod = {"ATR Trend",Default "Bollinger Trend", "Sushi Trend"};
input averageType = {default "ZLSMA", "TRAMA","VAWMA","eVWMA","SMMA", "EMA", "TEMA","WMA","VWMA","SMA","HMA", "McGinley"};;
input MovAvgLength = 100;
input MovAvgSrc    = close;
#input useChartTimeFrame = yes;
#input manualAggPeriod = AggregationPeriod.FIFTEEN_MIN;

def na = Double.NaN;
def src = supertrendSource;
def bodyMiddle = ohlc4;
def current = GetAggregationPeriod();
#def tf = if useChartTimeFrame then current else manualAggPeriod;
#def zlsmaSrc = if useChartTimeFrame then MovAvgSrc else close(Period=tf);
#---color
DefineGlobalColor("Blue" , CreateColor(33,150,243));
DefineGlobalColor("up", CreateColor(76,175,80));
DefineGlobalColor("dn", CreateColor(255,82,82));
def atrTrend = CalcMethod==CalcMethod."ATR Trend";
def SushTrend = CalcMethod==CalcMethod."Sushi Trend";

#vwma(source, length)
script VWMA {
    input x = close;
    input y = 15;
    def VWMA = average(x * volume, y) / average(volume, y);
    plot result = VWMA;
}
# vawma(src, len) =>
script vawma {
input src = hlc3;
input len = 50;
def sum; def vol; def v;
def  s   = fold i = 0 to len with p do
           src[i];
def volm = if IsNaN(volm[1]) then 0 else
           fold j = 0 to len with u do
           volume[j];
def ma   = if !IsNaN(s) and !IsNaN(volm) then
           fold k = 0 to len with w do
           len - k  else 0;
       v = fold l=1 to len with q do
           if IsNaN(v[1]) then volm else
           volume * ma;
     vol = fold m = 0 to len with r do
           r + v[m];
     sum = fold n = 0 to len with t do
           t + src[n] * v[n];
plot vawma = sum/vol;
}
#ma(mode, len,  src) =>
script ma {
    input type = "EMA";
    input src = close;
    input len = 100;
    def lsma  = inertia(src, len);
    def lsma2 = inertia(lsma, len);
    def eq    = lsma - lsma2;
    def zlsma = lsma + eq;
    def volumeSum = Sum(volume, len);
    def evwma = ((volumeSum - volume) * evwma[1] + volume * src) / volumeSum;
    def e = ExpAverage(src, len);
    def Mcg = if IsNaN(Mcg[1]) then Average(src, len) else
              CompoundValue(1, Mcg[1] + ((src - Mcg[1]) / (0.6 * len * Power(src / Mcg[1], 4))), src);
    def H = highest(high,len);
    def L = Lowest(low,len);
    def HH = Max(Sign(H - H[1]),0);
    def LL = Max(Sign(L - L[1])*-1,0);
    def TC = Power(Average(if HH or LL then 1 else 0, Len),2);
    def ama = CompoundValue(1, ama[1] + tc * (src - ama[1]), src);
    def ma;
ma = if type == "SMA"   then SimpleMovingAvg(src, len) else
     if type == "ZLSMA" then zlsma else  
     if type == "VAWMA" then vawma(src, len) else
     if type == "EMA"   then ExpAverage(src, len) else
     if type == "TEMA"  then 3 * (e - ExpAverage(e, len)) + ExpAverage(ExpAverage(e, len), len) else
     if type == "WMA"   then WMA(src, len) else
     if type == "VWMA"  then vwma(src, len) else
     if type == "SMMA"  then WildersSmoothing(src, len) else
     if type == "HMA"   then WMA(2 * WMA(src, len / 2) - WMA(src, len), Round(Sqrt(len), 0)) else
     if type == "eVWMA" then evwma else
     if type == "TRAMA" then ama else
     if type == "McGinley" then Mcg else Double.NaN;
    plot result = ma;
}
#pine_supertrend(src, factor, atrPeriod) =>
script supertrend {
    input src = hl2;
    input factor = 3;
    input Period = 10;
    input atrTrend = yes;
    input MovAvgType = AverageType.SIMPLE;
    def lowerBand;
    def upperBand;
    def nATR = atr(LENGTH=Period);
    def bbup = ma(MovAvgType, high,Period) + stdev(high,Period) * factor;
    def bbdn = ma(MovAvgType, low,Period)  - stdev(low,Period) * factor;
    def atrup = src + factor * nATR;
    def atrdn = src - factor * nATR;
    def up = if atrTrend then atrup else bbup;
    def dn = if atrTrend then atrdn else bbdn;
    def up1 = if (isNaN(upperBand[1]) or upperBand[1]==0) then up else if(upperBand[1]==0, up, upperBand[1]);
    def dn1 = if (isNaN(lowerBand[1]) or lowerBand[1]==0) then dn else if(lowerBand[1]==0, dn, lowerBand[1]);
    upperBand = if (up < up1) or (close[1] > up1) then up else up1;
    lowerBand = if (dn > dn1) or (close[1] < dn1) then dn else dn1;
    def trend;# = na
    def superTrend;# = na
    def prevSuperTrend = if(superTrend[1]==0, up1, superTrend[1]);
    if isNaN(nATR[1]) or isNaN(stdev(src,Period)) {
        trend = 1;
    } else
    if prevSuperTrend == up1 {
        trend = if close > upperBand then -1 else 1;
    } else {
        trend = if close < lowerBand then  1 else -1;
    }
    superTrend = if trend == -1 then lowerBand else upperBand;
    plot ST = superTrend;
    plot dir = trend;
}
#-- Moving Avg
def MovAvg = ma(averageType, MovAvgSrc, MovAvgLength);
plot MovAvgLine = if ShowMovAvgLine then MovAvg else na;
MovAvgLine.SetDefaultColor(Color.CYAN);

#-- ST
def supertrend = if SushTrend then na else supertrend(src, factor, Period, atrTrend, averageType).ST;
def direction  = if SushTrend then na else supertrend(src, factor, Period, atrTrend, averageType).DIR;

plot upTrend = if ShowTrendLine and (direction < 0) then supertrend else na;#, "Up Trend"
upTrend.SetDefaultColor(GlobalColor("up"));
plot downTrend = if ShowTrendLine and (direction > 0) then supertrend else na;#, "Down Trend"
downTrend.SetDefaultColor(GlobalColor("dn"));

def LongTrigger  = direction!=direction[1] and direction<0;
def ShortTrigger = direction!=direction[1] and direction>0;

plot LongDot = if ShowTrendLine and LongTrigger then upTrend else na;
LongDot.SetPaintingStrategy(PaintingStrategy.POINTS);
LongDot.AssignValueColor(GlobalColor("up"));
LongDot.SetLineWeight(4);
plot ShortDot = if ShowTrendLine and ShortTrigger then downTrend else na;
ShortDot.SetPaintingStrategy(PaintingStrategy.POINTS);
ShortDot.AssignValueColor(GlobalColor("dn"));
ShortDot.SetLineWeight(4);

AddChartBubble(ShowSignals and LongTrigger and if(UseMovAvgFilter, src>MovAvg, 1),
              if(ShowTrendLine,supertrend, low), "Bull", Color.GREEN, no);
AddChartBubble(ShowSignals and ShortTrigger and if(UseMovAvgFilter, src<MovAvg, 1),
              if(ShowTrendLine, supertrend, high), "Bear",  Color.RED, yes);


AddCloud(bodyMiddle, upTrend, Color.DARK_GREEN);
AddCloud(downTrend, bodyMiddle, Color.DARK_RED);

#-- END CODE--
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © HoanGhetti
#indicator("Sushi Trend [HG]", overlay = true)

def last = isNaN(close) or !SushTrend;

#setMinMax(float value, int row, bool set = false) =>
script setMinMax {
    input value = high;
    input row   = 0;
    input factor = 5;

    def maxResult = fold j = 0 to factor   with q do
                    if value==high[j] then q + 1 else q;
    def minResult = fold j1 = 0 to factor  with q1 do
                    if value==low[j1] then q1 + 1 else q1;

    def HighResult = fold i = 0 to factor  with p=high do
                     if maxResult[i]==1 then high[i] else p;
    def LowResult  = fold i1 = 0 to factor  with p1=low do
                    if minResult[i1]==1 then low[i1] else p1;

    def out = if row == 0 then maxResult>0 else minResult>0;
    def ou1 = if row == 0 then HighResult else LowResult;
    plot MaxMin = out;
    plot HiLo = ou1;
}

def stMatrixMax = Highest(high, FactorForSushiTrend *2);
def stMatrixMin = Lowest(low  , FactorForSushiTrend *2);
def max = setMinMax(stMatrixMax, 0, FactorForSushiTrend).MaxMin;
def min = setMinMax(stMatrixMin, 1, FactorForSushiTrend).MaxMin;
def setMax = setMinMax(stMatrixMax, 0, FactorForSushiTrend).HiLo;
def setMin = setMinMax(stMatrixMin, 1, FactorForSushiTrend).HiLo;

def valid = min and max and (stMatrixMin == setMin or stMatrixMax == setMax);
def barssince = if valid[1] then 1 else barssince[1] + 1;

def sushi = valid and barssince >= factor and !IsNaN(close);

def lastMax = if (sushi-sushi[1]) and sushi and max then stMatrixMax else if(lastMax[1]==0,stMatrixMax[1],lastMax[1]);
def lastMin = if (sushi-sushi[1]) and sushi then stMatrixMin else if(lastMin[1]==0, stMatrixMin[1], lastMin[1]);
def Sushidirection;# = na

if crosses(close, lastMax, CrossingDirection.ABOVE) {
    Sushidirection = 1;
    } else
if crosses(close, lastMin, CrossingDirection.BELOW) {
    Sushidirection = 0;
    } else {
    Sushidirection = Sushidirection[1];
}

def sushiTrend = if Sushidirection then lastMin else lastMax;
def sushiChange = SushTrend and Sushidirection-Sushidirection[1];
def sushiLongTrigger = sushiChange and Sushidirection == 1;
def sushiShortTrigger = sushiChange and Sushidirection == 0;

plot stPlot = if last or !ShowTrendLine then na else if !sushiChange then sushiTrend else na;
stPlot.AssignValueColor(if Sushidirection then GlobalColor("up") else GlobalColor("dn"));
#stPlot.SetLineWeight(2);
#def hlPlot = ohlc4;
plot LongSushiDot = if ShowTrendLine and sushiLongTrigger then sushiTrend else na;
LongSushiDot.SetPaintingStrategy(PaintingStrategy.POINTS);
LongSushiDot.AssignValueColor(GlobalColor("up"));
LongSushiDot.SetLineWeight(4);
plot ShortSushiDot = if ShowTrendLine and sushiShortTrigger then sushiTrend else na;
ShortSushiDot.SetPaintingStrategy(PaintingStrategy.POINTS);
ShortSushiDot.AssignValueColor(GlobalColor("dn"));
ShortSushiDot.SetLineWeight(4);

AddCloud(bodyMiddle, stPlot, Color.DARK_GREEN, Color.DARK_RED);

AddChartBubble(ShowSignals and sushiLongTrigger and if(UseMovAvgFilter, src>MovAvg, 1),
               if(ShowTrendLine,sushiTrend, low), "Bull", Color.GREEN, no);
AddChartBubble(ShowSignals and sushiShortTrigger and if(UseMovAvgFilter, src<MovAvg, 1),
               if(ShowTrendLine,sushiTrend, high), "Bear",  Color.RED, yes);

#--- ZigZag
def triggerUp = if SushTrend then sushiLongTrigger else LongTrigger;
def triggerDn = if SushTrend then sushiShortTrigger else ShortTrigger;
def zigzagDir = if SushTrend then if(Sushidirection==1, -1, 1) else direction;
def ZigZagSrc = if SushTrend then sushiTrend else supertrend;

plot zigzag = if !ShowZigZag then na else
              if triggerUp then ZigZagSrc else if triggerDn then ZigZagSrc else
              if isNaN(close[-1]) then
              if  zigzagDir>0 then highest(src, Period) else lowest(src,Period) else na;
zigzag.AssignValueColor(if zigzagDir[1]<0 then Color.WHITE else Color.GRAY);
zigzag.SetStyle(Curve.SHORT_DASH);
zigzag.ENAbleApproximation();

#--- END CODE
def lastUp = if triggerUp then ZigZagSrc else if(lastUp[1]==0,ZigZagSrc, lastUp[1]);
def lastDn = if triggerDn then ZigZagSrc else if(lastDn[1]==0,ZigZagSrc, lastDn[1]);

def centerUp;def centerDn;

if (lastUp!=lastUp[1]) {
        centerUp = (if(centerUp[1]==0, lastUp, centerUp[1]) + lastUp) / 2;
    } else {
        centerUp =  if(centerUp[1]==0, lastUp, centerUp[1]);
}
if (lastDn!=lastDn[1]) {
        centerDn = (if(centerDn[1]==0, lastDn, centerDn[1]) + lastDn) / 2;
    } else {
        centerDn =  if(centerDn[1]==0, lastDn, centerDn[1]);
}
def midSupUp = ma(averageType, centerUp , Period);
def midSupDn = ma(averageType, centerDn , Period);
def midSup = (midSupUp + midSupDn)/2;

plot MidLine = if isNaN(close) or midSup==0 then na else midSup;
MidLine.SetHiding(!SuperTrendCloud);
midLine.AssignValueColor(if midSup < src then GlobalColor("blue") else Color.PLUM);

def MidLineUp = midSupUp;
def MidLineDn = midSupDn;

def trendUp = SuperTrendCloud and midSup < src and midSupUp < src and  midSupDn < src;
def trendDn = SuperTrendCloud and midSup >= src and midSupUp >= src and  midSupDn >= src;

AddCloud(if trendUp then midSupDn else na, midSupUp, GlobalColor("blue"));
AddCloud(if trendDn then midSupDn else na, midSupUp, Color.PLUM);
AddCloud(if (!trendDn and !trendUp and SuperTrendCloud) then midSupDn else na, midSupUp, Color.DARK_GRAY);

#--- Bar Color

def BarUpEx = midSup < src and midSupUp < src and  midSupDn < src;
def BarDnEx = midSup >= src and midSupUp >= src and  midSupDn >= src;
def BarUp = midSup < src and midSupDn < src;
def BarDn = midSup >= src and midSupUp >= src;

AssignPriceColor(if !BarsColor then Color.CURRENT else
                 if BarUpEx then Color.GREEN else
                 if BarUp   then Color.DARK_GREEN else
                 if BarDnEx then Color.RED else
                 if BarDn   then Color.DARK_RED else Color.GRAY);
   


#--- END of CODE
Anyway to make this a Strategy?
 
Anyway to make this a Strategy?
try this. I removed the zigzag option but enabled the moving average as filter.

CSS:
#--- SuperTrend, Bollinger Trend, Sushi Trrend indicator
# Converted and modifiedTrueRange by Sam4Cok@Samer800    - 05/2023
#input BarsColor       = no;    
#input SupertrendCloud = no;
input ShowTrendLine = yes;
input ShowMovAvgLine = no;
input UseMovAvgFilter = yes;
input supertrendSource = hl2;
input Period = 12;        # "Length"
input factor = 2.0;       # "Factor"
input FactorForSushiTrend = 5;#, title = 'Engulfing Factor')
input CalcMethod = {"ATR Trend",Default "Bollinger Trend", "Sushi Trend"};
input averageType = {default "ZLSMA", "TRAMA","VAWMA","eVWMA","SMMA", "EMA", "TEMA","WMA","VWMA","SMA","HMA", "McGinley"};;
input MovAvgLength = 100;
input MovAvgSrc    = close;
#input useChartTimeFrame = yes;
#input manualAggPeriod = AggregationPeriod.FIFTEEN_MIN;

def na = Double.NaN;
def src = supertrendSource;
def bodyMiddle = ohlc4;
def current = GetAggregationPeriod();
#def tf = if useChartTimeFrame then current else manualAggPeriod;
#def zlsmaSrc = if useChartTimeFrame then MovAvgSrc else close(Period=tf);
#---color
DefineGlobalColor("Blue" , CreateColor(33,150,243));
DefineGlobalColor("up", CreateColor(76,175,80));
DefineGlobalColor("dn", CreateColor(255,82,82));
def atrTrend = CalcMethod==CalcMethod."ATR Trend";
def SushTrend = CalcMethod==CalcMethod."Sushi Trend";

#vwma(source, length)
script VWMA {
    input x = close;
    input y = 15;
    def VWMA = average(x * volume, y) / average(volume, y);
    plot result = VWMA;
}
# vawma(src, len) =>
script vawma {
input src = hlc3;
input len = 50;
def sum; def vol; def v;
def  s   = fold i = 0 to len with p do
           src[i];
def volm = if IsNaN(volm[1]) then 0 else
           fold j = 0 to len with u do
           volume[j];
def ma   = if !IsNaN(s) and !IsNaN(volm) then
           fold k = 0 to len with w do
           len - k  else 0;
       v = fold l=1 to len with q do
           if IsNaN(v[1]) then volm else
           volume * ma;
     vol = fold m = 0 to len with r do
           r + v[m];
     sum = fold n = 0 to len with t do
           t + src[n] * v[n];
plot vawma = sum/vol;
}
#ma(mode, len,  src) =>
script ma {
    input type = "EMA";
    input src = close;
    input len = 100;
    def lsma  = inertia(src, len);
    def lsma2 = inertia(lsma, len);
    def eq    = lsma - lsma2;
    def zlsma = lsma + eq;
    def volumeSum = Sum(volume, len);
    def evwma = ((volumeSum - volume) * evwma[1] + volume * src) / volumeSum;
    def e = ExpAverage(src, len);
    def Mcg = if IsNaN(Mcg[1]) then Average(src, len) else
              CompoundValue(1, Mcg[1] + ((src - Mcg[1]) / (0.6 * len * Power(src / Mcg[1], 4))), src);
    def H = highest(high,len);
    def L = Lowest(low,len);
    def HH = Max(Sign(H - H[1]),0);
    def LL = Max(Sign(L - L[1])*-1,0);
    def TC = Power(Average(if HH or LL then 1 else 0, Len),2);
    def ama = CompoundValue(1, ama[1] + tc * (src - ama[1]), src);
    def ma;
ma = if type == "SMA"   then SimpleMovingAvg(src, len) else
     if type == "ZLSMA" then zlsma else  
     if type == "VAWMA" then vawma(src, len) else
     if type == "EMA"   then ExpAverage(src, len) else
     if type == "TEMA"  then 3 * (e - ExpAverage(e, len)) + ExpAverage(ExpAverage(e, len), len) else
     if type == "WMA"   then WMA(src, len) else
     if type == "VWMA"  then vwma(src, len) else
     if type == "SMMA"  then WildersSmoothing(src, len) else
     if type == "HMA"   then WMA(2 * WMA(src, len / 2) - WMA(src, len), Round(Sqrt(len), 0)) else
     if type == "eVWMA" then evwma else
     if type == "TRAMA" then ama else
     if type == "McGinley" then Mcg else Double.NaN;
    plot result = ma;
}
#pine_supertrend(src, factor, atrPeriod) =>
script supertrend {
    input src = hl2;
    input factor = 3;
    input Period = 10;
    input atrTrend = yes;
    input MovAvgType = AverageType.SIMPLE;
    def lowerBand;
    def upperBand;
    def nATR = atr(LENGTH=Period);
    def bbup = ma(MovAvgType, high,Period) + stdev(high,Period) * factor;
    def bbdn = ma(MovAvgType, low,Period)  - stdev(low,Period) * factor;
    def atrup = src + factor * nATR;
    def atrdn = src - factor * nATR;
    def up = if atrTrend then atrup else bbup;
    def dn = if atrTrend then atrdn else bbdn;
    def up1 = if (isNaN(upperBand[1]) or upperBand[1]==0) then up else if(upperBand[1]==0, up, upperBand[1]);
    def dn1 = if (isNaN(lowerBand[1]) or lowerBand[1]==0) then dn else if(lowerBand[1]==0, dn, lowerBand[1]);
    upperBand = if (up < up1) or (close[1] > up1) then up else up1;
    lowerBand = if (dn > dn1) or (close[1] < dn1) then dn else dn1;
    def trend;# = na
    def superTrend;# = na
    def prevSuperTrend = if(superTrend[1]==0, up1, superTrend[1]);
    if isNaN(nATR[1]) or isNaN(stdev(src,Period)) {
        trend = 1;
    } else
    if prevSuperTrend == up1 {
        trend = if close > upperBand then -1 else 1;
    } else {
        trend = if close < lowerBand then  1 else -1;
    }
    superTrend = if trend == -1 then lowerBand else upperBand;
    plot ST = superTrend;
    plot dir = trend;
}
#-- Moving Avg
def MovAvg = ma(averageType, MovAvgSrc, MovAvgLength);
plot MovAvgLine = if ShowMovAvgLine then MovAvg else na;
MovAvgLine.SetDefaultColor(Color.CYAN);

#-- ST
def supertrend = if SushTrend then na else supertrend(src, factor, Period, atrTrend, averageType).ST;
def direction  = if SushTrend then na else supertrend(src, factor, Period, atrTrend, averageType).DIR;

plot upTrend = if ShowTrendLine and (direction < 0) then supertrend else na;#, "Up Trend"
upTrend.SetDefaultColor(GlobalColor("up"));
plot downTrend = if ShowTrendLine and (direction > 0) then supertrend else na;#, "Down Trend"
downTrend.SetDefaultColor(GlobalColor("dn"));

def LongTrigger  = direction!=direction[1] and direction<0;
def ShortTrigger = direction!=direction[1] and direction>0;

plot LongDot = if ShowTrendLine and LongTrigger then upTrend else na;
LongDot.SetPaintingStrategy(PaintingStrategy.POINTS);
LongDot.AssignValueColor(GlobalColor("up"));
LongDot.SetLineWeight(4);
plot ShortDot = if ShowTrendLine and ShortTrigger then downTrend else na;
ShortDot.SetPaintingStrategy(PaintingStrategy.POINTS);
ShortDot.AssignValueColor(GlobalColor("dn"));
ShortDot.SetLineWeight(4);

AddCloud(bodyMiddle, upTrend, Color.DARK_GREEN);
AddCloud(downTrend, bodyMiddle, Color.DARK_RED);

#-- END CODE--
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © HoanGhetti
#indicator("Sushi Trend [HG]", overlay = true)

def last = isNaN(close) or !SushTrend;

#setMinMax(float value, int row, bool set = false) =>
script setMinMax {
    input value = high;
    input row   = 0;
    input factor = 5;

    def maxResult = fold j = 0 to factor   with q do
                    if value==high[j] then q + 1 else q;
    def minResult = fold j1 = 0 to factor  with q1 do
                    if value==low[j1] then q1 + 1 else q1;

    def HighResult = fold i = 0 to factor  with p=high do
                     if maxResult[i]==1 then high[i] else p;
    def LowResult  = fold i1 = 0 to factor  with p1=low do
                    if minResult[i1]==1 then low[i1] else p1;

    def out = if row == 0 then maxResult>0 else minResult>0;
    def ou1 = if row == 0 then HighResult else LowResult;
    plot MaxMin = out;
    plot HiLo = ou1;
}

def stMatrixMax = Highest(high, FactorForSushiTrend *2);
def stMatrixMin = Lowest(low  , FactorForSushiTrend *2);
def max = setMinMax(stMatrixMax, 0, FactorForSushiTrend).MaxMin;
def min = setMinMax(stMatrixMin, 1, FactorForSushiTrend).MaxMin;
def setMax = setMinMax(stMatrixMax, 0, FactorForSushiTrend).HiLo;
def setMin = setMinMax(stMatrixMin, 1, FactorForSushiTrend).HiLo;

def valid = min and max and (stMatrixMin == setMin or stMatrixMax == setMax);
def barssince = if valid[1] then 1 else barssince[1] + 1;

def sushi = valid and barssince >= factor and !IsNaN(close);

def lastMax = if (sushi-sushi[1]) and sushi and max then stMatrixMax else if(lastMax[1]==0,stMatrixMax[1],lastMax[1]);
def lastMin = if (sushi-sushi[1]) and sushi then stMatrixMin else if(lastMin[1]==0, stMatrixMin[1], lastMin[1]);
def Sushidirection;# = na

if crosses(close, lastMax, CrossingDirection.ABOVE) {
    Sushidirection = 1;
    } else
if crosses(close, lastMin, CrossingDirection.BELOW) {
    Sushidirection = 0;
    } else {
    Sushidirection = Sushidirection[1];
}

def sushiTrend = if Sushidirection then lastMin else lastMax;
def sushiChange = SushTrend and Sushidirection-Sushidirection[1];
def sushiLongTrigger = sushiChange and Sushidirection == 1;
def sushiShortTrigger = sushiChange and Sushidirection == 0;

plot stPlot = if last or !ShowTrendLine then na else if !sushiChange then sushiTrend else na;
stPlot.AssignValueColor(if Sushidirection then GlobalColor("up") else GlobalColor("dn"));
#stPlot.SetLineWeight(2);
#def hlPlot = ohlc4;
plot LongSushiDot = if ShowTrendLine and sushiLongTrigger then sushiTrend else na;
LongSushiDot.SetPaintingStrategy(PaintingStrategy.POINTS);
LongSushiDot.AssignValueColor(GlobalColor("up"));
LongSushiDot.SetLineWeight(4);
plot ShortSushiDot = if ShowTrendLine and sushiShortTrigger then sushiTrend else na;
ShortSushiDot.SetPaintingStrategy(PaintingStrategy.POINTS);
ShortSushiDot.AssignValueColor(GlobalColor("dn"));
ShortSushiDot.SetLineWeight(4);

AddCloud(bodyMiddle, stPlot, Color.DARK_GREEN, Color.DARK_RED);


#--- END of CODE
def goLong = if sushTrend then sushiLongTrigger and if(UseMovAvgFilter, src>MovAvg, 1) else
                LongTrigger and if(UseMovAvgFilter, src>MovAvg, 1);
def goShort = if sushTrend then sushiShortTrigger and if(UseMovAvgFilter, src<MovAvg, 1) else
                ShortTrigger and if(UseMovAvgFilter, src<MovAvg, 1);

#--- END of CODE
def buy     = goLong;
def sell    = goShort;

#--- END Code

input OpenTime = 0930; #hint OpenTime: Opening time of market
input CloseTime = 1600; #hint CloseTime: Closing time of market
input StopPoints = 3.0; #hint: StopPct: Close position if target hit
input TradeSize = 1; #hint : TradeSize: How many shares/contract

# Set market hours
def Begin = SecondsFromTime(OpenTime);
def End = SecondsTillTime(CloseTime);

def SpansMidnight = if CloseTime < OpenTime then 1 else 0;

def isIntraDay = if GetAggregationPeriod() > 14400000 or GetAggregationPeriod() == 0 then 0 else 1;

def MarketOpen = if !isIntraDay then 1 else if !SpansMidnight and (Begin[-1] == 0 or Begin[-1] > 0) and (End > 0) then 1 else if SpansMidnight and (((Begin == 0 or Begin > 0) and (End < 0)) OR (Begin < 0 and End > 0)) then 1 else 0;

def FirstCandle = Begin[1] < 0 and (Begin == 0 or Begin > 0);
def LastCandle = MarketOpen[-1] == 0;
def EP = entryPrice();


AddOrder(OrderType.BUY_AUTO,buy, tickColor = GetColor(0), arrowColor = GetColor(0), name = "Buy");
AddOrder(OrderType.SELL_AUTO, sell, tickColor = GetColor(1), arrowColor = GetColor(1), name = "Sell");

AddOrder(OrderType.BUY_TO_OPEN, FirstCandle == 1, open[-1], TradeSize, name = "BTO: " + Close);
AddOrder(OrderType.SELL_TO_CLOSE, close > EP + StopPoints, open[-1], Tradesize, name="Target: " + Close);
AddOrder(OrderType.SELL_TO_CLOSE, LastCandle == 1, open[-1], Tradesize, name="MKT Close: " + Close);
#### END
 
Last edited by a moderator:
Any particular timeframe to use this on? And I assume without the zigzag option, it will still repaint?
just to clarify. ZigZag option nothing to do with trend calculation. The ZigZag will be plot based on the trend change not the opposite. Only last zigzag line will be unconfirmed till we have trend change.
 
View attachment 18673
SuperBollingerTrend:
https://www.tradingview.com/script/AjWfiZpw-SuperBollingerTrend-Expo/

SushiTrend:
https://www.tradingview.com/v/dfVzbU6r/

CODE:
CSS:
#--- SuperTrend, Bollinger Trend, Sushi Trrend indicator
# Converted and modifiedTrueRange by Sam4Cok@Samer800    - 05/2023
input BarsColor       = no;    
input SupertrendCloud = no;
input ShowTrendLine = yes;
input ShowZigZag = no;
input ShowMovAvgLine = no;
input ShowSignals = yes;
input UseMovAvgFilter = no;
input supertrendSource = hl2;
input Period = 12;        # "Length"
input factor = 2.0;       # "Factor"
input FactorForSushiTrend = 5;#, title = 'Engulfing Factor')
input CalcMethod = {"ATR Trend",Default "Bollinger Trend", "Sushi Trend"};
input averageType = {default "ZLSMA", "TRAMA","VAWMA","eVWMA","SMMA", "EMA", "TEMA","WMA","VWMA","SMA","HMA", "McGinley"};;
input MovAvgLength = 100;
input MovAvgSrc    = close;
#input useChartTimeFrame = yes;
#input manualAggPeriod = AggregationPeriod.FIFTEEN_MIN;

def na = Double.NaN;
def src = supertrendSource;
def bodyMiddle = ohlc4;
def current = GetAggregationPeriod();
#def tf = if useChartTimeFrame then current else manualAggPeriod;
#def zlsmaSrc = if useChartTimeFrame then MovAvgSrc else close(Period=tf);
#---color
DefineGlobalColor("Blue" , CreateColor(33,150,243));
DefineGlobalColor("up", CreateColor(76,175,80));
DefineGlobalColor("dn", CreateColor(255,82,82));
def atrTrend = CalcMethod==CalcMethod."ATR Trend";
def SushTrend = CalcMethod==CalcMethod."Sushi Trend";

#vwma(source, length)
script VWMA {
    input x = close;
    input y = 15;
    def VWMA = average(x * volume, y) / average(volume, y);
    plot result = VWMA;
}
# vawma(src, len) =>
script vawma {
input src = hlc3;
input len = 50;
def sum; def vol; def v;
def  s   = fold i = 0 to len with p do
           src[i];
def volm = if IsNaN(volm[1]) then 0 else
           fold j = 0 to len with u do
           volume[j];
def ma   = if !IsNaN(s) and !IsNaN(volm) then
           fold k = 0 to len with w do
           len - k  else 0;
       v = fold l=1 to len with q do
           if IsNaN(v[1]) then volm else
           volume * ma;
     vol = fold m = 0 to len with r do
           r + v[m];
     sum = fold n = 0 to len with t do
           t + src[n] * v[n];
plot vawma = sum/vol;
}
#ma(mode, len,  src) =>
script ma {
    input type = "EMA";
    input src = close;
    input len = 100;
    def lsma  = inertia(src, len);
    def lsma2 = inertia(lsma, len);
    def eq    = lsma - lsma2;
    def zlsma = lsma + eq;
    def volumeSum = Sum(volume, len);
    def evwma = ((volumeSum - volume) * evwma[1] + volume * src) / volumeSum;
    def e = ExpAverage(src, len);
    def Mcg = if IsNaN(Mcg[1]) then Average(src, len) else
              CompoundValue(1, Mcg[1] + ((src - Mcg[1]) / (0.6 * len * Power(src / Mcg[1], 4))), src);
    def H = highest(high,len);
    def L = Lowest(low,len);
    def HH = Max(Sign(H - H[1]),0);
    def LL = Max(Sign(L - L[1])*-1,0);
    def TC = Power(Average(if HH or LL then 1 else 0, Len),2);
    def ama = CompoundValue(1, ama[1] + tc * (src - ama[1]), src);
    def ma;
ma = if type == "SMA"   then SimpleMovingAvg(src, len) else
     if type == "ZLSMA" then zlsma else  
     if type == "VAWMA" then vawma(src, len) else
     if type == "EMA"   then ExpAverage(src, len) else
     if type == "TEMA"  then 3 * (e - ExpAverage(e, len)) + ExpAverage(ExpAverage(e, len), len) else
     if type == "WMA"   then WMA(src, len) else
     if type == "VWMA"  then vwma(src, len) else
     if type == "SMMA"  then WildersSmoothing(src, len) else
     if type == "HMA"   then WMA(2 * WMA(src, len / 2) - WMA(src, len), Round(Sqrt(len), 0)) else
     if type == "eVWMA" then evwma else
     if type == "TRAMA" then ama else
     if type == "McGinley" then Mcg else Double.NaN;
    plot result = ma;
}
#pine_supertrend(src, factor, atrPeriod) =>
script supertrend {
    input src = hl2;
    input factor = 3;
    input Period = 10;
    input atrTrend = yes;
    input MovAvgType = AverageType.SIMPLE;
    def lowerBand;
    def upperBand;
    def nATR = atr(LENGTH=Period);
    def bbup = ma(MovAvgType, high,Period) + stdev(high,Period) * factor;
    def bbdn = ma(MovAvgType, low,Period)  - stdev(low,Period) * factor;
    def atrup = src + factor * nATR;
    def atrdn = src - factor * nATR;
    def up = if atrTrend then atrup else bbup;
    def dn = if atrTrend then atrdn else bbdn;
    def up1 = if (isNaN(upperBand[1]) or upperBand[1]==0) then up else if(upperBand[1]==0, up, upperBand[1]);
    def dn1 = if (isNaN(lowerBand[1]) or lowerBand[1]==0) then dn else if(lowerBand[1]==0, dn, lowerBand[1]);
    upperBand = if (up < up1) or (close[1] > up1) then up else up1;
    lowerBand = if (dn > dn1) or (close[1] < dn1) then dn else dn1;
    def trend;# = na
    def superTrend;# = na
    def prevSuperTrend = if(superTrend[1]==0, up1, superTrend[1]);
    if isNaN(nATR[1]) or isNaN(stdev(src,Period)) {
        trend = 1;
    } else
    if prevSuperTrend == up1 {
        trend = if close > upperBand then -1 else 1;
    } else {
        trend = if close < lowerBand then  1 else -1;
    }
    superTrend = if trend == -1 then lowerBand else upperBand;
    plot ST = superTrend;
    plot dir = trend;
}
#-- Moving Avg
def MovAvg = ma(averageType, MovAvgSrc, MovAvgLength);
plot MovAvgLine = if ShowMovAvgLine then MovAvg else na;
MovAvgLine.SetDefaultColor(Color.CYAN);

#-- ST
def supertrend = if SushTrend then na else supertrend(src, factor, Period, atrTrend, averageType).ST;
def direction  = if SushTrend then na else supertrend(src, factor, Period, atrTrend, averageType).DIR;

plot upTrend = if ShowTrendLine and (direction < 0) then supertrend else na;#, "Up Trend"
upTrend.SetDefaultColor(GlobalColor("up"));
plot downTrend = if ShowTrendLine and (direction > 0) then supertrend else na;#, "Down Trend"
downTrend.SetDefaultColor(GlobalColor("dn"));

def LongTrigger  = direction!=direction[1] and direction<0;
def ShortTrigger = direction!=direction[1] and direction>0;

plot LongDot = if ShowTrendLine and LongTrigger then upTrend else na;
LongDot.SetPaintingStrategy(PaintingStrategy.POINTS);
LongDot.AssignValueColor(GlobalColor("up"));
LongDot.SetLineWeight(4);
plot ShortDot = if ShowTrendLine and ShortTrigger then downTrend else na;
ShortDot.SetPaintingStrategy(PaintingStrategy.POINTS);
ShortDot.AssignValueColor(GlobalColor("dn"));
ShortDot.SetLineWeight(4);

AddChartBubble(ShowSignals and LongTrigger and if(UseMovAvgFilter, src>MovAvg, 1),
              if(ShowTrendLine,supertrend, low), "Bull", Color.GREEN, no);
AddChartBubble(ShowSignals and ShortTrigger and if(UseMovAvgFilter, src<MovAvg, 1),
              if(ShowTrendLine, supertrend, high), "Bear",  Color.RED, yes);


AddCloud(bodyMiddle, upTrend, Color.DARK_GREEN);
AddCloud(downTrend, bodyMiddle, Color.DARK_RED);

#-- END CODE--
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © HoanGhetti
#indicator("Sushi Trend [HG]", overlay = true)

def last = isNaN(close) or !SushTrend;

#setMinMax(float value, int row, bool set = false) =>
script setMinMax {
    input value = high;
    input row   = 0;
    input factor = 5;

    def maxResult = fold j = 0 to factor   with q do
                    if value==high[j] then q + 1 else q;
    def minResult = fold j1 = 0 to factor  with q1 do
                    if value==low[j1] then q1 + 1 else q1;

    def HighResult = fold i = 0 to factor  with p=high do
                     if maxResult[i]==1 then high[i] else p;
    def LowResult  = fold i1 = 0 to factor  with p1=low do
                    if minResult[i1]==1 then low[i1] else p1;

    def out = if row == 0 then maxResult>0 else minResult>0;
    def ou1 = if row == 0 then HighResult else LowResult;
    plot MaxMin = out;
    plot HiLo = ou1;
}

def stMatrixMax = Highest(high, FactorForSushiTrend *2);
def stMatrixMin = Lowest(low  , FactorForSushiTrend *2);
def max = setMinMax(stMatrixMax, 0, FactorForSushiTrend).MaxMin;
def min = setMinMax(stMatrixMin, 1, FactorForSushiTrend).MaxMin;
def setMax = setMinMax(stMatrixMax, 0, FactorForSushiTrend).HiLo;
def setMin = setMinMax(stMatrixMin, 1, FactorForSushiTrend).HiLo;

def valid = min and max and (stMatrixMin == setMin or stMatrixMax == setMax);
def barssince = if valid[1] then 1 else barssince[1] + 1;

def sushi = valid and barssince >= factor and !IsNaN(close);

def lastMax = if (sushi-sushi[1]) and sushi and max then stMatrixMax else if(lastMax[1]==0,stMatrixMax[1],lastMax[1]);
def lastMin = if (sushi-sushi[1]) and sushi then stMatrixMin else if(lastMin[1]==0, stMatrixMin[1], lastMin[1]);
def Sushidirection;# = na

if crosses(close, lastMax, CrossingDirection.ABOVE) {
    Sushidirection = 1;
    } else
if crosses(close, lastMin, CrossingDirection.BELOW) {
    Sushidirection = 0;
    } else {
    Sushidirection = Sushidirection[1];
}

def sushiTrend = if Sushidirection then lastMin else lastMax;
def sushiChange = SushTrend and Sushidirection-Sushidirection[1];
def sushiLongTrigger = sushiChange and Sushidirection == 1;
def sushiShortTrigger = sushiChange and Sushidirection == 0;

plot stPlot = if last or !ShowTrendLine then na else if !sushiChange then sushiTrend else na;
stPlot.AssignValueColor(if Sushidirection then GlobalColor("up") else GlobalColor("dn"));
#stPlot.SetLineWeight(2);
#def hlPlot = ohlc4;
plot LongSushiDot = if ShowTrendLine and sushiLongTrigger then sushiTrend else na;
LongSushiDot.SetPaintingStrategy(PaintingStrategy.POINTS);
LongSushiDot.AssignValueColor(GlobalColor("up"));
LongSushiDot.SetLineWeight(4);
plot ShortSushiDot = if ShowTrendLine and sushiShortTrigger then sushiTrend else na;
ShortSushiDot.SetPaintingStrategy(PaintingStrategy.POINTS);
ShortSushiDot.AssignValueColor(GlobalColor("dn"));
ShortSushiDot.SetLineWeight(4);

AddCloud(bodyMiddle, stPlot, Color.DARK_GREEN, Color.DARK_RED);

AddChartBubble(ShowSignals and sushiLongTrigger and if(UseMovAvgFilter, src>MovAvg, 1),
               if(ShowTrendLine,sushiTrend, low), "Bull", Color.GREEN, no);
AddChartBubble(ShowSignals and sushiShortTrigger and if(UseMovAvgFilter, src<MovAvg, 1),
               if(ShowTrendLine,sushiTrend, high), "Bear",  Color.RED, yes);

#--- ZigZag
def triggerUp = if SushTrend then sushiLongTrigger else LongTrigger;
def triggerDn = if SushTrend then sushiShortTrigger else ShortTrigger;
def zigzagDir = if SushTrend then if(Sushidirection==1, -1, 1) else direction;
def ZigZagSrc = if SushTrend then sushiTrend else supertrend;

plot zigzag = if !ShowZigZag then na else
              if triggerUp then ZigZagSrc else if triggerDn then ZigZagSrc else
              if isNaN(close[-1]) then
              if  zigzagDir>0 then highest(src, Period) else lowest(src,Period) else na;
zigzag.AssignValueColor(if zigzagDir[1]<0 then Color.WHITE else Color.GRAY);
zigzag.SetStyle(Curve.SHORT_DASH);
zigzag.ENAbleApproximation();

#--- END CODE
def lastUp = if triggerUp then ZigZagSrc else if(lastUp[1]==0,ZigZagSrc, lastUp[1]);
def lastDn = if triggerDn then ZigZagSrc else if(lastDn[1]==0,ZigZagSrc, lastDn[1]);

def centerUp;def centerDn;

if (lastUp!=lastUp[1]) {
        centerUp = (if(centerUp[1]==0, lastUp, centerUp[1]) + lastUp) / 2;
    } else {
        centerUp =  if(centerUp[1]==0, lastUp, centerUp[1]);
}
if (lastDn!=lastDn[1]) {
        centerDn = (if(centerDn[1]==0, lastDn, centerDn[1]) + lastDn) / 2;
    } else {
        centerDn =  if(centerDn[1]==0, lastDn, centerDn[1]);
}
def midSupUp = ma(averageType, centerUp , Period);
def midSupDn = ma(averageType, centerDn , Period);
def midSup = (midSupUp + midSupDn)/2;

plot MidLine = if isNaN(close) or midSup==0 then na else midSup;
MidLine.SetHiding(!SuperTrendCloud);
midLine.AssignValueColor(if midSup < src then GlobalColor("blue") else Color.PLUM);

def MidLineUp = midSupUp;
def MidLineDn = midSupDn;

def trendUp = SuperTrendCloud and midSup < src and midSupUp < src and  midSupDn < src;
def trendDn = SuperTrendCloud and midSup >= src and midSupUp >= src and  midSupDn >= src;

AddCloud(if trendUp then midSupDn else na, midSupUp, GlobalColor("blue"));
AddCloud(if trendDn then midSupDn else na, midSupUp, Color.PLUM);
AddCloud(if (!trendDn and !trendUp and SuperTrendCloud) then midSupDn else na, midSupUp, Color.DARK_GRAY);

#--- Bar Color

def BarUpEx = midSup < src and midSupUp < src and  midSupDn < src;
def BarDnEx = midSup >= src and midSupUp >= src and  midSupDn >= src;
def BarUp = midSup < src and midSupDn < src;
def BarDn = midSup >= src and midSupUp >= src;

AssignPriceColor(if !BarsColor then Color.CURRENT else
                 if BarUpEx then Color.GREEN else
                 if BarUp   then Color.DARK_GREEN else
                 if BarDnEx then Color.RED else
                 if BarDn   then Color.DARK_RED else Color.GRAY);
   


#--- END of CODE
Is it possible to build a scanner from this? Thanks
 
Is it possible to build a scanner from this? Thanks

No, it can not be used in a scan.
When reviewing threads found on the forum. Make note of the ones that have a "repaints" prefix.
Repainters for the most part are too complex to use in scans, watchlists and conditional orders.

Read more about repainters:
https://usethinkscript.com/threads/answers-to-commonly-asked-questions.6006/#post-57833

The amazing @samer800 also provided a non-repainting version; but unfortunately it is still too complex to work in the Scan Hacker.
 
Last edited:
No, it can not be used in a scan.
When reviewing threads found on the forum. Make note of the ones that have a "repaints" prefix.
Repainters for the most part are too complex to use in scans, watchlists and conditional orders.

Read more about repainters:
https://usethinkscript.com/threads/answers-to-commonly-asked-questions.6006/#post-57833

The amazing @samer800 also provided a non-repainting version; but unfortunately it is still too complex to work in the Scan Hacker.
Thanks for the prompt response.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
367 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top