• LIMITED TIME ONLY: Take $40 off VIP with code: BTD40.

RSI Prediction!

drkellog

New member
Does anyone know of a thinkscript that will show you what the price would have to be in order for the RSI to be less than 30 on the current candle? Example... The RSI is currently 42 but i dont want to place an order until the RSI is 30 on the same candle. If I am trading a 4 hour candle I dont really want to wait an entire 4 hours watching until it maybe hits my area of interest for a starter position. Using a standard 14 period RSI. I would like to set my orders ahead of time. I dont really want to have to sit there and physically watch until it gets there. In theory i would like to place several orders on different stocks while i am doing other things then i can check on them every now and then and revise the orders with the new predictive RSI values. I think this would help a lot of people on this forum if someone is feeling up to the challenge. Thank you :)
 
Last edited:

halcyonguy

Well-known member
VIP
Lifetime
Does anyone know of a thinkscript that will show you what the price would have to be in order for the RSI to be less than 30 on the current candle? Example... The RSI is currently 42 but i dont want to place an order until the RSI is 30 on the same candle. If I am trading a 4 hour candle I dont really want to wait an entire 4 hours watching until it maybe hits my area of interest for a starter position. Using a standard 14 period RSI. I would like to set my orders ahead of time. I dont really want to have to sit there and physically watch until it gets there. In theory i would like to place several orders on different stocks while i am doing other things then i can check on them every now and then and revise the orders with the new predictive RSI values. I think this would help a lot of people on this forum if someone is feeling up to the challenge. Thank you

this version requires the user to change an input number , on the edit studies window.

input a % price factor number,
on the last bar,
calculate a price that will produce the desired RSI number.

draw labels of values
on the last bar,
..draw a new line for the desired RSI
..draw a bubble with values


Code:
# rsi_predict_price_00c2

# calc a price, that will produce the desired RSI number.
# input a % factor number, to change the price.

input price_factor_percent = 99.0;
input desired_rsi_low_level = 30;
input show_labels = yes;
input values_bubble = yes;


# ---------------------------------------
# RSI
# TD Ameritrade IP Company, Inc. (c) 2007-2021
declare lower;
input length = 14;
input over_Bought = 70;
input over_Sold = 30;
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 lastbarbn = highestall( if( !isnan(close), bn, 0));
def lastbar = if lastbarbn == bn then 1 else 0;


# on last bar,  calc price for desired rsi #
def NetChgAvg2;
def TotChgAvg2;
def ChgRatio2;
def rsi2;
def i;
if bn == lastbarbn then {
i = price_factor_percent;
#i = 98.0;

  NetChgAvg2 = MovingAverage(averageType, (price * (i/100)) - price[1], length);
  TotChgAvg2 = MovingAverage(averageType, AbsValue((price * (i/100)) - price[1]), length);
  ChgRatio2 = if TotChgAvg2 != 0 then NetChgAvg2 / TotChgAvg2 else 0;
  RSI2 = 50 * (ChgRatio2 + 1);

} else {
# don't change this i variable, leave at 100
  i = 100.0;
  NetChgAvg2 = MovingAverage(averageType, (price * (i/100)) - price[1], length);
  TotChgAvg2 = MovingAverage(averageType, AbsValue((price * (i/100)) - price[1]), length);
  ChgRatio2 = if TotChgAvg2 != 0 then NetChgAvg2 / TotChgAvg2 else 0;
  RSI2 = 50 * (ChgRatio2 + 1);
}

input test1_bubbles = yes;
addchartbubble(test1_bubbles and !isnan(close), 30, rsi2 + "\n$" + (price * (i/100)) + "\n" + i + "%" , color.yellow, yes);

# show_labels
addlabel(show_labels, "input a % to change the RSI number.  i , " + i + "%" , color.green);
addlabel(show_labels, "desired rsi: " + desired_rsi_low_level , color.cyan);
addlabel(show_labels, "adjusted rsi level " +  round(rsi2, 1), color.yellow);
addlabel(show_labels, "price  $" + round(price * (i/100),2) , color.yellow);

plot z1 = 0;
z1.SetDefaultColor(Color.gray);
plot z2 = 100;
z2.SetDefaultColor(Color.gray);

# plot a line from prev rsi to alt rsi #

#def last2 = (lastbarbn - bn) >=2  ?

def last = ( !isnan(close[0]) and isnan(close[-1]) );
def prev = ( !isnan(close[-1]) and isnan(close[-2]) );

#def newrsiline = ( last or last[1] );
plot w = if (last or prev) then rsi2 else na;
w.SetDefaultColor(Color.cyan);

addchartbubble(values_bubble and last, rsi2*0.94, round(rsi2,1) + "  / $" + round(price * (i/100),2) + "  / i  " + i + "%" , color.yellow, no);
#


Et55ZAs.jpg

hal_predrsi
-------------

i have started on the next version, using a fold loop, to automatically calculate the required price, for the desired RSI number.
but, so far, it doesn't work.
rsi2 is supposed to equal the count of k's that are < desired RSI #. (similar to i in the first ver.)
------------
this goes in 1st half of if-then ( if bn == lastbarbn then { )
rsi2 = fold k = 1 to 100
with p
do p + (if (50 * ( (( MovingAverage(averageType, ((price * (k/100)) - price[1]), length))/( MovingAverage(averageType, AbsValue( (price * (k/100)) - price[1]), length))) + 1)) < desired_rsi_low_level then 1 else 0);
 
Last edited:

cfire23

New member
sorry, i have not. maybe in a week or two i will get back to it.
halcyonguy,

would you code a thinkscript that predicts moving average intersection? for example: ma(9) decreases and crosses below ma(20) but then begins increasing back up toward ma(20). the requested script would alert/identify that ma(9) had stopped decreasing, started increasing, and predicts/projects based on upward movement of potential crossover with ma(20).

one of the most challenging aspects is trying to identify a stock before move up/ and or the bottom. it is very easy to see AFTER the move up/gain that the ma(9) was increasing and crossed over ma(20). the intention of the prediction and/or projection script is to identify that ma(9) has stopped decreasing, turned up, and if it continues increasing it will crossover the ma(20).

great job coding the RSI prediction. this could definitely be applied to other indicators but i do not possess the necessary coding / math skills to make it happen. appreciate any and all assistance regarding moving average crossover prediction.

best regards,
 

halcyonguy

Well-known member
VIP
Lifetime
halcyonguy,

would you code a thinkscript that predicts moving average intersection? for example: ma(9) decreases and crosses below ma(20) but then begins increasing back up toward ma(20). the requested script would alert/identify that ma(9) had stopped decreasing, started increasing, and predicts/projects based on upward movement of potential crossover with ma(20).

one of the most challenging aspects is trying to identify a stock before move up/ and or the bottom. it is very easy to see AFTER the move up/gain that the ma(9) was increasing and crossed over ma(20). the intention of the prediction and/or projection script is to identify that ma(9) has stopped decreasing, turned up, and if it continues increasing it will crossover the ma(20).

great job coding the RSI prediction. this could definitely be applied to other indicators but i do not possess the necessary coding / math skills to make it happen. appreciate any and all assistance regarding moving average crossover prediction.

best regards,

this predicts where 2 signals would theoretically cross in the future, not where the price will go to.

find if 2 lines will cross in the future, if they were extended.
. use the difference between the close of 2 bars, to calculate the slope.
. can use an offset to look at a bar farther back, not just previous bar.
. extend dotted lines from the end of 2 signal lines.
. labels display several stats, and the price of crossing, and how many bars out.



Ruby:
# predict_crossing_00d

# find if 2 lines will cross in the future, if they were extended.
# halcyonguy  12/2021

def na = double.nan;
def bn = barnumber();

def lastbar = !isnan(close[0]) and isnan(close[-1]);
def lastbarbn = if bn == 1 then na else if lastbar then bn else lastbarbn[1];
# ------------------------------------------------------
# 2 averages for testing

input avg1_len = 9;
input avg1_type =  AverageType.simple;
def ma1 = MovingAverage(avg1_type, close, avg1_len);

input avg2_len = 20;
input avg2_type =  AverageType.simple;
def ma2 = MovingAverage(avg2_type, close, avg2_len);

plot z1 = ma1;
z1.setdefaultcolor(color.cyan);
plot z2 = ma2;
z2.setdefaultcolor(color.yellow);

addlabel(1, "avg1 len " + avg1_len, color.yellow);
addlabel(1, "avg2 len " + avg2_len, color.yellow);
# ------------------------------------------------------
#  set 2 formulas to sig1 and sig2
def sig1 = ma1;
def sig2 = ma2;

# calc close chg from a bar,  x bars back
input  barsback_for_offest_for_slope = 1;
def bb = barsback_for_offest_for_slope;
def s1chg = if !isnan(close) then (sig1[0] - sig1[bb])/bb else s1chg[1];
def s2chg = if !isnan(close) then (sig2[0] - sig2[bb])/bb else s2chg[1];

#check if 2 signals are moving towards each other , consolidating
def consol1 = if (sig1 > sig2 and s1chg < s2chg) then 1 else 0;
def consol2 = if (sig1 < sig2 and s1chg > s2chg) then 1 else 0;
def consol = (consol1 or consol2);
def notconsol = (!consol1 or !consol2);

def consolcnt = if ( !consol[1] and consol) then 1 else (consolcnt[1] + 1);
def wideningcnt = if ( consol[1] and !consol) then 1 else (wideningcnt[1] + 1);

addlabel(1, "bars back for offest for slope " + bb, color.lime);

addlabel(consol, "consolidating for " + consolcnt + " bars", color.yellow);
addlabel(!consol, "widening for " + wideningcnt + " bars", color.magenta);

# ------------------------------------
# bars till a crossing
def bars_x = round( (sig2 - sig1) / (s1chg - s2chg), 0);

# future price levels
def f1 = round((sig1 + ( bars_x * s1chg)), 2);
def f2 = round((sig2 + ( bars_x * s2chg)), 2);

addlabel(1, "#1 chg: " + s1chg, color.yellow);
addlabel(1, "#2 chg: " + s2chg, color.yellow);

addlabel(consol, "#1 cross $: " + f1, color.cyan);
addlabel(consol, "#2 cross $: " + f2, color.cyan);
#--------------------

def cross_price = if (!isnan(close[-1]) or notconsol) then na
 else if lastbar then (sig1 + ( bars_x * s1chg))
 else cross_price[1];

plot z4 = cross_price;
z4.SetStyle(Curve.MEDIUM_DASH);
z4.SetDefaultColor(Color.cyan);
z4.setlineweight(1);
z4.hidebubble();

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

#if last bar then  bn of crossing
def bnx = if lastbar then (bars_x + bn) else bnx[1];

addlabel(consol, "last# " + lastbarbn + " cross # " + bnx , color.pink);

# extended lines , after lastbar
def extline_offset = 1;
def ex1 = if bn == 1 then na else
   if lastbar[extline_offset] then sig1[extline_offset] + s1chg[extline_offset]
   else (ex1[1] + s1chg);

plot ext1 = ex1;
ext1.SetStyle(Curve.MEDIUM_DASH);
ext1.setdefaultcolor(color.white);
#ext1.setlineweight(1);
ext1.hidebubble();


# start on lastbar
#def ex2 = if bn == 1 then na else
#   if lastbar then sig2
#   else (ex2[1] + s2chg);

# start on a bar after lastbar, so there is a gap between lines
def ex2 = if bn == 1 then na else
   if lastbar[extline_offset] then sig2[extline_offset] + s2chg[extline_offset]
   else (ex2[1] + s2chg);

plot ext2 = ex2;
ext2.SetStyle(Curve.MEDIUM_DASH);
ext2.setdefaultcolor(color.white);
#ext2.setlineweight(1);
ext2.hidebubble();

# draw vert line at time of lines crossing
addverticalline(bn == bnx , "signal cross", color.yellow);

def bars_xb = round(bars_x,0);
AddLabel(consol, "signals 1 & 2 will cross in " + bars_xb + " bars", Color.YELLOW);
AddLabel(!consol, "signals 1 & 2 will not cross", Color.magenta);

#--------------------------------
# test data
input test1 = no;
addchartbubble( test1, low *0.992, "C " + consolcnt + "\nW " + wideningcnt, color.cyan, no);
input test2 = no;
addchartbubble(test2, cross_price, bn + "\n" + ex1 + "\n" + ex1[1] + "\n" + s1chg , color.cyan, no);
input test3 = no;
addchartbubble(test3, cross_price, bn + "\n" + ex1 + "\n" + ex1[1] + "\n" + s1chg , color.cyan, no);
input test4 = no;
addchartbubble(test4, cross_price, bn + "\n" + bnx + "\n" + 0 , color.cyan, no);

# ==================================

# https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/AverageType
#  EXPONENTIAL
#  HULL
#  SIMPLE
#  WEIGHTED
#  WILDERS


# ==============================
# derive a formula
# ----------------
# this is the result formula from below
#   signal diff is on top and bar to bar changes are on bottom.
#     bars_x = (sig2 - sig1) / (s1chg - s2chg)
# ----------------
#  if sig1 > sig2 then,
#  look for when sig1 < sig2q
#
# (sig1 + ( bars_x * s1chg)) <= (sig2 + ( bars_x * s2chg))
#
# rename vars and simplify
# a=sig1
# b=sig2
# a2=s1chg
# b2=s2chg
# x=bars_x

# (a + ( x * a2)) <= (b + ( x * b2))
# a2*x + a <= b2*x + b
# a2*x - b2*x <= b - a
# (a2 - b2) / x <= b - a
# (a2 - b2)  <= (b - a ) * x
# (a2 - b2) / (b - a ) <= x

# replace var names
# invert formula, so signal diff is on top and bar to bar changes are on bottom.
#def bars_x = round( (sig2 - sig1) / (s1chg - s2chg), 1);
#=============================
#


some examples using lengths of 55 and 123

TJX
JaJBmW6.jpg


BABA
xxXZ0t9.jpg

hal_cross
 
Last edited:

Similar threads

Top