# 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));
#