# Hoping to get help with this RSI label

#### Vorlauf

##### New member
As is, it only shows RSI rising if above 50 and RSI falling if below 50. Thats only half the story. If it goes from 70 to 60, its still above 50 so shows RSI rising. Possible to have it show for example of RSI rising, it be above 50 and higher than 3 candles before? Same with RSI falling, below 50 and rsi higher 3 candles before?
input n = 21;
def RSI_ = RSI(14, 70, 30, close, AverageType.WILDERS);
def RSIUP = RSI_ > 50 and RSI_ > RSI_[1];
def RSIDN = RSI_ < 50 and RSI_ < RSI_[1];
addlabel(1, "RSI : " + Round(RSI_,2) + (if RSIUP then " RISING"
else if RSIDN then " FALLING"
else " NEUTRAL"),
if RSIUP then Color.GREEN else if RSIDN then Color.RED else Color.YELLOW);

#### halcyonguy

##### Active member
VIP
here is a study to check what you mentioned at the end of your post.

RSI rising
....RSI above 50 and higher than all 3 previous candles
RSI falling
....RSI below 50 and lower than all 3 previous candles

this doesn't test if the previous RSI values were increasing, each higher than the previous.

i reduced the elements of the rules into the simplest formulas, so that they could be combined into other conditions. ( like above 50 and dropping)
i like to copy the referenced code into a study, if it isn't too many lines.

Ruby:
``````# RSIrangelabel_01
# ---------------------
# halcyonguy
# 21-06-16
#
# ---- study rules ----
# RSI rising,
#   RSI above 50 and higher than all 3 prev candles
# RSI falling,
#   RSI below 50 and lower than all 3 prev candles
# -----------------------------------------

# ----------------------------------------
# copy relevant 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;
def rsihighest = highest(rsi_[1], compare_prev_bars);
def rsilowest = lowest(rsi_[1], compare_prev_bars);

def rsihigherprev = ( rsi_ > rsihighest );
def rsilowerprev = ( rsi_ < rsilowest );

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"),
if RSIUP then Color.GREEN else if RSIDN then Color.RED else Color.YELLOW);

# --------------------------------
# test data
input show_hi_low_label = no;
addlabel(show_hi_low_label , "highest " + rsihighest + " lowest " + rsilowest , color.yellow);

#``````

Last edited:

#### halcyonguy

##### Active member
VIP
in the previous version, RSI rising could be when past RSI values were moving up and down, and still be less than the current RSI value.

in this version ( v02) , for it to be RSI rising, each RSI within the past x bars , has be be bigger than the previous. ( and vice versa for falling) default of x is , compare_prev_bars = 3
instead of using static code like this to detect just 1 quantity series of increasing values, x[0] > x[1] and x[1] > x[2] and ....
i used a fold loop, so it can work with any quantity of 'compare_prev_bars'

there are several options for turning on test data ( labels, arrows, numbers )
testing, 6/16 , extended hours on, WING 30min chart, shows RSI falling , after hours

Ruby:
``````#  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);
#``````

Last edited: