#/ This source code is subject to the terms of the Mozilla Public License 2.0 at https://mo
#// © Decam9
#study(title = "Constance Brown Altered RSI", shorttitle = "CB Altered RSI", overlay = false)
#Converted and mod by Sam4Cok@Samer800 - 01/2024
Declare lower;
#//Plot the RSI
input selectRSI = {default Regular, Slow, Rapid, Harris, "Ehlers Smoothed", RSX};
input rsiSource = close; # "RSI Source"
input rsiLength = 14; # "RSI Length"
input signalLength = 9;
input ShortTrendLength = 100;
input LongTrendLength = 200;
def na = Double.NaN;
def last = isNaN(close);
#// rsx RSI
script rsi_rsx {
input src = close;
input len = 14;
def nRSI = rsi(Price=src, Length=len);
def src_out = 100 * src;
def mom0 = (src_out-src_out[1]);
def moa0 = AbsValue(mom0);
def Kg = 3 / (len + 2.0);
def Hg = 1 - Kg;
def f28 = CompoundValue(1, Kg * mom0 + Hg * f28[1], nRSI);
def f30 = CompoundValue(1, Hg * f30[1] + Kg * f28, nRSI);
def mom1 = f28 * 1.5 - f30 * 0.5;
def f38 = CompoundValue(1, Hg * f38[1] + Kg * mom1, nRSI);
def f40 = CompoundValue(1, Kg * f38 + Hg * f40[1], nRSI);
def mom2 = f38 * 1.5 - f40 * 0.5;
def f48 = CompoundValue(1, Hg * f48[1] + Kg * mom2, nRSI);
def f50 = CompoundValue(1, Kg * f48 + Hg * f50[1], nRSI);
def mom_out = f48 * 1.5 - f50 * 0.5;
def f58 = CompoundValue(1, Hg * f58[1] + Kg * moa0, nRSI);
def f60 = CompoundValue(1, Kg * f58 + Hg * f60[1], nRSI);
def moa1 = f58 * 1.5 - f60 * 0.5;
def f68 = CompoundValue(1, Hg * f68[1] + Kg * moa1, nRSI);
def f70 = CompoundValue(1, Kg * f68 + Hg * f70[1], nRSI);
def moa2 = f68 * 1.5 - f70 * 0.5;
def f78 = CompoundValue(1, Hg * f78[1] + Kg * moa2, nRSI);
def f80 = CompoundValue(1, Kg * f78 + Hg * f80[1], nRSI);
def moa_out = f78 * 1.5 - f80 * 0.5;
def rsiout = max(min((mom_out / moa_out + 1.0) * 50.0, 100), 0);
plot rsx = rsiout;
}
#// Slow RSI
script rsi_slo {
input src = close;
input per = 14;
def _rsival;
def nRSI = rsi(Price=src,Length= per);
def up = fold k = 0 to per with p do
if (src[k] - GetValue(src, k + 1)) > 0 then p + (src[k] - GetValue(src, k + 1)) else p;
def dn = fold i = 0 to per with q do
if (src[i] - GetValue(src, i + 1)) > 0 then q else q - (src[i] - GetValue(src, i + 1));
if (up + dn == 0) {
_rsival = CompoundValue(1, _rsival[1] + (1 / Max(per, 1)) * (50 - _rsival[1]), nRSI);
} else {
_rsival = CompoundValue(1, _rsival[1] + (1 / Max(per, 1)) * (100 * up / (up + dn) - _rsival[1]), nRSI);
}
plot out = _rsival;
}
# // Rapid RSI
script rsi_rap {
input src = close;
input per = 14;
def sump = fold k = 0 to per with p do
if (src[k] - GetValue(src, k + 1)) > 0 then p + (src[k] - GetValue(src, k + 1)) else p;
def sumn = fold i = 0 to per with q do
if (src[i] - GetValue(src, i + 1)) < 0 then q - (src[i] - GetValue(src, i + 1)) else q;
def out = if sumn > 0 then 100 - 100 / (1 + sump / sumn) else 50;
plot rsi_cut = out;
}
#// Harris RSI
script rsi_har {
input src = close;
input per = 14;
def avUp = fold k = 0 to per with p do
if (src[k] - GetValue(src, k + 1)) > 0 then p + (src[k] - GetValue(src, k + 1)) else p;
def avDn = fold i = 0 to per with q do
if (src[i] - GetValue(src, i + 1)) > 0 then q else q - (src[i] - GetValue(src, i + 1));
def Up = fold k1 = 0 to per with p1 do
if (src[k1] - GetValue(src, k1 + 1)) > 0 then p1 + 1 else p1;
def Dn = fold i1 = 0 to per with q1 do
if (src[i1] - GetValue(src, i1 + 1)) > 0 then q1 else q1 + 1;
def avgUp = if (up != 0) then avUp / up else avgUp[1];
def avgDn = if (dn != 0) then avDn / dn else avgDn[1];
def rs = if rs[1]==0 then 1 else
if avgDn != 0 then avgUp / avgDn else rs[1];
def out = 100-100 / (1.0 + rs);
plot rsi_har = out;
}
# //Ehlers' Smoothed RSI
script rsi_ehl {
input src = close;
input per = 14;
def bar_index = if isNaN(close) then bar_index[1] else bar_index[1] + 1;
def _src = if (bar_index > 2) then (src + 2 * src[1] + src[2]) / 4.0 else src;
def Up = fold k = 0 to per with p do
if (_src[k] - GetValue(_src, k + 1)) > 0 then p + (_src[k] - GetValue(_src, k + 1)) else p;
def Dn = fold i = 0 to per with q do
if (_src[i] - GetValue(_src, i + 1)) > 0 then q else q - (_src[i] - GetValue(_src, i + 1));
def out = 50 * (up-dn) / (up+dn) + 50;
plot rsi_ehl = out;
}
def Regular = rsi(Price = rsiSource,Length = rsiLength);
def Slow = rsi_slo(rsiSource, rsiLength);
def Rapid = rsi_rap(rsiSource, rsiLength);
def Harris = rsi_har(rsiSource, rsiLength);
def Ehlers = rsi_ehl(rsiSource, rsiLength);
def RSX = rsi_rsx(rsiSource, rsiLength);
def nRSI1;
Switch (selectRSI) {
Case Slow : nRSI1 = Slow;
Case Rapid : nRSI1 = Rapid;
Case Harris : nRSI1 = Harris;
Case "Ehlers Smoothed" : nRSI1 = Ehlers;
Case RSX : nRSI1 = RSX;
Default : nRSI1 = Regular;
}
def nRSI = nRsi1;
def Signal = ExpAverage(nRsi, signalLength);
plot rsiLine = nRsi; # "RSI"
plot sigLine = Signal; # "Signal"
rsiLine.SetDefaultColor(Color.MAGENTA);
sigLine.SetDefaultColor(Color.GRAY);
#//Trend Determination
def Ma1 = ShortTrendLength;
def Ma2 = LongTrendLength;
def isUptrend = Average(close,Ma1) >= Average(close,Ma2);
#//Bullzone Resistance
plot BullOBT = if isUptrend then 90 else na; # "Bullish Resistance"
plot BullOBB = if isUptrend then 80 else na; # "Bullish Resistance"
#//Bearzone Resistance
plot BearOBT = if isUptrend then na else 65; # "Bearish Resistance"
plot BearOBB = if isUptrend then na else 55; # "Bearish Resistance"
#//Bullzone Support
plot BullOST = if isUptrend then 50 else na; # "Bullish Support"
plot BullOSB = if isUptrend then 40 else na; # "Bullish Support"
#//Bearzone Support
plot BearOST = if isUptrend then na else 30; # "Bearish Support"
plot BearOSB = if isUptrend then na else 20; # "Bearish Support"
BullOBT.SetDefaultColor(Color.GREEN);
BullOBB.SetDefaultColor(Color.DARK_GREEN);
BullOST.SetDefaultColor(Color.DARK_GREEN);
BullOSB.SetDefaultColor(Color.GREEN);
BearOBT.SetDefaultColor(Color.RED);
BearOBB.SetDefaultColor(Color.DARK_RED);
BearOST.SetDefaultColor(Color.DARK_RED);
BearOSB.SetDefaultColor(Color.RED);
AddCloud(if nRsi >= BullOBT then nRsi else na, BullOBT, Color.GREEN);
AddCloud(if nRsi >= BullOBB then nRsi else na, BullOBB, Color.DARK_GREEN);
AddCloud(if nRsi <= BearOSB then BearOSB else na, nRsi, Color.RED);
AddCloud(if nRsi <= BearOST then BearOST else na, nRsi, Color.DARK_RED);
#-- End of CODE