Need help coding RSI strategy

Trader_Vic

New member
VIP
I am trying to write a simple strategy using RSI as an indicator. I would like code that 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. am trying to use a recursive function on TOS for this. The logic seems to work for the immediate candle (i.e if it moves +5 points next candle), however, I would like to get some help on how to code the same logic from (lowest RSI value + 5) happens in 2nd, 3rd, 4th, 5th or ...Nth candle
 
Solution
I am trying to write a simple strategy using RSI as an indicator. I would like code that 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. am trying to use a recursive function on TOS for this. The logic seems to work for the immediate candle (i.e if it moves +5 points next candle), however, I would like to get some help on how to code the same logic from (lowest RSI value + 5) happens in 2nd, 3rd, 4th, 5th or ...Nth candle

an updated version,

i fixed an issue and added , looking at over bought level.

the purple trigger line, used to start right away, which could cause bad buys. it caused signals before the lowest rsi...
I am trying to write a simple strategy using RSI as an indicator. I would like code that 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. am trying to use a recursive function on TOS for this. The logic seems to work for the immediate candle (i.e if it moves +5 points next candle), however, I would like to get some help on how to code the same logic from (lowest RSI value + 5) happens in 2nd, 3rd, 4th, 5th or ...Nth candle

EDIT
see post#3 for an updated version

---------

RSI data,
. each time RSI crosses below oversold, and stays below oversold, find the lowest value of rsi.
. calculate a trigger level that is some 'rise amount' plus the lowest RSI.
. when RSI crosses above the trigger level, draw green arrow and 2 vertical lines.

draw gray line at the lowest RSI level
draw yellow arrow at lowest RSI levels
draw a purple line at the trigger level


Code:
# rsi_below20_plus_5_01

#https://usethinkscript.com/threads/need-help-coding-rsi-strategy.14623/
#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;

# RSI
# TD Ameritrade IP Company, Inc. (c) 2007-2023
input length = 14;
input over_Bought = 80;
input over_Sold = 20;
input price = close;
input averageType = AverageType.WILDERS;
#input showBreakoutSignals = no;

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;

plot RSI = 50 * (ChgRatio + 1);
plot OverSold = over_Sold;
plot OverBought = over_Bought;
#plot UpSignal = if RSI crosses above OverSold then OverSold else Double.NaN;
#plot DownSignal = if RSI crosses below OverBought then OverBought else Double.NaN;
#UpSignal.SetHiding(!showBreakoutSignals);
#DownSignal.SetHiding(!showBreakoutSignals);
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));
#UpSignal.SetDefaultColor(Color.UPTICK);
#UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#DownSignal.SetDefaultColor(Color.DOWNTICK);
#DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

#---------------------------------

def na = double.nan;
def bn = barnumber();
def big = 99999;
def loop = 200;

input rsi_rise_amount = 5.0;

# 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;
if bn == 1 then {
  os_low = 0;
  os_lowbn = 0;
  os_low_plusy = 0;

} else if rsi crosses above os_low_plusy[1] then {
  os_low = if rsi < over_Sold then os_low[1] else 0;
  os_lowbn = 0;
  os_low_plusy = 0;

} else if rsi crosses above over_Sold then {
  os_low = 0;
  os_lowbn = 0;
  os_low_plusy = os_low_plusy[1];

} else if rsi crosses below over_Sold then {
# when rsi crosses below 20, find the lowest value of rsi, and the bn
 os_low = (fold i = 0 to loop
 with p = big
 while getvalue(rsi, -i) < over_Sold
 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 = os_low + rsi_rise_amount;

} else {
  os_low = os_low[1];
  os_lowbn = os_lowbn[1];
  os_low_plusy = os_low_plusy[1];
}


#---------------------------------
# plot arrow on all 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();


input show_lowest_levels = yes;
plot z1 = if show_lowest_levels and os_low > 0 then os_low else na;
plot z2 = if show_lowest_levels and os_low_plusy > 0 then os_low_plusy else na;
z1.SetDefaultColor(Color.light_gray);
z1.hidebubble();
z2.SetPaintingStrategy(PaintingStrategy.horizontal);
z2.SetDefaultColor(Color.magenta);
z2.hidebubble();


# plot arrow when rsi crosses above lowest + y
plot w = if rsi crosses above os_low_plusy then rsi else na;
w.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
w.SetDefaultColor(Color.green);
w.setlineweight(3);
w.hidebubble();


def vert = !isnan(w) or !isnan(w[1]);
addverticalline(vert, " ", color.green);

#---------------------------------
#


GLD 15min
over sold = 24
rise amount = 7.0
qcMOoF0.jpg
 
Last edited:
I am trying to write a simple strategy using RSI as an indicator. I would like code that 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. am trying to use a recursive function on TOS for this. The logic seems to work for the immediate candle (i.e if it moves +5 points next candle), however, I would like to get some help on how to code the same logic from (lowest RSI value + 5) happens in 2nd, 3rd, 4th, 5th or ...Nth candle

an updated version,

i fixed an issue and added , looking at over bought level.

the purple trigger line, used to start right away, which could cause bad buys. it caused signals before the lowest rsi level bar.
now the line doesn't start until the lowest or highest rsi bar.


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

GLD 30min
S2lC2JH.jpg
 
Solution

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
366 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top