RSI with Vertical Colors for ThinkorSwim

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

Could you please look at the indicator from tradingview link below for vertical colors? Vertical colors are added with gradual green or red enhancements ( from light green to dark green or from light red to dark red changes) under oversold or overbought states. https://www.tradingview.com/script/2UwrBaSr-color-coded-Relative-Strength-Index/


add columns of color shading , dependant on what range the RSI is in.

add a yes/no control for vertical lines

convert the original hex color numbers to decimal, for RGB numbers. to be used in a CreateColor(255 ,84 ,0)
https://www.w3schools.com/colors/colors_picker.asp

add clouds to be drawn, based on different ranges of the rsi.
0 to 10, 10 to 20, 20 to 30, 30 to 70, 70 to 80, 80 to 90, 90 to 100.

i tried to use 1 addcloud, and 3 RGB color number formulas, but it didn't work. addcloud wants a constant for a color.
so i made an addcloud for each rsi range.

clouds need 2 data points in a row. if rsi moves fast and only has a value in 1 range, no cloud will be drawn.
with just basic rules, there can be gaps in colors, when a data value jumps across several level ranges.
add a condition to be true, if the next bar is in the range.

i didn't clean this study up. i left my commented code in to show what i did.


Ruby:
# rsi_colors_obos_0

# copy and chng vert lines to clouds

# petergluis
# https://usethinkscript.com/threads/i-need-your-help-to-add-colors-into-rsi-when-i-convert-color-coded-relative-strength-index-for-thinkorswim.10347/#post-92080
# post #2
# Smoothed RSI2
# Pensar
# Created early 2020
# Modified by Peter Luis

declare lower;
input period = 2;
input over_bought = 90;
input over_sold = 10;
input idata = close;
input averageType = AverageType.WILDERS;

def src_1 = ohlc4;
input len_1 = 9;
def len_2 = 2 * len_1;
def len_3 = 2 * len_2;
input Over_Bought1 = 70;
def MidLine = 50;
input Over_Sold1 = 30;

def NetChgAvg1 = ExpAverage(src_1 - src_1[1], len_1);
def TotChgAvg1 = ExpAverage(AbsValue(src_1 - src_1[1]), len_1);
def ChgRatio1 = if TotChgAvg1 != 0
                  then NetChgAvg1 / TotChgAvg1
                  else 0;
plot rsi1 = 50 * (ChgRatio1 + 1);

def NetChgAvg2 = ExpAverage(src_1 - src_1[1], len_2);
def TotChgAvg2 = ExpAverage(AbsValue(src_1 - src_1[1]), len_2);
def ChgRatio2 = if TotChgAvg1 != 0
                  then NetChgAvg2 / TotChgAvg2
                  else 0;
plot rsi2 = 50 * (ChgRatio2 + 1);

def NetChgAvg3 = ExpAverage(src_1 - src_1[1], len_3);
def TotChgAvg3 = ExpAverage(AbsValue(src_1 - src_1[1]), len_3);
def ChgRatio3 = if TotChgAvg1 != 0
                  then NetChgAvg3 / TotChgAvg3
                  else 0;
plot rsi3 = 50 * (ChgRatio3 + 1);
plot middle_Line = MidLine;
plot Over_Bought_Line = Over_Bought1;
plot Over_Sold_Line = Over_Sold1;
plot Over_Bought_line1 = 90;
plot Over_sold_line1 = 10;


def NetChgAvg = MovingAverage(averageType, idata - idata[3], period);
def TotChgAvg = MovingAverage(averageType, AbsValue(idata - idata[3]), period);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

plot RSI = 50 * (ChgRatio + 1);
     RSI.DefineColor("OverBought", color.red);
     RSI.DefineColor("Normal", color.gray);
     RSI.DefineColor("OverSold", color.green);
     RSI.AssignValueColor(if RSI > over_Bought then RSI.color("OverBought")
                          else if RSI < over_Sold then RSI.color("OverSold")
                          else RSI.color("Normal"));
plot OverSold = over_Sold;
plot OverBought = over_Bought;
     OverSold.SetDefaultColor(color.green);
     Oversold.setstyle(curve.short_dash);
     Oversold.setlineweight(3);
     OverBought.SetDefaultColor(color.red);
     OverBought.setstyle(curve.short_dash);
     Oversold.setlineweight(3);

AddCloud(if RSI > OverBought then RSI else double.nan, Overbought, color.red,color.red);
AddCloud(if RSI < OverSold then RSI else double.nan, OverSold, color.green,color.green);


input show_vertical_lines = no;
AddVerticalLine(show_vertical_lines and rsi1 crosses above 70, "70", Color.RED);
AddVerticalLine(show_vertical_lines and rsi1 crosses above 75, "75", Color.RED);
AddVerticalLine(show_vertical_lines and rsi1 crosses above 80, "80", Color.DARK_RED);
AddVerticalLine(show_vertical_lines and rsi1 crosses above 85, "85", Color.DARK_RED);
AddVerticalLine(show_vertical_lines and rsi1 crosses below 20, "20", Color.green);
AddVerticalLine(show_vertical_lines and rsi1 crosses below 15, "15", Color.GREEN);
AddVerticalLine(show_vertical_lines and rsi1 crosses below 10, "10", Color.GREEN);
AddVerticalLine(show_vertical_lines and rsi1 crosses below 5, "5", Color.GREEN);

#------------------------------------------------------

# add cloud colors
# show_color_shading


def na = double.nan;
input show_color_shading = yes;

#  RGB color #'s
#col1 = #FF0000 ,  255,   0, 0  red
#col2 = #FF5400 ,  255,  84, 0  lt red
#col3 = #FFAA00 ,  255, 170, 0  orange
#col4 = #FFFF00 ,  255, 255, 0  yellow
#col5 = #AAFF00 ,  170, 255, 0  lt green ?
#col6 = #54FF00 ,   84, 255, 0  md green ?
#col7 = #00FF00 ,    0, 255, 0  green


# this leaves gaps in colors, when 2 points in a row, aren't in the same range
#addcloud( (if rsi <= 10 then 100 else na), 0 , CreateColor(255 ,0 ,0));
#addcloud( (if rsi > 10 and rsi <= 20 then 100 else na), 0 , CreateColor(255 ,84 ,0));
#addcloud( (if rsi > 20 and rsi <= 30 then 100 else na), 0 , CreateColor(255 ,170 ,0));
#addcloud( (if rsi > 30 and rsi <= 70 then 100 else na), 0 , CreateColor(255 ,255 ,0));
#addcloud( (if rsi > 70 and rsi <= 80 then 100 else na), 0 , CreateColor(170 ,255 ,0));
#addcloud( (if rsi > 80 and rsi <= 90 then 100 else na), 0 , CreateColor(84 ,255 ,0));
#addcloud( (if rsi > 90 then 100 else na), 0 , CreateColor(0 ,255 ,0));


# with just basic rules, there can be gaps in colors when data value jump across several level ranges.
# add condition to be true, if next bar is in the range
def r10 = if show_color_shading and (rsi <= 10 or rsi[-1] <= 10) then 1 else 0;
def r20 = if show_color_shading and ((rsi > 10 and rsi <= 20) or (rsi[-1] > 10 and rsi[-1] <= 20)) then 1 else 0;
def r30 = if show_color_shading and ((rsi > 20 and rsi <= 30) or (rsi[-1] > 20 and rsi[-1] <= 30)) then 1 else 0;
def r70 = if show_color_shading and ((rsi > 30 and rsi <= 70) or (rsi[-1] > 30 and rsi[-1] <= 70)) then 1 else 0;
def r80 = if show_color_shading and ((rsi > 70 and rsi <= 80) or (rsi[-1] > 70 and rsi[-1] <= 80)) then 1 else 0;
def r90 = if show_color_shading and ((rsi > 80 and rsi <= 90) or (rsi[-1] > 80 and rsi[-1] <= 90)) then 1 else 0;
def r100 = if show_color_shading and (rsi > 90 or rsi[-1] > 90) then 1 else 0;


# make a cloud for each color
# use if then to enable the high $ when rsi is in the range
addcloud( (if r10 then 100 else na), 0 , CreateColor(255 ,0 ,0));
addcloud( (if r20 then 100 else na), 0 , CreateColor(255 ,84 ,0));
addcloud( (if r30 then 100 else na), 0 , CreateColor(255 ,170 ,0));
addcloud( (if r70 then 100 else na), 0 , CreateColor(255 ,255 ,0));
addcloud( (if r80 then 100 else na), 0 , CreateColor(170 ,255 ,0));
addcloud( (if r90 then 100 else na), 0 , CreateColor(84 ,255 ,0));
addcloud( (if r100 then 100 else na), 0 , CreateColor(0 ,255 ,0));


# -------------------------------
#addcloud( 100 , 0 , color.green, color.red);
# 1st price level is the higher value
# 2nd price level is the lower value
# 1st color, is used when 1st $ > 2nd $
# 2nd color, is used when 1st $ < 2nd $

# ------------------------------------------
input test2_rsi_numbers = no;
addchartbubble(test2_rsi_numbers, 0, rsi, color.yellow, yes);

# -------------------------------------------------
# https://www.w3schools.com/colors/colors_picker.asp

# ---------------------------


#doesn't work

def red1 = if rsi <= 10 then 255
 else if rsi > 10 and rsi <= 20 then 255
 else if rsi > 20 and rsi <= 30 then 255
 else if rsi > 30 and rsi <= 70 then 255
 else if rsi > 70 and rsi <= 80 then 170
 else if rsi > 80 and rsi <= 90 then 84
 else 0;

def green1 = if rsi <= 10 then 0
 else if rsi > 10 and rsi <= 20 then 84
 else if rsi > 20 and rsi <= 30 then 170
 else if rsi > 30 and rsi <= 70 then 255
 else if rsi > 70 and rsi <= 80 then 255
 else if rsi > 80 and rsi <= 90 then 255
 else 255;

def blue1 = 0;

# doesnt work. wants constants for color
#addcloud(100,0,CreateColor(red1,green1,blue1));

#

DLSnLv1.jpg

hal_color
 
Last edited:
add columns of color shading , dependant on what range the RSI is in.

add a yes/no control for vertical lines

convert the original hex color numbers to decimal, for RGB numbers. to be used in a CreateColor(255 ,84 ,0)
https://www.w3schools.com/colors/colors_picker.asp

add clouds to be drawn, based on different ranges of the rsi.
0 to 10, 10 to 20, 20 to 30, 30 to 70, 70 to 80, 80 to 90, 90 to 100.

i tried to use 1 addcloud, and 3 RGB color number formulas, but it didn't work. addcloud wants a constant for a color.
so i made an addcloud for each rsi range.

clouds need 2 data points in a row. if rsi moves fast and only has a value in 1 range, no cloud will be drawn.
with just basic rules, there can be gaps in colors, when a data value jumps across several level ranges.
add a condition to be true, if the next bar is in the range.

i didn't clean this study up. i left my commented code in to show what i did.


Ruby:
# rsi_colors_obos_0

# copy and chng vert lines to clouds

# petergluis
# https://usethinkscript.com/threads/i-need-your-help-to-add-colors-into-rsi-when-i-convert-color-coded-relative-strength-index-for-thinkorswim.10347/#post-92080
# post #2
# Smoothed RSI2
# Pensar
# Created early 2020
# Modified by Peter Luis

declare lower;
input period = 2;
input over_bought = 90;
input over_sold = 10;
input idata = close;
input averageType = AverageType.WILDERS;

def src_1 = ohlc4;
input len_1 = 9;
def len_2 = 2 * len_1;
def len_3 = 2 * len_2;
input Over_Bought1 = 70;
def MidLine = 50;
input Over_Sold1 = 30;

def NetChgAvg1 = ExpAverage(src_1 - src_1[1], len_1);
def TotChgAvg1 = ExpAverage(AbsValue(src_1 - src_1[1]), len_1);
def ChgRatio1 = if TotChgAvg1 != 0
                  then NetChgAvg1 / TotChgAvg1
                  else 0;
plot rsi1 = 50 * (ChgRatio1 + 1);

def NetChgAvg2 = ExpAverage(src_1 - src_1[1], len_2);
def TotChgAvg2 = ExpAverage(AbsValue(src_1 - src_1[1]), len_2);
def ChgRatio2 = if TotChgAvg1 != 0
                  then NetChgAvg2 / TotChgAvg2
                  else 0;
plot rsi2 = 50 * (ChgRatio2 + 1);

def NetChgAvg3 = ExpAverage(src_1 - src_1[1], len_3);
def TotChgAvg3 = ExpAverage(AbsValue(src_1 - src_1[1]), len_3);
def ChgRatio3 = if TotChgAvg1 != 0
                  then NetChgAvg3 / TotChgAvg3
                  else 0;
plot rsi3 = 50 * (ChgRatio3 + 1);
plot middle_Line = MidLine;
plot Over_Bought_Line = Over_Bought1;
plot Over_Sold_Line = Over_Sold1;
plot Over_Bought_line1 = 90;
plot Over_sold_line1 = 10;


def NetChgAvg = MovingAverage(averageType, idata - idata[3], period);
def TotChgAvg = MovingAverage(averageType, AbsValue(idata - idata[3]), period);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

plot RSI = 50 * (ChgRatio + 1);
     RSI.DefineColor("OverBought", color.red);
     RSI.DefineColor("Normal", color.gray);
     RSI.DefineColor("OverSold", color.green);
     RSI.AssignValueColor(if RSI > over_Bought then RSI.color("OverBought")
                          else if RSI < over_Sold then RSI.color("OverSold")
                          else RSI.color("Normal"));
plot OverSold = over_Sold;
plot OverBought = over_Bought;
     OverSold.SetDefaultColor(color.green);
     Oversold.setstyle(curve.short_dash);
     Oversold.setlineweight(3);
     OverBought.SetDefaultColor(color.red);
     OverBought.setstyle(curve.short_dash);
     Oversold.setlineweight(3);

AddCloud(if RSI > OverBought then RSI else double.nan, Overbought, color.red,color.red);
AddCloud(if RSI < OverSold then RSI else double.nan, OverSold, color.green,color.green);


input show_vertical_lines = no;
AddVerticalLine(show_vertical_lines and rsi1 crosses above 70, "70", Color.RED);
AddVerticalLine(show_vertical_lines and rsi1 crosses above 75, "75", Color.RED);
AddVerticalLine(show_vertical_lines and rsi1 crosses above 80, "80", Color.DARK_RED);
AddVerticalLine(show_vertical_lines and rsi1 crosses above 85, "85", Color.DARK_RED);
AddVerticalLine(show_vertical_lines and rsi1 crosses below 20, "20", Color.green);
AddVerticalLine(show_vertical_lines and rsi1 crosses below 15, "15", Color.GREEN);
AddVerticalLine(show_vertical_lines and rsi1 crosses below 10, "10", Color.GREEN);
AddVerticalLine(show_vertical_lines and rsi1 crosses below 5, "5", Color.GREEN);

#------------------------------------------------------

# add cloud colors
# show_color_shading


def na = double.nan;
input show_color_shading = yes;

#  RGB color #'s
#col1 = #FF0000 ,  255,   0, 0  red
#col2 = #FF5400 ,  255,  84, 0  lt red
#col3 = #FFAA00 ,  255, 170, 0  orange
#col4 = #FFFF00 ,  255, 255, 0  yellow
#col5 = #AAFF00 ,  170, 255, 0  lt green ?
#col6 = #54FF00 ,   84, 255, 0  md green ?
#col7 = #00FF00 ,    0, 255, 0  green


# this leaves gaps in colors, when 2 points in a row, aren't in the same range
#addcloud( (if rsi <= 10 then 100 else na), 0 , CreateColor(255 ,0 ,0));
#addcloud( (if rsi > 10 and rsi <= 20 then 100 else na), 0 , CreateColor(255 ,84 ,0));
#addcloud( (if rsi > 20 and rsi <= 30 then 100 else na), 0 , CreateColor(255 ,170 ,0));
#addcloud( (if rsi > 30 and rsi <= 70 then 100 else na), 0 , CreateColor(255 ,255 ,0));
#addcloud( (if rsi > 70 and rsi <= 80 then 100 else na), 0 , CreateColor(170 ,255 ,0));
#addcloud( (if rsi > 80 and rsi <= 90 then 100 else na), 0 , CreateColor(84 ,255 ,0));
#addcloud( (if rsi > 90 then 100 else na), 0 , CreateColor(0 ,255 ,0));


# with just basic rules, there can be gaps in colors when data value jump across several level ranges.
# add condition to be true, if next bar is in the range
def r10 = if show_color_shading and (rsi <= 10 or rsi[-1] <= 10) then 1 else 0;
def r20 = if show_color_shading and ((rsi > 10 and rsi <= 20) or (rsi[-1] > 10 and rsi[-1] <= 20)) then 1 else 0;
def r30 = if show_color_shading and ((rsi > 20 and rsi <= 30) or (rsi[-1] > 20 and rsi[-1] <= 30)) then 1 else 0;
def r70 = if show_color_shading and ((rsi > 30 and rsi <= 70) or (rsi[-1] > 30 and rsi[-1] <= 70)) then 1 else 0;
def r80 = if show_color_shading and ((rsi > 70 and rsi <= 80) or (rsi[-1] > 70 and rsi[-1] <= 80)) then 1 else 0;
def r90 = if show_color_shading and ((rsi > 80 and rsi <= 90) or (rsi[-1] > 80 and rsi[-1] <= 90)) then 1 else 0;
def r100 = if show_color_shading and (rsi > 90 or rsi[-1] > 90) then 1 else 0;


# make a cloud for each color
# use if then to enable the high $ when rsi is in the range
addcloud( (if r10 then 100 else na), 0 , CreateColor(255 ,0 ,0));
addcloud( (if r20 then 100 else na), 0 , CreateColor(255 ,84 ,0));
addcloud( (if r30 then 100 else na), 0 , CreateColor(255 ,170 ,0));
addcloud( (if r70 then 100 else na), 0 , CreateColor(255 ,255 ,0));
addcloud( (if r80 then 100 else na), 0 , CreateColor(170 ,255 ,0));
addcloud( (if r90 then 100 else na), 0 , CreateColor(84 ,255 ,0));
addcloud( (if r100 then 100 else na), 0 , CreateColor(0 ,255 ,0));


# -------------------------------
#addcloud( 100 , 0 , color.green, color.red);
# 1st price level is the higher value
# 2nd price level is the lower value
# 1st color, is used when 1st $ > 2nd $
# 2nd color, is used when 1st $ < 2nd $

# ------------------------------------------
input test2_rsi_numbers = no;
addchartbubble(test2_rsi_numbers, 0, rsi, color.yellow, yes);

# -------------------------------------------------
# https://www.w3schools.com/colors/colors_picker.asp

# ---------------------------


#doesn't work

def red1 = if rsi <= 10 then 255
 else if rsi > 10 and rsi <= 20 then 255
 else if rsi > 20 and rsi <= 30 then 255
 else if rsi > 30 and rsi <= 70 then 255
 else if rsi > 70 and rsi <= 80 then 170
 else if rsi > 80 and rsi <= 90 then 84
 else 0;

def green1 = if rsi <= 10 then 0
 else if rsi > 10 and rsi <= 20 then 84
 else if rsi > 20 and rsi <= 30 then 170
 else if rsi > 30 and rsi <= 70 then 255
 else if rsi > 70 and rsi <= 80 then 255
 else if rsi > 80 and rsi <= 90 then 255
 else 255;

def blue1 = 0;

# doesnt work. wants constants for color
#addcloud(100,0,CreateColor(red1,green1,blue1));

#

DLSnLv1.jpg
Halcyonguy, Thank you very much for your excellent job!
 
Last edited:
Halcyonguy, Thank you very much for your excellent job!
Halcyonguy, I have a question about how to convert following condition expression of its original code above from pine script to Thinkscript. It seems that pine script is more powerful in condition expressions than Thinkscript:

sent_2 = rsi_1 > 70 or rsi_1 < 30 ? rsi_2 < 30 ? -(30-rsi_2)/2 : rsi_2 > 70 ? (rsi_2-70)/2 : 0 : 0

Thank you very much for your help.
 
Last edited:
Halcyonguy, I have a question about how to convert following condition expression of its original code above from pine script to Thinkscript. It seems that pine script is more powerful in condition expressions than Thinkscript:

sent_2 = rsi_1 > 70 or rsi_1 < 30 ? rsi_2 < 30 ? -(30-rsi_2)/2 : rsi_2 > 70 ? (rsi_2-70)/2 : 0 : 0

Thank you very much for your help.

pine if thens,
You look for the ? and then what is before it, is the condition.
after the ? is the true choice.
after the : is the false choice.
this one has a condition after a ? , so it is a little tricky. it helps to indent the lines differently.


copy this code to use in tos
Code:
def sent_2 =
if ( rsi_1 > 70 or rsi_1 < 30 ) then
    if rsi_2 < 30 then
        -(30-rsi_2)/2
    else  if  rsi_2 > 70 then
         (rsi_2-70)/2
    else  0
else  0;




?
https://www.tradingview.com/pine-sc...tml#conditional-operator-and-the-iff-function

condition ? result1 : result2

If condition is true then the ternary operator will return result1, otherwise it will return result2.


https://www.tradingview.com/pine-script-docs/en/v4/language/Conditional_structures.html

https://kodify.net/tradingview/if-else/if-statement/
 
Last edited:
pine if thens,
You look for the ? and then what is before it, is the condition.
after the ? is the true choice.
after the : is the false choice.
this one has a condition after a ? , so it is a little tricky. it helps to indent the lines differently.


copy this code to use in tos
Code:
def sent_2 =
if ( rsi_1 > 70 or rsi_1 < 30 ) then
    if rsi_2 < 30 then
        -(30-rsi_2)/2
    else  if  rsi_2 > 70 then
         (rsi_2-70)/2
    else  0
else  0;




?
https://www.tradingview.com/pine-sc...tml#conditional-operator-and-the-iff-function

condition ? result1 : result2

If condition is true then the ternary operator will return result1, otherwise it will return result2.


https://www.tradingview.com/pine-script-docs/en/v4/language/Conditional_structures.html

https://kodify.net/tradingview/if-else/if-statement/
Halcyonguy, thank you very much your explanation. I learn a lot from you.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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