# rsi_beyond_osob_then_rev_02
#https://usethinkscript.com/threads/need-help-coding-rsi-strategy.14623/
#Need help coding RSI strategy
#Trader_Vic Feb 25, 2023
#records the value of RSI when it hits below 20,
# and would like to print a signal when RSI value moves up +5 points from the lowest value recorded below 20.
#---------------------------------
declare lower;
def na = double.nan;
def bn = barnumber();
def big = 99999;
def loop = 200;
input rsi_rise_amount = 5.0;
#---------------------------------
# RSI
# TD Ameritrade IP Company, Inc. (c) 2007-2023
input rsi_length = 14;
input over_Bought = 80;
input over_Sold = 20;
def price = close;
input averageType = AverageType.WILDERS;
def NetChgAvg = MovingAverage(averageType, price - price[1], rsi_length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), rsi_length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
plot RSI = 50 * (ChgRatio + 1);
plot OverSold = over_Sold;
plot OverBought = over_Bought;
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));
#---------------------------------
#/////////////////////////////////
# rsi crosses below oversold, then reverses
# if rsi crossed below 20,
# find the lowest value of rsi, and the bn
# then look for rsi crossing above (lowest + rise amount)
def os_low;
def os_lowbn;
def os_low_plusy;
def test1;
if bn == 1 then {
os_low = 0;
os_lowbn = 0;
os_low_plusy = big;
test1 = 1;
} else if rsi crosses above os_low_plusy[1] and bn >= os_lowbn[1] then {
os_low = 0;
os_lowbn = 0;
os_low_plusy = big;
test1 = 2;
} else if rsi crosses above over_Sold then {
os_low = 0;
os_lowbn = 0;
os_low_plusy = if rsi < os_low_plusy[1] then os_low_plusy[1] else 0;
test1 = 3;
} else if rsi crosses below over_Sold then {
# when rsi crosses below 20, find the lowest value of rsi, while rsi < os
os_low = (fold i = 0 to loop
with p = big
while getvalue(rsi, -i) < over_Sold and !isnan(getvalue(close, -i))
do (if getvalue(rsi, -i) < p then getvalue(rsi, -i) else p));
os_lowbn = bn + (fold j = 0 to loop
with q
while getvalue(rsi, -j) != os_low
do q + 1);
os_low_plusy = big;
test1 = 4;
} else {
os_low = os_low[1];
os_lowbn = os_lowbn[1];
os_low_plusy = if os_lowbn[1] > 0 and bn >= os_lowbn[1] then (os_low + rsi_rise_amount) else big;
test1 = 5;
}
#---------------------------------
# plot horz lines at lowest rsi levels
plot z1 = if os_low > 0 then os_low else na;
z1.SetDefaultColor(Color.light_gray);
z1.hidebubble();
# plot purple line at lowest + y level
plot z2 = if os_low_plusy < big then os_low_plusy
else if rsi crosses above os_low_plusy[1] then os_low_plusy[1]
else na;
z2.SetPaintingStrategy(PaintingStrategy.horizontal);
z2.SetDefaultColor(Color.magenta);
z2.setlineweight(2);
z2.hidebubble();
# plot yellow arrow on lowest rsi values, below over sold level
plot x = if os_lowbn == bn then rsi else na;
x.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
x.SetDefaultColor(Color.yellow);
x.setlineweight(2);
x.hidebubble();
# plot green arrow when rsi crosses above lowest + y
plot u = if rsi crosses above os_low_plusy[1] then rsi else na;
u.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
u.SetDefaultColor(Color.green);
u.setlineweight(3);
u.hidebubble();
# 2 green vert lines on signal bar
def vertu = !isnan(u) or !isnan(u[1]);
addverticalline(vertu, " ", color.green);
#---------------------------------
#/////////////////////////////////
# rsi crosses above overbought, then reverses
# if rsi crossed above 80,
# find the highest value of rsi, and the bn
# then look for rsi crossing below (highest - rise amount)
def ob_hi;
def ob_hibn;
def ob_hi_plusy;
def test2;
if bn == 1 then {
ob_hi = 0;
ob_hibn = 0;
# ob_hi_plusy = big;
ob_hi_plusy = 0;
test2 = 1;
} else if rsi crosses below ob_hi_plusy[1] and bn >= ob_hibn[1] then {
ob_hi = 0;
ob_hibn = 0;
# ob_hi_plusy = big;
ob_hi_plusy = 0;
test2 = 2;
} else if rsi crosses below over_bought then {
ob_hi = 0;
ob_hibn = 0;
ob_hi_plusy = if rsi > ob_hi_plusy[1] then ob_hi_plusy[1] else 0;
test2 = 3;
} else if rsi crosses above over_bought then {
# when rsi crosses above 80, find the highest value of rsi, while rsi > ob
ob_hi = (fold m = 0 to loop
# with r = big
with r = 0
while getvalue(rsi, -m) > over_bought and !isnan(getvalue(close, -m))
do (if getvalue(rsi, -m) > r then getvalue(rsi, -m) else r));
ob_hibn = bn + (fold n = 0 to loop
with s
while getvalue(rsi, -n) != ob_hi
do s + 1);
# ob_hi_plusy = big;
ob_hi_plusy = 0;
test2 = 4;
} else {
ob_hi = ob_hi[1];
ob_hibn = ob_hibn[1];
# ob_hi_plusy = if ob_hibn[1] > 0 and bn >= ob_hibn[1] then (ob_hi + rsi_rise_amount) else big;
ob_hi_plusy = if ob_hibn[1] > 0 and bn >= ob_hibn[1] then (ob_hi - rsi_rise_amount) else 0;
test2 = 5;
}
#---------------------------------
# plot horz lines at highest rsi levels
plot z3 = if ob_hi > 0 then ob_hi else na;
z3.SetDefaultColor(Color.light_gray);
z3.hidebubble();
# plot purple line at highest - y level
plot z4 = if ob_hi_plusy > 0 then ob_hi_plusy
else if rsi crosses below ob_hi_plusy[1] then ob_hi_plusy[1]
else na;
z4.SetPaintingStrategy(PaintingStrategy.horizontal);
z4.SetDefaultColor(Color.magenta);
z4.setlineweight(2);
z4.hidebubble();
# plot yellow arrow on highest rsi values, above overbought level
plot x2 = if ob_hibn == bn then rsi else na;
x2.SetPaintingStrategy(PaintingStrategy.ARROW_down);
x2.SetDefaultColor(Color.yellow);
x2.setlineweight(2);
x2.hidebubble();
# plot green arrow when rsi crosses above lowest + y
plot d = if rsi crosses below ob_hi_plusy[1] then rsi else na;
d.SetPaintingStrategy(PaintingStrategy.ARROW_down);
d.SetDefaultColor(Color.red);
d.setlineweight(3);
d.hidebubble();
# 2 red vert lines on signal bar
def vertd = !isnan(d) or !isnan(d[1]);
addverticalline(vertd, " ", color.red);
#/////////////////////////////////
addchartbubble(0, rsi*1.5,
test1 + "\n" +
bn + "\n" +
os_lowbn[1] + "\n" +
rsi + " r\n" +
os_low_plusy + " y\n"
# (rsi crosses above os_low_plusy)
, color.yellow, yes);
#