# rsi_candle_border_color_addchart
#https://usethinkscript.com/threads/paint-candle-border-based-on-rsi.15275/
#Paint candle border based on RSI
def na = double.nan;
def bn = barnumber();
#------------------------------
# RSI
# TD Ameritrade IP Company, Inc. (c) 2007-2023
#declare lower;
input length = 14;
input over_Bought = 70;
input over_Sold = 30;
input price = close;
input averageType = AverageType.WILDERS;
input showBreakoutSignals = no;
def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);
def OverSold = over_Sold;
#plot OverBought = over_Bought;
#plot UpSignal = if RSI crosses above OverSold then OverSold else Double.NaN;
#plot DownSignal = if RSI crosses below OverBought then OverBought else Double.NaN;
#UpSignal.SetHiding(!showBreakoutSignals);
#DownSignal.SetHiding(!showBreakoutSignals);
#RSI.DefineColor("OverBought", GetColor(5));
#RSI.DefineColor("Normal", GetColor(7));
#RSI.DefineColor("OverSold", GetColor(1));
#RSI.AssignValueColor(if RSI > over_Bought then RSI.color("OverBought") else if RSI < over_Sold then RSI.color("OverSold") else RSI.color("Normal"));
#OverSold.SetDefaultColor(GetColor(8));
#OverBought.SetDefaultColor(GetColor(8));
#UpSignal.SetDefaultColor(Color.UPTICK);
#UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#DownSignal.SetDefaultColor(Color.DOWNTICK);
#DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#-----------------------------
#RSI over 70 Yellow
#RSI 50-70: Green
#RSI 30-50: Orange
#RSI under 30: Red
input rsi_level1 = 30;
input rsi_level2 = 50;
input rsi_level3 = 70;
DefineGlobalColor("color1", color.red);
DefineGlobalColor("color2", color.orange);
DefineGlobalColor("color3", color.green);
DefineGlobalColor("color4", color.yellow);
# GlobalColor("color1")
#DefineGlobalColor("Gray", CreateColor(28, 28, 28)); #Gray
# GlobalColor("x")
def zone1 = (rsi < rsi_level1);
def zone2 = (rsi >= rsi_level1 and rsi < rsi_level2);
def zone3 = (rsi >= rsi_level2 and rsi < rsi_level3);
def zone4 = (rsi >= rsi_level3);
#---------------------------
#yellow outline, addchart()
#https://usethinkscript.com/threads/draw-a-box-around-the-overnight-session-on-futures-intraday-charts.2087/page-3#post-58481
#sleepyz
input bar_outline_factor = 2;
#hint bar_outline_factor: adjust the height of the colored border. \n0 = original candle height. multiples of ticksize.
def o = open;
def h = high;
def l = low;
def c = close;
def nan = Double.NaN;
def h0 = h + TickSize() * bar_outline_factor;
def l0 = l - TickSize() * bar_outline_factor;
#-----------------------
# zone1 rsi<30
def h1 = if zone1 then h0 else nan;
def l1 = if zone1 then l0 else nan;
# hollow , open = high , close = low , is hollow
def o1 = if zone1 then h1 else nan;
def c1 = if zone1 then l1 else nan;
# solid , open = low , close = high , is solid
#def o1 = if cond then l1 else nan;
#def c1 = if cond then h1 else nan;
AddChart(growcolor = GlobalColor("color1"), high = h1, low = l1, open = c1, close = o1, type = ChartType.CANDLE);
#-----------------------
# zone2 rsi , 30 to 50
def h2 = if zone2 then h0 else nan;
def l2 = if zone2 then l0 else nan;
# hollow , open = high , close = low , is hollow
def o2 = if zone2 then h2 else nan;
def c2 = if zone2 then l2 else nan;
AddChart(growcolor = GlobalColor("color2"), high = h2, low = l2, open = c2, close = o2, type = ChartType.CANDLE);
#-----------------------
# zone3 rsi , 50 to 70
def h3 = if zone3 then h0 else nan;
def l3 = if zone3 then l0 else nan;
# hollow , open = high , close = low , is hollow
def o3 = if zone3 then h3 else nan;
def c3 = if zone3 then l3 else nan;
AddChart(growcolor = GlobalColor("color3"), high = h3, low = l3, open = c3, close = o3, type = ChartType.CANDLE);
#-----------------------
# zone4 rsi > 70
def h4 = if zone4 then h0 else nan;
def l4 = if zone4 then l0 else nan;
# hollow , open = high , close = low , is hollow
def o4 = if zone4 then h4 else nan;
def c4 = if zone4 then l4 else nan;
AddChart(growcolor = GlobalColor("color4"), high = h4, low = l4, open = c4, close = o4, type = ChartType.CANDLE);
#----------------------
# RSI % chg , bar to bar
# show when RSI has changed by a percentage (20%)
# Example if RSI went from 30 to 36, I would like an arrow or other indicator to plot.
# plot arrows 1 tick away from border
def y = 1;
input min_rsi_change_percent = 20.0;
def rsiper = round(100*(rsi-rsi[1])/rsi[1],1);
plot zperup = if (rsiper >= min_rsi_change_percent) then (l0 - (TickSize() * y)) else na;
zperup.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zperup.SetDefaultColor(Color.green);
zperup.setlineweight(4);
zperup.hidebubble();
plot zperdwn = if (rsiper <= -min_rsi_change_percent) then (h0 + (TickSize() * y)) else na;
zperdwn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
zperdwn.SetDefaultColor(Color.red);
zperdwn.setlineweight(4);
zperdwn.hidebubble();
#----------------------
input test_lines = no;
addverticalline(test_lines and zone1, "-", GlobalColor("color1"));
addverticalline(test_lines and zone2, "-", GlobalColor("color2"));
addverticalline(test_lines and zone3, "-", GlobalColor("color3"));
addverticalline(test_lines and zone4, "-", GlobalColor("color4"));
#