```
# RSIrangelabel_02
# -----------------------------------------
# halcyonguy
# 21-06-16
# v02 , checks series of RSI values, to see if prev is < next , or the opposite
# ---- study rules ----
# RSI rising,
# ....RSI above 50
# ....AND for the previous x RSI values, the previous value is < the next. a series of increasing values
# ......option to show arrows when this happens
#
# RSI falling,
# ....RSI below 50
# ....AND for the previous x RSI values, the previous value is > the next. a series of decreasing values
# ......option to show arrows when this happens
# -----------------------------------------
def na = double.nan;
# ----------------------------------------
# copy relevent RSI code from TD study
# RSI
input length = 21;
input price = close;
input averageType = AverageType.WILDERS;
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 mid = 50;
def RSIabove = (RSI_ > mid);
def RSIbelow = (RSI_ < mid);
input compare_prev_bars = 3;
# check if the prev x bars had increasing values
# count each RSI that is bigger than the previous
def upcnt = fold i = 0 to compare_prev_bars
with p
do p + ( if getvalue(rsi_, i) > getvalue(rsi_, i+1) then 1 else 0 );
# if the fold number matches compare_prev_bars, then all the bars are increasing
def upmatch = ( upcnt == compare_prev_bars);
# check if the prev x bars had decreasing values
# count each RSI that is smaller than the previous
def dwncnt = fold k = 0 to compare_prev_bars
with q
do q + ( if getvalue(rsi_, k) < getvalue(rsi_, k+1) then 1 else 0 );
# if the fold number matches compare_prev_bars, then all the bars are decreasing
def dwnmatch = ( dwncnt == compare_prev_bars);
def rsihighest = highest(rsi_[1], compare_prev_bars);
def rsilowest = lowest(rsi_[1], compare_prev_bars);
def rsihigherprev = ( rsi_ > rsihighest and upmatch);
def rsilowerprev = ( rsi_ < rsilowest and dwnmatch);
def rsiup = rsiabove and rsihigherprev;
def RSIdn = rsibelow and rsilowerprev;
addlabel(1, "RSI : " + Round(RSI_, 2) + (if RSIUP then " RISING"
else if RSIDN then " FALLING"
else " NEUTRAL") + (" over " + compare_prev_bars + " bars"),
if RSIUP then Color.GREEN else if RSIDN then Color.RED else Color.YELLOW);
# --------------------------------
# test data
input arrows_incr_or_decr_series_of_RSI = yes;
plot upmatch2 = if arrows_incr_or_decr_series_of_RSI then upmatch else na;
upmatch2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
upmatch2.SetDefaultColor(Color.white);
plot dwnmatch2 = if arrows_incr_or_decr_series_of_RSI then dwnmatch else na;
dwnmatch2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
dwnmatch2.SetDefaultColor(Color.white);
input show_hi_low_label = no;
addlabel(show_hi_low_label , "highest " + rsihighest + " lowest " + rsilowest , color.yellow);
addlabel(show_hi_low_label, "upcnt " + upcnt + " / prevbar " + compare_prev_bars, if upmatch then color.green else color.magenta);
input show_rsi_values_below_candles = no;
plot rsival = if show_rsi_values_below_candles then rsi_ else na;
rsival.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
#
```