SuperTrended Moving Averages For ThinkOrSwim

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

Hi All,

Could you anyone help us to get TOS conversion for the below indicator, I found this is very useful.

https://www.tradingview.com/script/sfV6H5h5-SuperTrended-Moving-Averages/

check the below

CODE - Updated, code fix - added different Mov Avg Type.

CSS:
# https://www.tradingview.com/v/sfV6H5h5/
#//@KivancOzbilgic
#indicator('SuperTrended Moving Averages', 'ST MA', overlay=true,
#Converted by Sam4Cok@Samer800    - 11/2023
#- Updated - minor code fix - Sam4Cok@Samer800    - 01/2024
input maType  = {SMA, default EMA, HMA, WMA, VWMA, DEMA, ZLSMA, ZLEMA, VAR, TILL, TSF};
input Source = close;
input maLength  = 100;    # " Length MA"
input changeATR = yes;
input atrLength = 10;    # " Length Atr"
input atrMultiplier = 0.5;   # "Band Mult"
input ShowCloud = yes;
input showsignals = no;

def na = Double.NaN;
def avPlot = ohlc4;

#// ] -------------- FUNCTIONS : Moving Avg ------------------ [
#pine_linreg(src, len, offset=0) =>
Script linreg {
input src = close;
input len = 100;
input offset = 0;
    def na = Double.NaN;
    def bar_index = isNaN(close);
    def x_sum = if bar_index then na else
                fold i = 0 to len with p do
                 p + i;
    def xx_sum = if bar_index then na else
                fold ii = 0 to len with pp do
                 pp + ii * ii;
    def y_sum = sum(src, len);
    def xy_sum = fold j = 0 to len with q do
                  q  + j * GetValue(src, len - j - 1);
    def slope = (len * xy_sum - x_sum * y_sum) / (len * xx_sum - x_sum * x_sum);
    def intercept = (y_sum - slope * x_sum) / len;
    def linreg = intercept + slope * (len - offset - 1);
    plot out = linreg;
}
Script TSF {
input src = close;
input length = 100;
    def lrc = Inertia(src, length);
    def lrc1 = linreg(src, length, 1);
    def lrs = lrc - lrc1;
    def TSF = Inertia(src, length) + lrs;
    plot out = TSF;
}
script TILL {
    input src = close;
    input length = 100;
    input T3a1 = 0.7;
    def T3e1 = ExpAverage(src, length);
    def T3e2 = ExpAverage(T3e1, length);
    def T3e3 = ExpAverage(T3e2, length);
    def T3e4 = ExpAverage(T3e3, length);
    def T3e5 = ExpAverage(T3e4, length);
    def T3e6 = ExpAverage(T3e5, length);
    def T3c1 = -T3a1 * T3a1 * T3a1;
    def T3c2 = 3 * T3a1 * T3a1 + 3 * T3a1 * T3a1 * T3a1;
    def T3c3 = -6 * T3a1 * T3a1 - 3 * T3a1 - 3 * T3a1 * T3a1 * T3a1;
    def T3c4 = 1 + 3 * T3a1 + T3a1 * T3a1 * T3a1 + 3 * T3a1 * T3a1;
    def T3 = T3c1 * T3e6 + T3c2 * T3e5 + T3c3 * T3e4 + T3c4 * T3e3;
    plot out = T3;
}
script VAR {
    input src = close;
    input length = 100;
    def curClose = src;
    def prevClose = src[1];
    def valpha = 2 / (length + 1);
    def vud1 = if curClose > prevClose then curClose - prevClose else 0;
    def vdd1 = if prevClose > curClose then prevClose - curClose else 0;
    def vUD = sum(vud1, 9);
    def vDD = sum(vdd1, 9);
    def vCMO = if vUD + vDD == 0 then 0 else (vUD - vDD) / (vUD + vDD);
    def cmo = valpha * AbsValue(vCMO);
    def VAR = CompoundValue(1, cmo * curClose + (1 - cmo) * VAR[1], src);
    plot out = VAR;
}
#export zlSma(float src, simple int len) =>
script ZLSMA {
    input src = close;
    input len = 14;
    def lsma = Inertia(src, len);
    def lsma2 = Inertia(lsma, len);
    def eq = lsma - lsma2;
    def zlsma = lsma + eq;
    plot return = zlsma;
}
#export zlSma(float src, simple int len) =>
script ZLEMA {
    input src = close;
    input len = 14;
    def lenR = Round(len / 2, 0);
    def zxLag = if len / 2 == lenR then len / 2 else (len - 1) / 2;
    def zxEMAData = src + src - src[zxLag];
    def ZLEMA = ExpAverage(zxEMAData, len);
    plot out = ZLEMA;
}
#vwma(source, length)
script VWMA {
    input src = close;
    input len = 15;
    def v = volume;
    def VWMA = SimpleMovingAvg(src * v, len) / SimpleMovingAvg(v, len);
    plot result = VWMA;
}
#export multiMa(float source, simple int length, string type) =>
def mov;
Switch (maType) {
Case SMA : mov = Average(source, maLength);
Case VAR : mov = VAR(source, maLength);
Case WMA : mov = WMA(source, maLength);
Case VWMA : mov = VWMA(source, maLength);
Case DEMA : mov = DEMA(source, maLength);
Case ZLSMA : mov = ZLSMA(source, maLength);
Case ZLEMA : mov = ZLEMA(source, maLength);
Case TILL : mov = TILL(source, maLength);
Case TSF : mov = TSF(source, maLength);
Case HMA : mov = HullMovingAvg(source, maLength);
Default : mov = ExpAverage(source, maLength);
}
#Supertrend(lenAtr, lenMas, mult, highP, lowP, closeP) =>
Script Supertrend {
input src = hl2;
input atrLength = 14;
input mult = 2;
input changeATR = yes;
    def tr = TrueRange(high, close, low);
    def nATR = if changeATR then ATR(Length = atrLength) else SimpleMovingAvg(tr, atrLength);
    def up = src - mult * nATR;
    def dn = src + mult * nATR;
    def lower;
    def upper;
    def up2 = if isNaN(upper[1]) then up else upper[1];
    def dn2 = if isNaN(lower[1]) then dn else lower[1];
    def up1 = if up2==0 then up else up2;
    def dn1 = if dn2==0 then dn else dn2;
        upper = if (close[1] > up1) then if up > up1 then up else up1 else up;
        lower = if (close[1] < dn1) then if dn < dn1 then dn else dn1 else dn;
    def trend;
    def trend2 = if isNaN(trend[1]) then 0 else trend[1];
    def trend1 = if trend2==0 then 1 else trend2;
    if trend1 == -1 and close > dn1 {
        trend = 1;
    } else if trend1 == 1 and close < up1 {
        trend = -1;
    } else {
        trend = trend1;
    }
    def superTrend = if trend == -1 then lower else upper;

    plot ST = superTrend;
    plot dir = trend;
}

def ma = mov;
def ST    = Supertrend(ma, atrLength, atrMultiplier,changeATR).ST;
def Trend = Supertrend(ma, atrLength, atrMultiplier,changeATR).DIR;
def change = Trend!=Trend[1];
def buySignal  = trend > 0 and change;
def sellSignal = trend < 0 and change;

#-- plts

plot upPlot = if Trend > 0 then ST else na;  # 'Up Band'
plot loPlot = if Trend < 0 then ST else na; # 'Low Band'
upPlot.SetDefaultColor(CreateColor(76,175,80));
loPlot.SetDefaultColor(CreateColor(255,82,82));

plot upPt = if buySignal then ST else na;
plot dnPt = if sellSignal then ST else na;
upPt.SetLineWeight(2);
dnPt.SetLineWeight(2);
upPt.SetDefaultColor(CreateColor(76,175,80));
dnPt.SetDefaultColor(CreateColor(255,82,82));
upPt.setPaintingStrategy(PaintingStrategy.POINTS);
dnPt.setPaintingStrategy(PaintingStrategy.POINTS);

#-- Signals and Cloud

AddChartBubble(showsignals and buySignal, ST, "B", Color.GREEN, no);
AddChartBubble(showsignals and sellSignal, ST, "S", Color.RED);

AddCloud(if ShowCloud then avPlot else na, upPlot, CreateColor(0,35,0)); # 'Upper Area'
AddCloud(if ShowCloud then loPlot else na, avPlot, CreateColor(55,0,0));  # 'Lower Area'

#--- END CODE
 
Last edited:
check the below

CSS:
# https://www.tradingview.com/v/sfV6H5h5/
#//@KivancOzbilgic
#indicator('SuperTrended Moving Averages', 'ST MA', overlay=true,
#Converted by Sam4Cok@Samer800    - 11/2023

input maType  = {SMA,default EMA, SMMA, WMA, VWMA,DEMA, TEMA, ZLSMA, ZLDEMA, ZLTEMA, McGinley, HMA};
input sourceType = {"High/Low",Default "Custom"};
input customSource = FundamentalType.HL2;
input maLength  = 100;    # " Length MA"
input changeATR = yes;
input atrLength = 10;    # " Length Atr"
input atrMultiplier = 0.5;   # "Band Mult"
input ShowCloud = yes;
input showsignals = yes;


def na = Double.NaN;
def avPlot = ohlc4;
def custom = sourceType==sourceType."Custom";
def src = Fundamental(FundamentalType = customSource);

#// ] -------------- FUNCTIONS : Moving Avg ------------------ [
#export zlSma(float src, simple int len) =>
script zlSma {
    input src = close;
    input len = 14;
    def lsma = Inertia(src, len);
    def lsma2 = Inertia(lsma, len);
    def eq = lsma - lsma2;
    def zlsma = lsma + eq;
    plot return = zlsma;
}

#export zlDema(float src, simple int len) =>
script zlDema {
    input src = close;
    input len = 14;
    def zdema1 = ExpAverage(src, len);
    def zdema2 = ExpAverage(zdema1, len);
    def dema1 = 2 * zdema1 - zdema2;
    def zdema12 = ExpAverage(dema1, len);
    def zdema22 = ExpAverage(zdema12, len);
    def zldema = 2 * zdema12 - zdema22;
    plot return = zldema;
}
#export zlTema(float src, simple int len) =>
script zlTema {
    input src = close;
    input len = 14;
    def ema1 = ExpAverage(src, len);
    def ema2 = ExpAverage(ema1, len);
    def ema3 = ExpAverage(ema2, len);
    def tema1 = 3 * (ema1 - ema2) + ema3;
    def ema1a = ExpAverage(tema1, len);
    def ema2a = ExpAverage(ema1a, len);
    def ema3a = ExpAverage(ema2a, len);
    def zltema = 3 * (ema1a - ema2a) + ema3a;
    plot return = zltema;
}
#export mcginley(float src, simple int len)=>
script mcginley {
    input src = close;
    input len = 14;
    def mg;
    def t = ExpAverage(src, len);
    mg = if IsNaN(mg[1]) then t else
        CompoundValue(1 ,mg[1] + (src - mg[1]) / (len * Power(src / mg[1], 4)),src);
    plot return = mg;
}
#vwma(source, length)
script VWMA {
    input src = close;
    input len = 15;
    def v = volume;
    def VWMA = SimpleMovingAvg(src * v, len) / SimpleMovingAvg(v, len);
    plot result = VWMA;
}
#export multiMa(float source, simple int length, string type) =>
script multiMa {
    input source = close;
    input length = 14;
    input type = "SMA";
    def multiMa =
        if type == "SMA"    then SimpleMovingAvg(source, length) else
        if type == "EMA"    then ExpAverage(source, length) else
        if type == "SMMA"   then WildersAverage(source, length) else
        if type == "WMA"    then WMA(source, length) else
        if type == "VWMA"   then vwma(source, length) else
        if type == "DEMA"   then DEMA(source, length) else
        if type == "TEMA"   then TEMA(source, length) else
        if type == "ZLSMA"  then zlSma(source, length) else
        if type == "ZLDEMA" then zlDema(source, length) else
        if type == "ZLTEMA" then zlTema(source, length) else
        if type == "McGinley" then mcginley(source, length) else
        if type == "HMA"    then  HullMovingAvg(source, length ) else ExpAverage(source, length);
    plot return = multiMa;
}
#Supertrend(lenAtr, lenMas, mult, highP, lowP, closeP) =>
Script Supertrend {
input maUp = high;
input maDn = low;
input atrLength = 14;
input mult = 2;
input changeATR = yes;
    def tr = TrueRange(high, close, low);
    def nATR = if changeATR then ATR(Length = atrLength) else SimpleMovingAvg(tr, atrLength);
    def up = maDn - mult * nATR;
    def dn = maUp + mult * nATR;
    def lower;
    def upper;
    def up1 = if upper[1] then upper[1] else up;
    def dn1 = if lower[1] then lower[1] else dn;
        upper = if (close[1] > up1) then max(up, up1) else up;#  else up1;
        lower = if (close[1] < dn1) then min(dn, dn1) else dn;# else dn1;
    def trend;# = na
    if (!upper or !lower) {
        trend = 1;
    } else
    if trend[1] == -1 and close > dn1 {
        trend = 1;
    } else if trend[1] == 1 and close < up1 {
        trend = -1;
    } else {
        trend = if trend[1] then trend[1] else 1;
    }
    def superTrend = if trend == -1 then lower else upper;

    plot ST = superTrend;
    plot dir = trend;
}
def maUp = multiMa(if custom then src else high, maLength, maType);
def maDn = multiMa(if custom then src else low, maLength, maType);

def ST    = Supertrend(maUp, maDn, atrLength, atrMultiplier,changeATR).ST;
def Trend = Supertrend(maUp, maDn, atrLength, atrMultiplier,changeATR).DIR;
def buySignal = trend == 1 and trend[1] == -1;
def sellSignal = trend == -1 and trend[1] == 1;

#-- plts

plot upPlot = if Trend > 0 then ST else na;  # 'Up Band'
upPlot.SetDefaultColor(CreateColor(76,175,80));
plot loPlot = if Trend > 0 then na else ST; # 'Low Band'
loPlot.SetDefaultColor(CreateColor(255,82,82));

#-- Signals and Cloud

AddChartBubble(showsignals and buySignal, ST, "B", Color.GREEN, no);
AddChartBubble(showsignals and sellSignal, ST, "S", Color.RED);

AddCloud(if ShowCloud then avPlot else na, upPlot, CreateColor(0,35,0)); # 'Upper Area'
AddCloud(if ShowCloud then loPlot else na, avPlot, CreateColor(55,0,0));  # 'Lower Area'

#--- END CODE
@samer800,

Thank you for the wonderful work, you are amazing person and thanks for helping us.
It works great in chart but i am not able to use this indicator for scanner, I am getting following error, could you please help this indicator to work for scanner.

Thanks in advance.

 
@samer800,

Thank you for the wonderful work, you are amazing person and thanks for helping us.
It works great in chart but i am not able to use this indicator for scanner, I am getting following error, could you please help this indicator to work for scanner.

Thanks in advance.

try the below for scanner. remove/add the "#" at the end of the code to search for buy/sell signals and adjust number of bars as desire.

CSS:
# https://www.tradingview.com/v/sfV6H5h5/
#//@KivancOzbilgic
#indicator('SuperTrended Moving Averages', 'ST MA', overlay=true,
#Converted by Sam4Cok@Samer800    - 11/2023
# Scanner by Sam4Cok@Samer800    - 11/2023
input maType  = AverageType.EXPONENTIAL;;
input sourceType = {"High/Low",Default "Custom"};
input customSource = FundamentalType.HL2;
input maLength  = 100;    # " Length MA"
input changeATR = yes;
input atrLength = 10;    # " Length Atr"
input atrMultiplier = 0.5;   # "Band Mult"

def na = Double.NaN;
def avPlot = ohlc4;
def custom = sourceType==sourceType."Custom";
def src = Fundamental(FundamentalType = customSource);

#// ] -------------- FUNCTIONS : Moving Avg ------------------ [

#Supertrend(lenAtr, lenMas, mult, highP, lowP, closeP) =>
def maUp = MovingAverage(maType, if custom then src else high, maLength);
def maDn = MovingAverage(maType, if custom then src else low, maLength);

    def tr = TrueRange(high, close, low);
    def nATR = if changeATR then ATR(Length = atrLength) else SimpleMovingAvg(tr, atrLength);
    def up = maDn - atrMultiplier * nATR;
    def dn = maUp + atrMultiplier * nATR;
    def lower;
    def upper;
    def up1 = if upper[1] then upper[1] else up;
    def dn1 = if lower[1] then lower[1] else dn;
        upper = if (close[1] > up1) then max(up, up1) else up;#  else up1;
        lower = if (close[1] < dn1) then min(dn, dn1) else dn;# else dn1;
    def dir;# = na
    if (!upper or !lower) {
        dir = 1;
    } else
    if dir[1] == -1 and close > dn1 {
        dir = 1;
    } else if dir[1] == 1 and close < up1 {
        dir = -1;
    } else {
        dir = if dir[1] then dir[1] else 1;
    }
    def superTrend = if dir == -1 then lower else upper;

    def ST = superTrend;
    def trend = dir;


def buySignal = trend == 1 and trend[1] == -1;
def sellSignal = trend == -1 and trend[1] == 1;

#-- Signals
plot scanBuy = buySignal within 5 bars;
#plot scanSell = sellSignal within 5 bars;

#--- END CODE
 
Thank you, @samer800 , for this script

For some reason, I am not able to get this to display on the weekly chart (put two years and 3 years time-frame to get MA length). I see this working on TV.

The tickers I tested are TAL, MARA

Any help?
 
Thank you, @samer800 , for this script

For some reason, I am not able to get this to display on the weekly chart (put two years and 3 years time-frame to get MA length). I see this working on TV.

The tickers I tested are TAL, MARA

Any help?
updated the original code to fix the issue. check it.

CSS:
# https://www.tradingview.com/v/sfV6H5h5/
#//@KivancOzbilgic
#indicator('SuperTrended Moving Averages', 'ST MA', overlay=true,
#Converted by Sam4Cok@Samer800    - 11/2023
#- Updated - minor code fix - Sam4Cok@Samer800    - 01/2024
input maType  = {SMA, default EMA, HMA, WMA, VWMA, DEMA, ZLSMA, ZLEMA, VAR, TILL, TSF};
input Source = close;
input maLength  = 100;    # " Length MA"
input changeATR = yes;
input atrLength = 10;    # " Length Atr"
input atrMultiplier = 0.5;   # "Band Mult"
input ShowCloud = yes;
input showsignals = no;

def na = Double.NaN;
def avPlot = ohlc4;

#// ] -------------- FUNCTIONS : Moving Avg ------------------ [
#pine_linreg(src, len, offset=0) =>
Script linreg {
input src = close;
input len = 100;
input offset = 0;
    def na = Double.NaN;
    def bar_index = isNaN(close);
    def x_sum = if bar_index then na else
                fold i = 0 to len with p do
                 p + i;
    def xx_sum = if bar_index then na else
                fold ii = 0 to len with pp do
                 pp + ii * ii;
    def y_sum = sum(src, len);
    def xy_sum = fold j = 0 to len with q do
                  q  + j * GetValue(src, len - j - 1);
    def slope = (len * xy_sum - x_sum * y_sum) / (len * xx_sum - x_sum * x_sum);
    def intercept = (y_sum - slope * x_sum) / len;
    def linreg = intercept + slope * (len - offset - 1);
    plot out = linreg;
}
Script TSF {
input src = close;
input length = 100;
    def lrc = Inertia(src, length);
    def lrc1 = linreg(src, length, 1);
    def lrs = lrc - lrc1;
    def TSF = Inertia(src, length) + lrs;
    plot out = TSF;
}
script TILL {
    input src = close;
    input length = 100;
    input T3a1 = 0.7;
    def T3e1 = ExpAverage(src, length);
    def T3e2 = ExpAverage(T3e1, length);
    def T3e3 = ExpAverage(T3e2, length);
    def T3e4 = ExpAverage(T3e3, length);
    def T3e5 = ExpAverage(T3e4, length);
    def T3e6 = ExpAverage(T3e5, length);
    def T3c1 = -T3a1 * T3a1 * T3a1;
    def T3c2 = 3 * T3a1 * T3a1 + 3 * T3a1 * T3a1 * T3a1;
    def T3c3 = -6 * T3a1 * T3a1 - 3 * T3a1 - 3 * T3a1 * T3a1 * T3a1;
    def T3c4 = 1 + 3 * T3a1 + T3a1 * T3a1 * T3a1 + 3 * T3a1 * T3a1;
    def T3 = T3c1 * T3e6 + T3c2 * T3e5 + T3c3 * T3e4 + T3c4 * T3e3;
    plot out = T3;
}
script VAR {
    input src = close;
    input length = 100;
    def curClose = src;
    def prevClose = src[1];
    def valpha = 2 / (length + 1);
    def vud1 = if curClose > prevClose then curClose - prevClose else 0;
    def vdd1 = if prevClose > curClose then prevClose - curClose else 0;
    def vUD = sum(vud1, 9);
    def vDD = sum(vdd1, 9);
    def vCMO = if vUD + vDD == 0 then 0 else (vUD - vDD) / (vUD + vDD);
    def cmo = valpha * AbsValue(vCMO);
    def VAR = CompoundValue(1, cmo * curClose + (1 - cmo) * VAR[1], src);
    plot out = VAR;
}
#export zlSma(float src, simple int len) =>
script ZLSMA {
    input src = close;
    input len = 14;
    def lsma = Inertia(src, len);
    def lsma2 = Inertia(lsma, len);
    def eq = lsma - lsma2;
    def zlsma = lsma + eq;
    plot return = zlsma;
}
#export zlSma(float src, simple int len) =>
script ZLEMA {
    input src = close;
    input len = 14;
    def lenR = Round(len / 2, 0);
    def zxLag = if len / 2 == lenR then len / 2 else (len - 1) / 2;
    def zxEMAData = src + src - src[zxLag];
    def ZLEMA = ExpAverage(zxEMAData, len);
    plot out = ZLEMA;
}
#vwma(source, length)
script VWMA {
    input src = close;
    input len = 15;
    def v = volume;
    def VWMA = SimpleMovingAvg(src * v, len) / SimpleMovingAvg(v, len);
    plot result = VWMA;
}
#export multiMa(float source, simple int length, string type) =>
def mov;
Switch (maType) {
Case SMA : mov = Average(source, maLength);
Case VAR : mov = VAR(source, maLength);
Case WMA : mov = WMA(source, maLength);
Case VWMA : mov = VWMA(source, maLength);
Case DEMA : mov = DEMA(source, maLength);
Case ZLSMA : mov = ZLSMA(source, maLength);
Case ZLEMA : mov = ZLEMA(source, maLength);
Case TILL : mov = TILL(source, maLength);
Case TSF : mov = TSF(source, maLength);
Case HMA : mov = HullMovingAvg(source, maLength);
Default : mov = ExpAverage(source, maLength);
}
#Supertrend(lenAtr, lenMas, mult, highP, lowP, closeP) =>
Script Supertrend {
input src = hl2;
input atrLength = 14;
input mult = 2;
input changeATR = yes;
    def tr = TrueRange(high, close, low);
    def nATR = if changeATR then ATR(Length = atrLength) else SimpleMovingAvg(tr, atrLength);
    def up = src - mult * nATR;
    def dn = src + mult * nATR;
    def lower;
    def upper;
    def up2 = if isNaN(upper[1]) then up else upper[1];
    def dn2 = if isNaN(lower[1]) then dn else lower[1];
    def up1 = if up2==0 then up else up2;
    def dn1 = if dn2==0 then dn else dn2;
        upper = if (close[1] > up1) then if up > up1 then up else up1 else up;
        lower = if (close[1] < dn1) then if dn < dn1 then dn else dn1 else dn;
    def trend;
    def trend2 = if isNaN(trend[1]) then 0 else trend[1];
    def trend1 = if trend2==0 then 1 else trend2;
    if trend1 == -1 and close > dn1 {
        trend = 1;
    } else if trend1 == 1 and close < up1 {
        trend = -1;
    } else {
        trend = trend1;
    }
    def superTrend = if trend == -1 then lower else upper;

    plot ST = superTrend;
    plot dir = trend;
}

def ma = mov;
def ST    = Supertrend(ma, atrLength, atrMultiplier,changeATR).ST;
def Trend = Supertrend(ma, atrLength, atrMultiplier,changeATR).DIR;
def change = Trend!=Trend[1];
def buySignal  = trend > 0 and change;
def sellSignal = trend < 0 and change;

#-- plts

plot upPlot = if Trend > 0 then ST else na;  # 'Up Band'
plot loPlot = if Trend < 0 then ST else na; # 'Low Band'
upPlot.SetDefaultColor(CreateColor(76,175,80));
loPlot.SetDefaultColor(CreateColor(255,82,82));

plot upPt = if buySignal then ST else na;
plot dnPt = if sellSignal then ST else na;
upPt.SetLineWeight(2);
dnPt.SetLineWeight(2);
upPt.SetDefaultColor(CreateColor(76,175,80));
dnPt.SetDefaultColor(CreateColor(255,82,82));
upPt.setPaintingStrategy(PaintingStrategy.POINTS);
dnPt.setPaintingStrategy(PaintingStrategy.POINTS);

#-- Signals and Cloud

AddChartBubble(showsignals and buySignal, ST, "B", Color.GREEN, no);
AddChartBubble(showsignals and sellSignal, ST, "S", Color.RED);

AddCloud(if ShowCloud then avPlot else na, upPlot, CreateColor(0,35,0)); # 'Upper Area'
AddCloud(if ShowCloud then loPlot else na, avPlot, CreateColor(55,0,0));  # 'Lower Area'

#--- END CODE
 
Good Morning,

Great work @samer800 I like the indicator after testing it. Is there anyway to turn this into a scanner?

Also @samer800 does the indicator repaint?
The scan is found: https://usethinkscript.com/threads/supertrended-moving-averages-for-thinkorswim.17207/#post-134682

Repainting?
Moving average indicators generally don't repaint. Indicators on the forum that do not have a "repaints" prefix generally don't repaint.

Almost all the indicators on the forum will have the current candle update every tick until the candle is closed. This is because there is no way to predict the current candle close until it happens.
 
try the below for scanner. remove/add the "#" at the end of the code to search for buy/sell signals and adjust number of bars as desire.

CSS:
# https://www.tradingview.com/v/sfV6H5h5/
#//@KivancOzbilgic
#indicator('SuperTrended Moving Averages', 'ST MA', overlay=true,
#Converted by Sam4Cok@Samer800    - 11/2023
# Scanner by Sam4Cok@Samer800    - 11/2023
input maType  = AverageType.EXPONENTIAL;;
input sourceType = {"High/Low",Default "Custom"};
input customSource = FundamentalType.HL2;
input maLength  = 100;    # " Length MA"
input changeATR = yes;
input atrLength = 10;    # " Length Atr"
input atrMultiplier = 0.5;   # "Band Mult"

def na = Double.NaN;
def avPlot = ohlc4;
def custom = sourceType==sourceType."Custom";
def src = Fundamental(FundamentalType = customSource);

#// ] -------------- FUNCTIONS : Moving Avg ------------------ [

#Supertrend(lenAtr, lenMas, mult, highP, lowP, closeP) =>
def maUp = MovingAverage(maType, if custom then src else high, maLength);
def maDn = MovingAverage(maType, if custom then src else low, maLength);

    def tr = TrueRange(high, close, low);
    def nATR = if changeATR then ATR(Length = atrLength) else SimpleMovingAvg(tr, atrLength);
    def up = maDn - atrMultiplier * nATR;
    def dn = maUp + atrMultiplier * nATR;
    def lower;
    def upper;
    def up1 = if upper[1] then upper[1] else up;
    def dn1 = if lower[1] then lower[1] else dn;
        upper = if (close[1] > up1) then max(up, up1) else up;#  else up1;
        lower = if (close[1] < dn1) then min(dn, dn1) else dn;# else dn1;
    def dir;# = na
    if (!upper or !lower) {
        dir = 1;
    } else
    if dir[1] == -1 and close > dn1 {
        dir = 1;
    } else if dir[1] == 1 and close < up1 {
        dir = -1;
    } else {
        dir = if dir[1] then dir[1] else 1;
    }
    def superTrend = if dir == -1 then lower else upper;

    def ST = superTrend;
    def trend = dir;


def buySignal = trend == 1 and trend[1] == -1;
def sellSignal = trend == -1 and trend[1] == 1;

#-- Signals
plot scanBuy = buySignal within 5 bars;
#plot scanSell = sellSignal within 5 bars;

#--- END CODE
Hi @samer800. Thanks for this.

I'm not sure why I can make the scanner version work. Can you verify, please.

Though if you can make it add a bubble (and a scanner) whenever Close crosses above the last loPlot value.

Thanks in advanced.
 
The scan is found: https://usethinkscript.com/threads/supertrended-moving-averages-for-thinkorswim.17207/#post-134682

Repainting?
Moving average indicators generally don't repaint. Indicators on the forum that do not have a "repaints" prefix generally don't repaint.

Almost all the indicators on the forum will have the current candle update every tick until the candle is closed. This is because there is no way to predict the current candle close until it happens.
This is perfect, thanks @MerryDay!
 
I tried creating a MTF version. 4 hr default. Lines look like good support/resistance levels, but may not be exact to the default time.

Code:
# https://www.tradingview.com/v/sfV6H5h5/
#//@KivancOzbilgic
#indicator('SuperTrended Moving Averages', 'ST MA', overlay=true,
#Converted by Sam4Cok@Samer800    - 11/2023
#- Updated - minor code fix - Sam4Cok@Samer800    - 01/2024
input maType  = {SMA, default EMA, HMA, WMA, VWMA, DEMA, ZLSMA, ZLEMA, VAR, TILL, TSF};
input agg = aggregationPeriod.four_hours;
def Source = close(period = agg);
input maLength  = 135;    #100 " Length MA"
input changeATR = yes;
input atrLength = 10;    # " Length Atr"
input atrMultiplier = 0.5;   # "Band Mult"
input ShowCloud = no;
input showsignals = no;

def na = Double.NaN;
def avPlot = ohlc4;

#// ] -------------- FUNCTIONS : Moving Avg ------------------ [
#pine_linreg(src, len, offset=0) =>
Script linreg {
input src = close;
input len = 100;
input offset = 0;
    def na = Double.NaN;
    def bar_index = isNaN(close);
    def x_sum = if bar_index then na else
                fold i = 0 to len with p do
                 p + i;
    def xx_sum = if bar_index then na else
                fold ii = 0 to len with pp do
                 pp + ii * ii;
    def y_sum = sum(src, len);
    def xy_sum = fold j = 0 to len with q do
                  q  + j * GetValue(src, len - j - 1);
    def slope = (len * xy_sum - x_sum * y_sum) / (len * xx_sum - x_sum * x_sum);
    def intercept = (y_sum - slope * x_sum) / len;
    def linreg = intercept + slope * (len - offset - 1);
    plot out = linreg;
}
Script TSF {
input src = close;
input length = 100;
    def lrc = Inertia(src, length);
    def lrc1 = linreg(src, length, 1);
    def lrs = lrc - lrc1;
    def TSF = Inertia(src, length) + lrs;
    plot out = TSF;
}
script TILL {
    input src = close;
    input length = 100;
    input T3a1 = 0.7;
    def T3e1 = ExpAverage(src, length);
    def T3e2 = ExpAverage(T3e1, length);
    def T3e3 = ExpAverage(T3e2, length);
    def T3e4 = ExpAverage(T3e3, length);
    def T3e5 = ExpAverage(T3e4, length);
    def T3e6 = ExpAverage(T3e5, length);
    def T3c1 = -T3a1 * T3a1 * T3a1;
    def T3c2 = 3 * T3a1 * T3a1 + 3 * T3a1 * T3a1 * T3a1;
    def T3c3 = -6 * T3a1 * T3a1 - 3 * T3a1 - 3 * T3a1 * T3a1 * T3a1;
    def T3c4 = 1 + 3 * T3a1 + T3a1 * T3a1 * T3a1 + 3 * T3a1 * T3a1;
    def T3 = T3c1 * T3e6 + T3c2 * T3e5 + T3c3 * T3e4 + T3c4 * T3e3;
    plot out = T3;
}
script VAR {
    input src = close;
    input length = 100;
    def curClose = src;
    def prevClose = src[1];
    def valpha = 2 / (length + 1);
    def vud1 = if curClose > prevClose then curClose - prevClose else 0;
    def vdd1 = if prevClose > curClose then prevClose - curClose else 0;
    def vUD = sum(vud1, 9);
    def vDD = sum(vdd1, 9);
    def vCMO = if vUD + vDD == 0 then 0 else (vUD - vDD) / (vUD + vDD);
    def cmo = valpha * AbsValue(vCMO);
    def VAR = CompoundValue(1, cmo * curClose + (1 - cmo) * VAR[1], src);
    plot out = VAR;
}
#export zlSma(float src, simple int len) =>
script ZLSMA {
    input src = close;
    input len = 14;
    def lsma = Inertia(src, len);
    def lsma2 = Inertia(lsma, len);
    def eq = lsma - lsma2;
    def zlsma = lsma + eq;
    plot return = zlsma;
}
#export zlSma(float src, simple int len) =>
script ZLEMA {
    input src = close;
    input len = 14;
    def lenR = Round(len / 2, 0);
    def zxLag = if len / 2 == lenR then len / 2 else (len - 1) / 2;
    def zxEMAData = src + src - src[zxLag];
    def ZLEMA = ExpAverage(zxEMAData, len);
    plot out = ZLEMA;
}
#vwma(source, length)
script VWMA {
    input src = close;
    input len = 15;
    def v = volume;
    def VWMA = SimpleMovingAvg(src * v, len) / SimpleMovingAvg(v, len);
    plot result = VWMA;
}
#export multiMa(float source, simple int length, string type) =>
def mov;
Switch (maType) {
Case SMA : mov = Average(source, maLength);
Case VAR : mov = VAR(source, maLength);
Case WMA : mov = WMA(source, maLength);
Case VWMA : mov = VWMA(source, maLength);
Case DEMA : mov = DEMA(source, maLength);
Case ZLSMA : mov = ZLSMA(source, maLength);
Case ZLEMA : mov = ZLEMA(source, maLength);
Case TILL : mov = TILL(source, maLength);
Case TSF : mov = TSF(source, maLength);
Case HMA : mov = HullMovingAvg(source, maLength);
Default : mov = ExpAverage(source, maLength);
}
#Supertrend(lenAtr, lenMas, mult, highP, lowP, closeP) =>
Script Supertrend {
input src = hl2;
input atrLength = 14;
input mult = 2;
input changeATR = yes;
    def tr = TrueRange(high, close, low);
    def nATR = if changeATR then ATR(Length = atrLength) else SimpleMovingAvg(tr, atrLength);
    def up = src - mult * nATR;
    def dn = src + mult * nATR;
    def lower;
    def upper;
    def up2 = if isNaN(upper[1]) then up else upper[1];
    def dn2 = if isNaN(lower[1]) then dn else lower[1];
    def up1 = if up2==0 then up else up2;
    def dn1 = if dn2==0 then dn else dn2;
        upper = if (close[1] > up1) then if up > up1 then up else up1 else up;
        lower = if (close[1] < dn1) then if dn < dn1 then dn else dn1 else dn;
    def trend;
    def trend2 = if isNaN(trend[1]) then 0 else trend[1];
    def trend1 = if trend2==0 then 1 else trend2;
    if trend1 == -1 and close > dn1 {
        trend = 1;
    } else if trend1 == 1 and close < up1 {
        trend = -1;
    } else {
        trend = trend1;
    }
    def superTrend = if trend == -1 then lower else upper;

    plot ST = superTrend;
    plot dir = trend;
}

def ma = mov;
def ST    = Supertrend(ma, atrLength, atrMultiplier,changeATR).ST;
def Trend = Supertrend(ma, atrLength, atrMultiplier,changeATR).DIR;
def change = Trend!=Trend[1];
def buySignal  = trend > 0 and change;
def sellSignal = trend < 0 and change;

#-- plts

plot upPlot = if Trend > 0 then ST else na;  # 'Up Band'
plot loPlot = if Trend < 0 then ST else na; # 'Low Band'
#upPlot.SetDefaultColor(CreateColor(76,175,80));
upPlot.SetDefaultColor(Color.cyan);
#loPlot.SetDefaultColor(CreateColor(255,82,82));
loPlot.SetDefaultColor(Color.magenta);
upPlot.SetLineWeight(3);
loPlot.SetLineWeight(3);
plot upPt = if buySignal then ST else na;
plot dnPt = if sellSignal then ST else na;
upPt.SetLineWeight(2);
dnPt.SetLineWeight(2);
upPt.SetDefaultColor(CreateColor(76,175,80));
dnPt.SetDefaultColor(CreateColor(255,82,82));
upPt.setPaintingStrategy(PaintingStrategy.POINTS);
dnPt.setPaintingStrategy(PaintingStrategy.POINTS);

#-- Signals and Cloud

AddChartBubble(showsignals and buySignal, ST, "B", Color.GREEN, no);
AddChartBubble(showsignals and sellSignal, ST, "S", Color.RED);

AddCloud(if ShowCloud then avPlot else na, upPlot, CreateColor(0,35,0)); # 'Upper Area'
AddCloud(if ShowCloud then loPlot else na, avPlot, CreateColor(55,0,0));  # 'Lower Area'

#--- END CODE
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
406 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