#k_rsi_colors
#Keltner Band with RSI
#based on Bollinger Band with RSI coded by Halcyonguy, so all credit goes to Halcyonguy
#https://usethinkscript.com/threads/bollinger-band-with-rsi.17083/
##########################
def na = double.nan;
def bn = barnumber();
input price = close;
input color_the_bars = yes;
input show_arrows = yes;
#------------------------------
#keltner
input factor2 = 2.00;
input length = 20;
input trlength = 10;
input Avgtype_ma = AverageType.EXPONENTIAL;
input Avgtype_tr = AverageType.WILDERS;
def tr = MovingAverage(Avgtype_tr, TrueRange(high, close, low), trlength);
def Average = MovingAverage(Avgtype_ma, price, length);
def Upper_Band2 = Average + tr * factor2;
def Lower_Band2 = Average - tr * factor2;
input show_kelt_lines = yes;
plot zkup = if show_kelt_lines then upper_band2 else na;
plot zkmid = if show_kelt_lines then average else na;
plot zklo = if show_kelt_lines then lower_band2 else na;
zkup.SetDefaultColor(GetColor(5));
zkmid.SetDefaultColor(GetColor(1));
zklo.SetDefaultColor(GetColor(0));
#-------------------------------
# rsi
input rsi_len = 14;
input rsi_over_bought = 70;
input rsi_over_Sold = 30;
input rsi_avg_type = AverageType.WILDERS;
def NetChgAvg = MovingAverage(rsi_avg_type, price - price[1], rsi_len);
def TotChgAvg = MovingAverage(rsi_avg_type, AbsValue(price - price[1]), rsi_len);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);
#------------------------------
def long_rule1 = (close < lower_band2);
def long_rule2 = (rsi < rsi_over_Sold);
def short_rule1 = (close > upper_band2);
def short_rule2 = (rsi > rsi_over_bought);
def long = long_rule1 and long_rule2;
def short = short_rule1 and short_rule2;
#------------------------------
def y = 0.002;
plot zlong = if show_arrows and long then (low * (1 - y)) else na;
zlong.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zlong.SetDefaultColor(Color.green);
zlong.setlineweight(1);
zlong.hidebubble();
plot zshort = if show_arrows and short then (high * (1 + y)) else na;
zshort.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
zshort.SetDefaultColor(Color.red);
zshort.setlineweight(1);
zshort.hidebubble();
#------------------------------
AssignPriceColor(if !color_the_bars then color.current
else if long then color.green
else if short then color.red
else color.gray);
#