Support & Resistance S/R Channel For ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
Per several member requests from here:
https://usethinkscript.com/threads/convert-tradingview-support-resistance-scripts.12101/

try this. - Updated

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © BarsStallone
#// Code update provided by @christofferka
#indicator("Support/Resistance", shorttitle="S/R", overlay=true, max_lines_count = 500)
#//Converted by Sam4Cok@Samer800 - 01/2023
# update by Sam4Cok@Samer800    - 02/2024

input timeframe = {Default "Chart", "Custom TF"};
input customeTimeframe = AggregationPeriod.FIFTEEN_MIN;
input showTfLabel = yes;
input showLastLines = yes;
input pivotLength = 2;
input rsiLength = 9;
input cmoLength = 1;

def na = Double.NaN;
def bar = BarNumber();
def chart = GetAggregationPeriod();
script tfLabel {
input chart = 300000;
    def tf = if chart < AggregationPeriod.HOUR then chart / AggregationPeriod.MIN else
             if chart < AggregationPeriod.DAY then chart / AggregationPeriod.HOUR else
             if chart < AggregationPeriod.MONTH then chart / AggregationPeriod.DAY else chart / AggregationPeriod.MONTH;
    def tfLab = if chart < AggregationPeriod.HOUR then 1 else
                if chart < AggregationPeriod.DAY then 2 else
                if chart < AggregationPeriod.MONTH then 3 else 4;
    plot val = tf;
    plot lab = tfLab;
}
def tf = tfLabel(chart).val;
def tfLab = tfLabel(chart).lab;
def htf =  tfLabel(customeTimeframe).val;
def htfLab = tfLabel(customeTimeframe).lab;
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if !IsNaN(data) then data else repl;
    plot return = ret_val;
}
script fixnan {
    input source = close;
    def fix = if !IsNaN(source) then source else nz(fix[1],source);
    plot result = fix;
}
def hi; def lo; def op; def cl;def val; def lab;
Switch (timeframe) {
Case  "Custom TF" :
    hi = high(period= customeTimeframe);
    lo = low(period= customeTimeframe);
    op = open(period= customeTimeframe);
    cl = close(period= customeTimeframe);
    val = htf;
    lab = htfLab;
Default :
    hi = high;
    lo = low;
    op = open;
    cl = close;
    val = tf;
    lab = tfLab;
}
AddLabel(showTfLabel, val + if lab==1 then " m" else
                            if lab==2 then " H" else
                            if lab==3 then " D" else " M", Color.WHITE);
#//Legacy RSI calc
def rsi_new = rsi(Price = cl, Length = rsiLength);
#//CMO based on HMA
def length1 = cmoLength;
def src1 = HullMovingAvg(op, 5)[1];
def src2 = HullMovingAvg(cl, 12);
def momm1 = src1 - src1[1];
def momm2 = src2 - src2[1];
def m1 = if momm1 >= momm2 then momm1 else 0;
def m2 = if momm1 >= momm2 then 0 else -momm1;
def sm1 = Sum(m1, length1);
def sm2 = Sum(m2, length1);
def cmo1 = sm1 - sm2;
def cmo2 = sm1 + sm2;
def cmo_new = 100 * cmo1 / cmo2;
#//Legacy Close Pivots calcs.
def len5 = pivotLength;
def hh = Highest(hi, len5);
def h1 = if stdev(hh, len5) then na else hh;
def hpivot = fixnan(h1);
def ll = Lowest(lo , len5);
def l1 = if stdev(ll, len5) then na else ll;
def lpivot = fixnan(l1);

#//Calc Values
def hpivot_new = hpivot;
def lpivot_new = lpivot;

def res = rsi_new > 75 and cmo_new <-50 and !isNaN(hpivot_new);
def sup = rsi_new < 25 and cmo_new > 50 and !isNaN(lpivot_new);

def calcXupH = if sup then high else calcXupH[1];
def calcXupL = if sup then low else calcXupL[1];
def calcXdnH = if res then high else calcXdnH[1];
def calcXdnL = if res then low else calcXdnL[1];
def calcXupIdx = if sup then bar else calcXupIdx[1];
def calcXdnIdx = if res then bar else calcXdnIdx[1];
def condUp = if showLastLines then bar >= highestAll(calcXupIdx) else 1;
def condDn = if showLastLines then bar >= highestAll(calcXdnIdx) else 1;

#//Lines drawing variables
def tf1H = if calcXupH and condUp then calcXupH else na;
def tf1L = if calcXupL and condUp then calcXupL else na;
def tf2H = if calcXdnH and condDn then calcXdnH else na;
def tf2L = if calcXdnL and condDn then calcXdnL else na;

AddCloud(if tf1H==tf1H[1] then tf1H else na, tf1L, Color.DARK_GREEN, Color.DARK_GREEN, yes);
AddCloud(if tf2H==tf2H[1] then tf2H else na, tf2L, Color.DARK_RED, Color.DARK_RED, yes);

#---- END of Code
 
Last edited:
Per several member requests from here:
https://usethinkscript.com/threads/convert-tradingview-support-resistance-scripts.12101/

try this.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © BarsStallone
#// Code update provided by @christofferka
#indicator("Support/Resistance", shorttitle="S/R", overlay=true, max_lines_count = 500)
#//Converted by Sam4Cok@Samer800 - 01/2023

input useChartTimeframe = yes;
input timeframe = AggregationPeriod.FIFTEEN_MIN;

def na = Double.NaN;
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if !IsNaN(data) then data else repl;
    plot return = ret_val;
}
script fixnan {
    input source = close;
    def fix = if !IsNaN(source) then source else nz(fix[1],source);
    plot result = fix;
}
def hi; def lo; def op; def cl;
if useChartTimeframe {
    hi = high;
    lo = low;
    op = open;
    cl = close;
    } else {
    hi = high(period= timeframe);
    lo = low(period= timeframe);
    op = open(period= timeframe);
    cl = close(period= timeframe);
}
#//CMO based on HMA
def length1 = 1;
def src1 = HullMovingAvg(op, 5)[1];
def src2 = HullMovingAvg(cl, 12);
def momm1 = src1 - src1[1];
def momm2 = src2 - src2[1];

def m1 = if momm1 >= momm2 then momm1 else 0;
def m2 = if momm1 >= momm2 then 0 else -momm1;
def sm1 = Sum(m1, length1);
def sm2 = Sum(m2, length1);
def cmo1 = sm1 - sm2;
def cmo2 = sm1 + sm2;
def cmo_new = 100 * cmo1 / cmo2;

def len5 = 2;
def h = Highest(hi, len5);
def h1 = if StandardDeviation(h, len5) then na else h;
def hpivot = fixnan(h1);
def l = Lowest(lo , len5);
def l1 = if StandardDeviation(l, len5) then na else l;
def lpivot = fixnan(l1);

#//Calc Values
def rsi_new = rsi(Price = cl, Length = 9);
def hpivot_new = hpivot;
def lpivot_new = lpivot;

def res = rsi_new > 75 and cmo_new < -50 and !isNaN(hpivot_new);
def sup = rsi_new < 25 and cmo_new > 50 and !isNaN(lpivot_new);

def calcXup = if sup then low else calcXup[1];
def calcXdown = if res then high else calcXdown[1];
 
#//Lines drawing variables
plot tf1 = if calcXup==0 then na else calcXup;
tf1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
tf1.SetDefaultColor(Color.UPTICK);
plot tf2 = if calcXdown==0 then na else calcXdown;
tf2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
tf2.SetDefaultColor(Color.DOWNTICK);

#---- END of Code
Can someone help to add price at tf1 and tf2? price at peak and valley.
 
Per several member requests from here:
https://usethinkscript.com/threads/convert-tradingview-support-resistance-scripts.12101/

try this.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © BarsStallone
#// Code update provided by @christofferka
#indicator("Support/Resistance", shorttitle="S/R", overlay=true, max_lines_count = 500)
#//Converted by Sam4Cok@Samer800 - 01/2023

input useChartTimeframe = yes;
input timeframe = AggregationPeriod.FIFTEEN_MIN;

def na = Double.NaN;
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if !IsNaN(data) then data else repl;
    plot return = ret_val;
}
script fixnan {
    input source = close;
    def fix = if !IsNaN(source) then source else nz(fix[1],source);
    plot result = fix;
}
def hi; def lo; def op; def cl;
if useChartTimeframe {
    hi = high;
    lo = low;
    op = open;
    cl = close;
    } else {
    hi = high(period= timeframe);
    lo = low(period= timeframe);
    op = open(period= timeframe);
    cl = close(period= timeframe);
}
#//CMO based on HMA
def length1 = 1;
def src1 = HullMovingAvg(op, 5)[1];
def src2 = HullMovingAvg(cl, 12);
def momm1 = src1 - src1[1];
def momm2 = src2 - src2[1];

def m1 = if momm1 >= momm2 then momm1 else 0;
def m2 = if momm1 >= momm2 then 0 else -momm1;
def sm1 = Sum(m1, length1);
def sm2 = Sum(m2, length1);
def cmo1 = sm1 - sm2;
def cmo2 = sm1 + sm2;
def cmo_new = 100 * cmo1 / cmo2;

def len5 = 2;
def h = Highest(hi, len5);
def h1 = if StandardDeviation(h, len5) then na else h;
def hpivot = fixnan(h1);
def l = Lowest(lo , len5);
def l1 = if StandardDeviation(l, len5) then na else l;
def lpivot = fixnan(l1);

#//Calc Values
def rsi_new = rsi(Price = cl, Length = 9);
def hpivot_new = hpivot;
def lpivot_new = lpivot;

def res = rsi_new > 75 and cmo_new < -50 and !isNaN(hpivot_new);
def sup = rsi_new < 25 and cmo_new > 50 and !isNaN(lpivot_new);

def calcXup = if sup then low else calcXup[1];
def calcXdown = if res then high else calcXdown[1];
 
#//Lines drawing variables
plot tf1 = if calcXup==0 then na else calcXup;
tf1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
tf1.SetDefaultColor(Color.UPTICK);
plot tf2 = if calcXdown==0 then na else calcXdown;
tf2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
tf2.SetDefaultColor(Color.DOWNTICK);

#---- END of Code

Does this repaint?
 
Does this repaint?
Depends.
What is your input useChartTimeframe set at?
When set to "yes" This script uses the current chart timeframe and there is no repainting.

When set to "no" This script will use whatever higher timeframe that you designate and yes, you are asking the script to repaint.
Think about it. You have a 30-min overlay on a five-min chart. There is no way of predicting at what price that 30min candle is going to close.
When the 30min candle finally closes, only then can it repaint the previous 6 bars with the 30min close price that you requested.
Read more:
https://usethinkscript.com/threads/...d.-,Multi-Timeframe,-REPAINTING-type Behavior
 
Last edited:
  • Love
Reactions: IPA
I get the following on some charts when "use chart timeframe" is set to "No".

SRChannel (no, FIFTEEN_MIN): Trying to self-assign a non-initialized rec: fix.

Any ideas?
 
I get the following on some charts when "use chart timeframe" is set to "No".

SRChannel (no, FIFTEEN_MIN): Trying to self-assign a non-initialized rec: fix.

Any ideas?
Having Problems? Does the MTF indicator give an error or appear to be not working or partially working when you load the study onto your chart?

A common error when using MTF indicators is trying to use a time frame that is lower than the chart you are posting it on. On the TOS platform, you can display data from a higher timeframe onto a lower timeframe, but not the other way around.
https://tlc.thinkorswim.com/center/...hapter-11---Referencing-Secondary-Aggregation
 
Last edited:
Per several member requests from here:
https://usethinkscript.com/threads/convert-tradingview-support-resistance-scripts.12101/

try this.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © BarsStallone
#// Code update provided by @christofferka
#indicator("Support/Resistance", shorttitle="S/R", overlay=true, max_lines_count = 500)
#//Converted by Sam4Cok@Samer800 - 01/2023

input useChartTimeframe = yes;
input timeframe = AggregationPeriod.FIFTEEN_MIN;

def na = Double.NaN;
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if !IsNaN(data) then data else repl;
    plot return = ret_val;
}
script fixnan {
    input source = close;
    def fix = if !IsNaN(source) then source else nz(fix[1],source);
    plot result = fix;
}
def hi; def lo; def op; def cl;
if useChartTimeframe {
    hi = high;
    lo = low;
    op = open;
    cl = close;
    } else {
    hi = high(period= timeframe);
    lo = low(period= timeframe);
    op = open(period= timeframe);
    cl = close(period= timeframe);
}
#//CMO based on HMA
def length1 = 1;
def src1 = HullMovingAvg(op, 5)[1];
def src2 = HullMovingAvg(cl, 12);
def momm1 = src1 - src1[1];
def momm2 = src2 - src2[1];

def m1 = if momm1 >= momm2 then momm1 else 0;
def m2 = if momm1 >= momm2 then 0 else -momm1;
def sm1 = Sum(m1, length1);
def sm2 = Sum(m2, length1);
def cmo1 = sm1 - sm2;
def cmo2 = sm1 + sm2;
def cmo_new = 100 * cmo1 / cmo2;

def len5 = 2;
def h = Highest(hi, len5);
def h1 = if StandardDeviation(h, len5) then na else h;
def hpivot = fixnan(h1);
def l = Lowest(lo , len5);
def l1 = if StandardDeviation(l, len5) then na else l;
def lpivot = fixnan(l1);

#//Calc Values
def rsi_new = rsi(Price = cl, Length = 9);
def hpivot_new = hpivot;
def lpivot_new = lpivot;

def res = rsi_new > 75 and cmo_new < -50 and !isNaN(hpivot_new);
def sup = rsi_new < 25 and cmo_new > 50 and !isNaN(lpivot_new);

def calcXup = if sup then low else calcXup[1];
def calcXdown = if res then high else calcXdown[1];
 
#//Lines drawing variables
plot tf1 = if calcXup==0 then na else calcXup;
tf1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
tf1.SetDefaultColor(Color.UPTICK);
plot tf2 = if calcXdown==0 then na else calcXdown;
tf2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
tf2.SetDefaultColor(Color.DOWNTICK);

#---- END of Code
Thanks! This is great. I would like to add a cloud from the top of the bar to the bottom of the bar (the bar used for the Support and resistance line). So the width of the cloud would be the open and close of the bar. I am having trouble adding that in. Would you be able to help me with that?
 
Last edited:
Per several member requests from here:
https://usethinkscript.com/threads/convert-tradingview-support-resistance-scripts.12101/

try this.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © BarsStallone
#// Code update provided by @christofferka
#indicator("Support/Resistance", shorttitle="S/R", overlay=true, max_lines_count = 500)
#//Converted by Sam4Cok@Samer800 - 01/2023

input useChartTimeframe = yes;
input timeframe = AggregationPeriod.FIFTEEN_MIN;

def na = Double.NaN;
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if !IsNaN(data) then data else repl;
    plot return = ret_val;
}
script fixnan {
    input source = close;
    def fix = if !IsNaN(source) then source else nz(fix[1],source);
    plot result = fix;
}
def hi; def lo; def op; def cl;
if useChartTimeframe {
    hi = high;
    lo = low;
    op = open;
    cl = close;
    } else {
    hi = high(period= timeframe);
    lo = low(period= timeframe);
    op = open(period= timeframe);
    cl = close(period= timeframe);
}
#//CMO based on HMA
def length1 = 1;
def src1 = HullMovingAvg(op, 5)[1];
def src2 = HullMovingAvg(cl, 12);
def momm1 = src1 - src1[1];
def momm2 = src2 - src2[1];

def m1 = if momm1 >= momm2 then momm1 else 0;
def m2 = if momm1 >= momm2 then 0 else -momm1;
def sm1 = Sum(m1, length1);
def sm2 = Sum(m2, length1);
def cmo1 = sm1 - sm2;
def cmo2 = sm1 + sm2;
def cmo_new = 100 * cmo1 / cmo2;

def len5 = 2;
def h = Highest(hi, len5);
def h1 = if StandardDeviation(h, len5) then na else h;
def hpivot = fixnan(h1);
def l = Lowest(lo , len5);
def l1 = if StandardDeviation(l, len5) then na else l;
def lpivot = fixnan(l1);

#//Calc Values
def rsi_new = rsi(Price = cl, Length = 9);
def hpivot_new = hpivot;
def lpivot_new = lpivot;

def res = rsi_new > 75 and cmo_new < -50 and !isNaN(hpivot_new);
def sup = rsi_new < 25 and cmo_new > 50 and !isNaN(lpivot_new);

def calcXup = if sup then low else calcXup[1];
def calcXdown = if res then high else calcXdown[1];
 
#//Lines drawing variables
plot tf1 = if calcXup==0 then na else calcXup;
tf1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
tf1.SetDefaultColor(Color.UPTICK);
plot tf2 = if calcXdown==0 then na else calcXdown;
tf2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
tf2.SetDefaultColor(Color.DOWNTICK);

#---- END of Code
Is there way to add a small label above each support and Resistance level to identify the time frame that is being plotted. This would assist when using multiple time frames.
 
Is there way to add a small label above each support and Resistance level to identify the time frame that is being plotted. This would assist when using multiple time frames.
updated the original code. check it out.
 
Thanks! This is great. I would like to add a cloud from the top of the bar to the bottom of the bar (the bar used for the Support and resistance line). So the width of the cloud would be the open and close of the bar. I am having trouble adding that in. Would you be able to help me with that?

updated the original code. check it out.
 
Last edited by a moderator:

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