Supertrend Channels [LuxAlgo] For ThinkOrSwim

Gmoyano81

New member
Someone could help me out to transform the Tradingview Supertrend Channels [LuxAlgo] to thinkorswim?
 
Last edited by a moderator:
Someone could help me out to transform this tradingview pinescript to thinkorswim?

// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © LuxAlgo
//@version=5
indicator("Supertrend Channels [LuxAlgo]",overlay=true,max_lines_count=500)
length = input(14)
mult = input(2)
//------------------------------------------------------------------------------
upper = 0.,lower = 0.,os = 0,max = 0.,min = 0.
src = close
atr = ta.atr(length)*mult
up = hl2 + atr
dn = hl2 - atr
upper := src[1] < upper[1] ? math.min(up,upper[1]) : up
lower := src[1] > lower[1] ? math.max(dn,lower[1]) : dn
os := src > upper ? 1 : src < lower ? 0 : os[1]
spt = os == 1 ? lower : upper
max := ta.cross(src,spt) ? nz(math.max(max[1],src),src) :
os == 1 ? math.max(src,max[1]) :
math.min(spt,max[1])
min := ta.cross(src,spt) ? nz(math.min(min[1],src),src) :
os == 0 ? math.min(src,min[1]) :
math.max(spt,min[1])
avg = math.avg(max,min)
//------------------------------------------------------------------------------
var area_up_col = color.new(#0cb51a,80)
var area_dn_col = color.new(#ff1100,80)
plot0 = plot(max,'Upper Channel'
,max != max[1] and os == 1 ? na : #0cb51a)
plot1 = plot(avg,'Average'
,#ff5d00)
plot2 = plot(min,'Lower Channel'
,min != min[1] and os == 0 ? na : #ff1100)
fill(plot0,plot1,area_up_col,'Upper Area')
fill(plot1,plot2,area_dn_col,'Lower Area')
try this. I added Bollinger trend as well.
CSS:
#/ This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
#// © LuxAlgo
#indicator("Supertrend Channels [LuxAlgo]",overlay=true,max_lines_count=500)
# Converted and mod by Sam4Cok@Samer800        - 05/2023
input ShowSupertrendChannel = yes;
input CalcMethod = {default "ATR Trend", "Bollinger Trend"};
input length = 14;
input mult   = 2;
input src = close;

def na = Double.NaN;
def atrTrend = CalcMethod == CalcMethod."ATR Trend";
#---color
DefineGlobalColor("Blue" , CreateColor(33, 150, 243));
DefineGlobalColor("up", CreateColor(76, 175, 80));
DefineGlobalColor("dn", CreateColor(255, 82, 82));

    def upperBand;
    def lowerBand;
    def nATR = ATR(LENGTH = length) * mult;
    def bbup = Average(high, length) + StDev(high, length) * mult;
    def bbdn = Average(low, length)  - StDev(low, length) * mult;
    def atrup = hl2 + nATR;
    def atrdn = hl2 - 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 upperBand[1];
    def dn1 = if (IsNaN(lowerBand[1]) or lowerBand[1] == 0) then dn else lowerBand[1];
        upperBand = if src[1] < up1 then min(up,up1) else up;
        lowerBand = if src[1] > dn1 then max(dn,dn1) else dn;

#-- ST
def max; def min;
def upper  = upperBand;
def lower  = lowerBand;

def os  = if src > upper then 1 else if src < lower then 0 else os[1];
def spt = if os == 1 then lower else upper;
def cross = (src>spt and src[1]<=spt[1]) or (src<spt and src[1]>=spt[1]);
def max1 = if (IsNaN(max[1]) or max[1]==0) then src else max[1];
def min1 = if (IsNaN(min[1]) or min[1]==0) then src else min[1];
    max = if cross then max(max1,src) else
                   if os == 1 then max(src,max1) else min(spt, max1);
    min = if cross then min(min1,src) else
                   if os == 0 then min(src, min1) else max(spt,min1);
def avg = (max + min) / 2;
def col = if avg>avg[1] and src>avg then 1 else
          if avg<avg[1] and src<avg then -1 else 0;

#//------------------------------------------------------------------------------

plot UpperChannel = if max != max[1] and os == 1 or !ShowSupertrendChannel then na else max;    # 'Upper Channel'
plot AvgChannel   = avg;                                              # 'Average' ,#ff5d00)
plot LowerCahnel = if min != min[1] and os == 0 or !ShowSupertrendChannel then na else min;     # 'Lower Channel'

UpperChannel.SetDefaultColor(GlobalColor("up"));
LowerCahnel.SetDefaultColor(GlobalColor("dn"));
AvgChannel.AssignValueColor(if col > 0 then GlobalColor("up") else
                            if col < 0 then GlobalColor("dn") else Color.DARK_GRAY);

#-- Cloud
AddCloud(UpperChannel,AvgChannel, Color.DARK_GREEN); # 'Upper Area'
AddCloud(AvgChannel,LowerCahnel, Color.DARK_RED);    # 'Lower Area'

#-- END of CODE
 
I know I'm a bit late to the party here but I've found this to be a really useful indicator.

Any chance this can be translated into MTF @samer800? I gave it a shot but my ThinkScript-fu needs work (obviously).
 
I know I'm a bit late to the party here but I've found this to be a really useful indicator.

Any chance this can be translated into MTF @samer800? I gave it a shot but my ThinkScript-fu needs work (obviously).
check the below

CSS:
#/ This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
#// © LuxAlgo
#indicator("Supertrend Channels [LuxAlgo]",overlay=true,max_lines_count=500)
# Converted and mod by Sam4Cok@Samer800        - 05/2023
# Updated - Added MTF option - 09/2023
input ShowSupertrendChannel = yes;
input CalcMethod = {default "ATR Trend", "Bollinger Trend"};
input length = 14;
input mult   = 2;
input src = close;
input useChartTimeframe =  {default"Yes", "No"};
input manualTimeframe   = AggregationPeriod.FIFTEEN_MIN;

def MTFc = close(Period=manualTimeframe);
def MTFh = high(Period=manualTimeframe);
def MTFl = low(Period=manualTimeframe);
def MTFhl = hl2(Period=manualTimeframe);
#-- MTF
def c; def h; def l; def hl;
Switch (useChartTimeframe) {
case "Yes" :
    c = src;
    h = high;
    l = low;
    hl = hl2;
case "No"  :
    c = MTFc;
    h = MTFh;
    l = MTFl;
    hl = MTFhl;
}

def na = Double.NaN;
def atrTrend = CalcMethod == CalcMethod."ATR Trend";
#---color
DefineGlobalColor("Blue" , CreateColor(33, 150, 243));
DefineGlobalColor("up", CreateColor(76, 175, 80));
DefineGlobalColor("dn", CreateColor(255, 82, 82));

    def upperBand;
    def lowerBand;
    def tr = TrueRange(h, c, l);
    def nATR = WildersAverage(tr, length) * mult;
    def bbup = Average(h, length) + StDev(h, length) * mult;
    def bbdn = Average(l, length)  - StDev(l, length) * mult;
    def atrup = hl + nATR;
    def atrdn = hl - 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 upperBand[1];
    def dn1 = if (IsNaN(lowerBand[1]) or lowerBand[1] == 0) then dn else lowerBand[1];
        upperBand = if c[1] < up1 then min(up,up1) else up;
        lowerBand = if c[1] > dn1 then max(dn,dn1) else dn;

#-- ST
def max; def min;
def upper  = upperBand;
def lower  = lowerBand;

def os  = if c > upper then 1 else if c < lower then 0 else os[1];
def spt = if os == 1 then lower else upper;
def cross = (c>spt and c[1]<=spt[1]) or (c<spt and c[1]>=spt[1]);
def max1 = if (IsNaN(max[1]) or max[1]==0) then c else max[1];
def min1 = if (IsNaN(min[1]) or min[1]==0) then c else min[1];
    max = if cross then max(max1,c) else
                   if os == 1 then max(c,max1) else min(spt, max1);
    min = if cross then min(min1,c) else
                   if os == 0 then min(c, min1) else max(spt,min1);
def avg = (max + min) / 2;
def col = if avg>avg[1] and c>avg then 1 else
          if avg<avg[1] and c<avg then -1 else 0;

#//---------------------------------------------

plot UpperChannel = if max != max[1] and os == 1 or !ShowSupertrendChannel then na else max;
plot AvgChannel   = avg;  # 'Average'
plot LowerCahnel = if min != min[1] and os == 0 or !ShowSupertrendChannel then na else min;

UpperChannel.SetDefaultColor(GlobalColor("up"));
LowerCahnel.SetDefaultColor(GlobalColor("dn"));
AvgChannel.AssignValueColor(if col > 0 then GlobalColor("up") else
                            if col < 0 then GlobalColor("dn") else Color.DARK_GRAY);

#-- Cloud
AddCloud(UpperChannel,AvgChannel, Color.DARK_GREEN); # 'Upper Area'
AddCloud(AvgChannel,LowerCahnel, Color.DARK_RED);    # 'Lower Area'

#-- END of CODE
 
Thank you @samer800... you are truly a god. I'll also go through and compare to the non MTF version to see what magic you made here get a bit better at this.

I did want to mention (in the case it might help you or someone else) that the study's plots will disappear (although the clouds will remain) when I add any new studies or edit any other studies currently displayed on that chart. And they will eventually "re-print" when I select a different ticker from my watchlist.

Does this happen on your end as well? Is there any way to optimize the script (or my system) so it doesn't "un-paint" when I edit other studies?

In any case, this is a great script and I really appreciate you taking the time to help out. It's much appreciated!
 
Last edited:

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
430 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