# Reverse Engineering RSI

##### New member
Hello, I'm new to the forum. I base a lot of my trades off extended RSI levels ( to the up or downside ) when they come in contact with Fibonacci levels. I scale in and try to average my price as close to the current price when the RSI recovers (returns above/below 30 or 70). Right now I am guessing how much farther the price will move before the reversal. Does anyone know how to calculate for example: If the RSI went from 30 down to 12, the price of the stock at 30 RSI is \$20, what would the price be when the RSI is 12? I'm trying to make my entries more precise. Does anyone know how to calculate this. I would love to have a chart that calculates this in TOS. Thanks in advance

##### New member
I guess to simplify the question, has anyone created a RS (relative strength) type average for real time use. RS = (Avg Gain)/(Avg Loss) for 14 day period on a stock.

##### New member
I believe I found the average to use to covert from a scale of 0 to 100 to the real price. I have averaging to points and dividing by 100 and multiplying the unknown distance on the RSI. It seems to be getting me close to the conversion. Here is the average i'm using which I found in the stock TOS RSI code:

Code:
``````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;

def RSI = 50 * (ChgRatio + 1);

plot TC = TotChgAvg / 100;``````

Any input would be appreciated or tell me to F off. Thanks

#### diazlaz

##### Well-known member
2019 Donor
VIP
look at the tos study ReverseEngineeringRSI

##### New member
Thanks diazlaz that was exactly what I was looking for. I figured it was included in TOS

#### sample

##### New member

Code:
``````#RevesedEngineeredRSI_Upper_Lowerbands mod by LAR
input length = 11;
input price = close;
input rsiValue = 65.0;
input smoothingType = {default Wilders, EMA};

def coeff = rsiValue / (100 - rsiValue);
def chg = price - price[1];
def diff;
switch (smoothingType) {
case Wilders:
diff =  (length - 1) * (WildersAverage(Max(-chg, 0), length) * coeff - WildersAverage(Max(chg, 0), length));
case EMA:
diff =  (length - 1) * (ExpAverage(Max(-chg, 0), length) * coeff - ExpAverage(Max(chg, 0), length)) / 2;
}

def value = price + if diff >= 0 then diff else diff / coeff;
plot RevEngRSI = compoundValue(1, value[1], Double.NaN);

RevEngRSI.SetDefaultColor(Color.BLACK);

input rsiValue1 = 35.0;

def coeff1 = rsiValue1 / (100 - rsiValue1);
def chg1 = price - price[1];
def diff1;
switch (smoothingType) {
case Wilders:
diff1 =  (length - 1) * (WildersAverage(Max(-chg1, 0), length) * coeff1 - WildersAverage(Max(chg1, 0), length));
case EMA:
diff1 =  (length - 1) * (ExpAverage(Max(-chg1, 0), length) * coeff1 - ExpAverage(Max(chg1, 0), length)) / 2;
}

def value1 = price + if diff1 >= 0 then diff1 else diff1 / coeff1;
plot RevEngRSI1 = compoundValue(1, value1[1], Double.NaN);

RevEngRSI1.SetDefaultColor(Color.BLACK);

input rsiValue2 = 50.0;
input smoothingType2 = {default Wilders, EMA};

def coeff2 = rsiValue2 / (100 - rsiValue2);
def chg2 = price - price[1];
def diff2;
switch (smoothingType2) {
case Wilders:
diff2 =  (length - 1) * (WildersAverage(Max(-chg2, 0), length) * coeff2 - WildersAverage(Max(chg2, 0), length));
case EMA:
diff2 =  (length - 1) * (ExpAverage(Max(-chg2, 0), length) * coeff2 - ExpAverage(Max(chg2, 0), length)) / 2;
}

def value2 = price + if diff2 >= 0 then diff2 else diff2 / coeff2;
plot RevEngRSI2 = compoundValue(1, value2[1], Double.NaN);

RevEngRSI2.SetDefaultColor(Color.BLACK);

AddCloud(revEngRSI2, data2 = RevEngRSI1, color2 = Color.red, color1 = Color.red);``````

##### New member
Thanks cpyleitz, that's a nice addition.

#### Jerseystranger

##### New member
I wanted to know if anyone has an indicator which shows, if the rsi for example is 50 and the stock price is \$100 how will i be able to tell the price of the stock when it higher. for instance if the is a pullback from 50 to 40 what would be the price of the stock when it would it goes back to 50. hope you can understand what i mean

#### Mr Bee

##### New member
VIP
I think you are looking for the built-in TOS indicator called ReverseEngineeringRSI. Pick the number you want to know the price the next bar would need to be to hit the number you are looking for.

#### Jerseystranger

##### New member
Thanks but no. I meant If the RSI was 50 and when it reaches 60. will I be able to predict the price of the stock.

#### MerryDay

Staff member
Staff
VIP
@Jerseystranger So your question is does the RSI oscillator move in at an identical logarithmic pace as price? The answer would be, no.
You cannot extrapolate price from RSI movements.

#### rtry9a

##### New member
I guess to simplify the question, has anyone created a RS (relative strength) type average for real time use. RS = (Avg Gain)/(Avg Loss) for 14 day period on a stock.
I use a 3-period RSI of HLC3 to day trade futures with mostly tick charts- works great, also solid with day charts

#### germanburrito

##### Active member
I use a 3-period RSI of HLC3 to day trade futures with mostly tick charts- works great, also solid with day charts
what tick count do you use if you dnt mind me asking

#### rtry9a

##### New member
with /nq, I start with 750 and modiy to 500 or 1000 depending on volume. Im looking for a smooth rolling pattern and adjust accordingly. I find it helpful to add a centerline and watch for crossovers to enter/exit trades. My study script:
Ruby:
``````declare lower;
input length = 3;
input over_Bought = 75;
input over_Sold = 25;
input price = hlc3;
input averageType = AverageType.WILDERS;
input showBreakoutSignals = no;

#Calculate 3-bar RSI
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 mid = 50;
mid.SetLineWeight(2);
mid.setDefaultColor(createcolor(150,50,0));

plot RSI = 50 * (ChgRatio + 1);
def OverSold = over_Sold;
def 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;
plot UpSignal = if RSI crosses above mid then OverSold else Double.NaN;
plot DownSignal = if RSI crosses below mid then OverBought else Double.NaN;

RSI.SetDefaultColor(Color.GRAY);

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);
UpSignal.SetDefaultColor(Color.blue);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
upSignal.setLineWeight(3);
DownSignal.SetDefaultColor(Color.dark_red);
downSignal.setLineWeight(3);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

Last edited by a moderator:

#### pine

##### New member
Hello,

Not sure if reverse engineer is the right way to describe this.

A partial code of the RSI is below. I am trying to find a way to find the close price if RSI is an input, i.e., if RSI is at 20 from now with length = 14, what would close price be, and have it plotted.

So based on the below, if RSI = 20, then ChgRatio = -0.6. And let's say if NetChgAvg or TotChgAvg is a constant input, then we can solve for other. And if both NetChgAvg and TotChgAvg are known, can 'price' be solved?

Code:
``````input length = 14;
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;

plot RSI = 50 * (ChgRatio + 1);``````

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?