Double SuperTrend ATR for ThinkorSwim

petergluis

Active member
I tried to convert Double SuperTrend ATR for ThinkorSwim. There is a minor issue with converting nz(Trend[1],1). Your help will be greatly appreciated.
https://www.tradingview.com/script/OsKg8IXR-Double-SuperTrend-ATR/
Ruby:
def Factor = 3;
def ATR = 12;
script nz {
    input data = 0;
    def ret_val = if IsNaN(data) then 0 else data;
    plot return = ret_val;
}
script MTF_ATR {
    input Length = 12;
    input TF = AggregationPeriod.HOUR;
    input averageType = AverageType.WILDERS;
    plot ATR =
        MovingAverage(
            averageType,
            TrueRange(
                high(period = TF),
                close(period = TF),
                low(period = TF)
            ),
            Length
        )
    ;
}
def Up = hl2 - (Factor * ATR(ATR));
def Dn = hl2 + (Factor * ATR(ATR));

def TUp = if close[1] > TUp[1] then Max(Up, TUp[1]) else Up;
def TDown = if close[1] < TDown[1] then Min(Dn, TDown[1]) else Dn;

def Trend = if close > TDown[1] then 1 else if close < TUp[1] then -1 else nz(Trend[1]);

def Tsl1 = if Trend == 1 then TUp else TDown;
def Tsl2 = if Trend == 1 then TDown else TUp;

plot p1 = Tsl1;
plot p2 = Tsl2;
AddCloud(p1,   p2,   Color.RED,   Color.GREEN);
 

Svanoy

Active member
@petergluis
There is another thread by @spartan.ts asking about how to handle the nz function, he figured out putting two inputs in the nz script worked.
Ruby:
def Factor = 3;
def ATR = 12;

script nz {
input data = 0;
input data2 = 0;
def ret_val = if IsNaN(data) then data2 else data;
plot return = ret_val;
}

script MTF_ATR {
    input Length = 12;
    input TF = AggregationPeriod.HOUR;
    input averageType = AverageType.WILDERS;
    plot ATR =
        MovingAverage(
            averageType,
            TrueRange(
                high(period = TF),
                close(period = TF),
                low(period = TF)
            ),
            Length
        )
    ;
}
def Up = hl2 - (Factor * ATR(ATR));
def Dn = hl2 + (Factor * ATR(ATR));

def TUp = if close[1] > TUp[1] then Max(Up, TUp[1]) else Up;
def TDown = if close[1] < TDown[1] then Min(Dn, TDown[1]) else Dn;

def Trend = if close > TDown[1] then 1 else if close < TUp[1] then -1 else nz(Trend[1],1);

def Tsl1 = if Trend == 1 then TUp else TDown;
def Tsl2 = if Trend == 1 then TDown else TUp;

plot p1 = Tsl1;
plot p2 = Tsl2;
AddCloud(p1,   p2,   Color.RED,   Color.GREEN);
 

petergluis

Active member
@petergluis
There is another thread by @spartan.ts asking about how to handle the nz function, he figured out putting two inputs in the nz script worked.
Ruby:
def Factor = 3;
def ATR = 12;

script nz {
input data = 0;
input data2 = 0;
def ret_val = if IsNaN(data) then data2 else data;
plot return = ret_val;
}

script MTF_ATR {
    input Length = 12;
    input TF = AggregationPeriod.HOUR;
    input averageType = AverageType.WILDERS;
    plot ATR =
        MovingAverage(
            averageType,
            TrueRange(
                high(period = TF),
                close(period = TF),
                low(period = TF)
            ),
            Length
        )
    ;
}
def Up = hl2 - (Factor * ATR(ATR));
def Dn = hl2 + (Factor * ATR(ATR));

def TUp = if close[1] > TUp[1] then Max(Up, TUp[1]) else Up;
def TDown = if close[1] < TDown[1] then Min(Dn, TDown[1]) else Dn;

def Trend = if close > TDown[1] then 1 else if close < TUp[1] then -1 else nz(Trend[1],1);

def Tsl1 = if Trend == 1 then TUp else TDown;
def Tsl2 = if Trend == 1 then TDown else TUp;

plot p1 = Tsl1;
plot p2 = Tsl2;
AddCloud(p1,   p2,   Color.RED,   Color.GREEN);
Svanoy, thank you very much for your help. I add band border color changes with Variable Moving Average to follow the trend changes.

Ruby:
#https://www.tradingview.com/script/6Ix0E5Yr-Variable-Moving-Average-LazyBear/
def src = close;
def l = 6;
def std = no;
def bc = no;
def k = 1.0 / l;
def Factor = 3;
def ATR = 12;

script nz {
    input data = 0;
    def ret_val = if IsNaN(data) then 0 else data;
    plot return = ret_val;
}
def pdm = Max((src - src[1]), 0);
def mdm = Max((src[1] - src), 0);
def pdmS = ((1 - k) * nz(pdmS[1]) + k * pdm);
def mdmS = ((1 - k) * nz(mdmS[1]) + k * mdm);
def s = pdmS + mdmS;
def pdi = pdmS / s;
def mdi = mdmS / s;
def pdiS = ((1 - k) * nz(pdiS[1]) + k * pdi);
def mdiS = ((1 - k) * nz(mdiS[1]) + k * mdi);
def d = AbsValue(pdiS - mdiS);
def s1 = pdiS + mdiS;
def iS = ((1 - k) * nz(iS[1]) + k * d / s1);
def hhv = Highest(iS, l);
def llv = Lowest(iS, l);
def d1 = hhv - llv;
def vI = (iS - llv) / d1;
def vma = (1 - k * vI) * nz(vma[1]) + k * vI * src;
plot line = vma;
line.AssignValueColor(if line> line[1] then Color.GREEN else if line <line[1] then Color.MAGENTA else color.gray);
line.SetLineWeight(4);

script nz {
input data = 0;
input data2 = 0;
def ret_val = if IsNaN(data) then data2 else data;
plot return = ret_val;
}

script MTF_ATR {
    input Length = 12;
    input TF = AggregationPeriod.HOUR;
    input averageType = AverageType.WILDERS;
    plot ATR =
        MovingAverage(
            averageType,
            TrueRange(
                high(period = TF),
                close(period = TF),
                low(period = TF)
            ),
            Length
        )
    ;
}
def Up = hl2 - (Factor * ATR(ATR));
def Dn = hl2 + (Factor * ATR(ATR));

def TUp = if close[1] > TUp[1] then Max(Up, TUp[1]) else Up;
def TDown = if close[1] < TDown[1] then Min(Dn, TDown[1]) else Dn;

def Trend = if close > TDown[1] then 1 else if close < TUp[1] then -1 else nz(Trend[1],1);

def Tsl1 = if Trend == 1 then TUp else TDown;
def Tsl2 = if Trend == 1 then TDown else TUp;

plot p1 = Tsl1;
plot p2 = Tsl2;
p1.AssignValueColor(if p1 < p2  then Color.green else Color.magenta);
p1.SetLineWeight(1);
p2.AssignValueColor(if p1 < p2  then Color.green else Color.magenta);
p2.SetLineWeight(1);

AddCloud(p1,   p2,   Color.light_RED,   Color.light_GREEN);
 
Last edited:

petergluis

Active member
I add Ehlers Instantaneous Trendline to Double SuperTrend ATR.
https://www.tradingview.com/script/DaHLcICg-Ehlers-Instantaneous-Trend-LazyBear/

Ruby:
#https://www.tradingview.com/script/6Ix0E5Yr-Variable-Moving-Average-LazyBear/
#https://www.tradingview.com/script/DaHLcICg-Ehlers-Instantaneous-Trend-LazyBear/
def src = close;
def l = 6;
def std = no;
def bc = no;
def k = 1.0 / l;
def Factor = 3;
def ATR = 12;

script nz {
    input data = 0;
    def ret_val = if IsNaN(data) then 0 else data;
    plot return = ret_val;
}
def pdm = Max((src - src[1]), 0);
def mdm = Max((src[1] - src), 0);
def pdmS = ((1 - k) * nz(pdmS[1]) + k * pdm);
def mdmS = ((1 - k) * nz(mdmS[1]) + k * mdm);
def s = pdmS + mdmS;
def pdi = pdmS / s;
def mdi = mdmS / s;
def pdiS = ((1 - k) * nz(pdiS[1]) + k * pdi);
def mdiS = ((1 - k) * nz(mdiS[1]) + k * mdi);
def d = AbsValue(pdiS - mdiS);
def s1 = pdiS + mdiS;
def iS = ((1 - k) * nz(iS[1]) + k * d / s1);
def hhv = Highest(iS, l);
def llv = Lowest(iS, l);
def d1 = hhv - llv;
def vI = (iS - llv) / d1;
def vma = (1 - k * vI) * nz(vma[1]) + k * vI * src;
plot line = vma;
line.AssignValueColor(if line> line[1] then Color.GREEN else if line <line[1] then Color.MAGENTA else color.gray);
line.SetLineWeight(4);

script nz {
input data = 0;
input data2 = 0;
def ret_val = if IsNaN(data) then data2 else data;
plot return = ret_val;
}

def src1 = hl2;
def a = 0.07;
def fr = no;
def ebc = no;
def hr = no;

def it = (a - ((a * a) / 4.0)) * src1 + 0.5 * a * a * src1[1] - (a - 0.75 * a * a) * src1[2] + 2 * (1 - a ) * nz(it[1], ((src1 + 2 * src1[1] + src1[2]) / 4.0)) - (1 - a ) * (1 - a ) * nz(it[2], ((src1 + 2 * src1[1] + src1[2]) / 4.0));

def lag = 2.0 * it - nz(it[2]);
plot dl = if fr and hr == no then if it > lag then lag else it else Double.NaN;
plot itl = if hr then Double.NaN else it;
plot ll = if hr then Double.NaN else lag;
AddCloud (ll, itl, Color.light_green, Color.light_RED);

script MTF_ATR {
    input Length = 12;
    input TF = AggregationPeriod.HOUR;
    input averageType = AverageType.WILDERS;
    plot ATR =
        MovingAverage(
            averageType,
            TrueRange(
                high(period = TF),
                close(period = TF),
                low(period = TF)
            ),
            Length
        )
    ;
}
def Up = hl2 - (Factor * ATR(ATR));
def Dn = hl2 + (Factor * ATR(ATR));

def TUp = if close[1] > TUp[1] then Max(Up, TUp[1]) else Up;
def TDown = if close[1] < TDown[1] then Min(Dn, TDown[1]) else Dn;

def Trend = if close > TDown[1] then 1 else if close < TUp[1] then -1 else nz(Trend[1],1);

def Tsl1 = if Trend == 1 then TUp else TDown;
def Tsl2 = if Trend == 1 then TDown else TUp;

plot p1 = Tsl1;
plot p2 = Tsl2;
p1.AssignValueColor(if p1 < p2  then Color.green else Color.magenta);
p1.SetLineWeight(1);
p2.AssignValueColor(if p1 < p2  then Color.green else Color.magenta);
p2.SetLineWeight(1);

AddCloud(p1,   p2,   Color.light_RED,   Color.light_GREEN);
 

rip78

New member
@petergluis

I love your Double SuperTrend ATR study. Have to be honest by saying that I think there are much better studies to use with it rather than the Variable Moving Average and Ehlers Instantaneous Trendline. Hope this does not offend as I really do appreciate all of the contributions you have made on this site.

I made a few modifications to your study. Nothing fancy. Just added Seth Urion's Hull Moving Average Concavity and Turning Points, with alerts and price bar color. Also added to the chart an old Eric Purdy slingshot study that I made a few changes to years ago. Let me know what you think.

http://tos.mx/Wg3jbNi
 

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
mod note:
The advantage of Supertrend Indicators is that they have less lag than traditional moving average studies.
@mashume's Hull has a lag because it waits for future bars to close before it REPAINTS the perfect signal. For us, longer-term swing traders, the lag is not a barrier to using @mashume's excellent study. But for scalpers, day traders, and short-swingers, @mashume's Hull might not be an optimal choice.
 
Last edited:

petergluis

Active member
@petergluis

I love your Double SuperTrend ATR study. Have to be honest by saying that I think there are much better studies to use with it rather than the Variable Moving Average and Ehlers Instantaneous Trendline. Hope this does not offend as I really do appreciate all of the contributions you have made on this site.

I made a few modifications to your study. Nothing fancy. Just added Seth Urion's Hull Moving Average Concavity and Turning Points, with alerts and price bar color. Also added to the chart an old Eric Purdy slingshot study that I made a few changes to years ago. Let me know what you think.

http://tos.mx/Wg3jbNi
Rip78, thank you very much for sharing your setup. Wish you enjoy your successful trading.
 

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.
Top