Dynamic RSI for ThinkorSwim

petergluis

Active member
I converted Dynamic RSI to ThinkorSwim. The Dynamic RSI indicator is a kind of exponential RSI . The overbought and oversold levels (respectively HiLine and LoLine) are calculated according to the recent highest and lowest values of the Dynamic RSI line.
https://www.tradingview.com/script/2FYksyrm-Dynamic-RSI/


Ruby:
#https://www.tradingview.com/script/2FYksyrm-Dynamic-RSI/
#© Dreadblitz
declare lower;
def DZbuy = 0.1;
def DZsell = 0.1;
input period = 14;
input averageType = AverageType.WILDERS;
Def Lb = 60;
input price = close;
input length = 14;
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);
RSI.DefineColor("UpTrend", Color.GREEN);
RSI.DefineColor("DownTrend", Color.RED);
RSI.SetLineWeight(3);
RSI.SetPaintingStrategy(PaintingStrategy.LINE);
RSI.SetStyle(Curve.FIRM);
RSI.AssignValueColor(if RSI  > RSI  [1] then RSI.Color("UpTrend") else RSI.Color("DownTrend"));

def RSILine = 50 * (ChgRatio + 1);
def jh = highest(RSILine, Lb);
def jl = lowest(RSILine, Lb);
def jc = (wma((jh-jl)*0.5,Period) + wma(jl,Period));
def Hiline = jh - jc * DZbuy;
def Loline = jl + jc * DZsell;
def R = (4 * RSILine + 3 * RSILine[1] + 2 * RSILine[2] + RSILine[3] ) / 10;

plot a = R;
a.DefineColor("UpTrend", Color.light_GREEN);
a.DefineColor("DownTrend", Color.light_RED);
a.SetLineWeight(2);
a.SetPaintingStrategy(PaintingStrategy.LINE);
a.SetStyle(Curve.FIRM);
a.AssignValueColor(if a  >a  [1] then a.Color("UpTrend") else a.Color("DownTrend"));
plot b = Hiline;
b.SetDefaultColor(GetColor(4));
b.SetLineWeight(2);
plot c = Loline;
c.SetDefaultColor(GetColor(9));
c.SetLineWeight(2);
plot d = jc;
d.DefineColor("UpTrend", Color.cyan);
d.DefineColor("DownTrend", Color.magenta);
d.SetLineWeight(2);
d.SetPaintingStrategy(PaintingStrategy.LINE);
d.SetStyle(Curve.FIRM);
d.AssignValueColor(if RSI > d then d.Color("UpTrend") else d.Color("DownTrend"));
Addcloud(RSI, a, color.green, color.red);
Plot overbought =80;
overbought.SetDefaultColor(GetColor(5));
plot oversold = 20;
oversold.SetDefaultColor(GetColor(6));
 
Last edited by a moderator:

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

I combined dynamic RSI with Stochastic RSI to monitor overbought and oversold states.

Ruby:
#https://www.tradingview.com/script/2FYksyrm-Dynamic-RSI/
#© Dreadblitz
declare lower;
def DZbuy = 0.1;
def DZsell = 0.1;
input period = 14;
input averageType = AverageType.WILDERS;
Def Lb = 60;
input price = close;
input length = 14;
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;
input KPeriod =5;
input DPeriod = 3;
input priceH = high;
input priceL = low;
input priceC = close;
input slowing_period = 3;
input averageType1 = AverageType.SIMPLE;
input showBreakoutSignals = {default "No", "On FullK", "On FullD", "On FullK & FullD"};

plot RSI = 50 * (ChgRatio + 1);
RSI.DefineColor("UpTrend", Color.GREEN);
RSI.DefineColor("DownTrend", Color.RED);
RSI.SetLineWeight(4);
RSI.SetPaintingStrategy(PaintingStrategy.LINE);
RSI.SetStyle(Curve.FIRM);
RSI.AssignValueColor(if RSI  > RSI  [1] then RSI.Color("UpTrend") else RSI.Color("DownTrend"));

def RSILine = 50 * (ChgRatio + 1);
def jh = highest(RSILine, Lb);
def jl = lowest(RSILine, Lb);
def jc = (wma((jh-jl)*0.5,Period) + wma(jl,Period));
def Hiline = jh - jc * DZbuy;
def Loline = jl + jc * DZsell;
def R = (4 * RSILine + 3 * RSILine[1] + 2 * RSILine[2] + RSILine[3] ) / 10;

plot a = R;
a.DefineColor("UpTrend", Color.light_GREEN);
a.DefineColor("DownTrend", Color.light_RED);
a.SetLineWeight(2);
a.SetPaintingStrategy(PaintingStrategy.LINE);
a.SetStyle(Curve.FIRM);
a.AssignValueColor(if a  >a  [1] then a.Color("UpTrend") else a.Color("DownTrend"));
plot b = Hiline;
b.SetDefaultColor(GetColor(4));
b.SetLineWeight(2);
plot c = Loline;
c.SetDefaultColor(GetColor(9));
c.SetLineWeight(2);
plot d = jc;
d.DefineColor("UpTrend", Color.cyan);
d.DefineColor("DownTrend", Color.magenta);
d.SetLineWeight(2);
d.SetPaintingStrategy(PaintingStrategy.LINE);
d.SetStyle(Curve.FIRM);
d.AssignValueColor(if RSI > d then d.Color("UpTrend") else d.Color("DownTrend"));
Addcloud(RSI, a, color.green, color.red);
Plot overbought =90;
overbought.SetDefaultColor(GetColor(2));
Plot overbought1 =80;
overbought1.SetDefaultColor(GetColor(2));
plot oversold1 = 20;
oversold1.SetDefaultColor(GetColor(6));
plot oversold = 10;
oversold.SetDefaultColor(GetColor(6));
def lowest_k = Lowest(priceL, KPeriod);
def c1 = priceC - lowest_k;
def c2 = Highest(priceH, KPeriod) - lowest_k;
def FastK = if c2 != 0 then c1 / c2 * 100 else 0;

plot FullK = MovingAverage(averageType1, FastK, slowing_period);
plot FullD = MovingAverage(averageType1, FullK, DPeriod);


def upK = FullK crosses above OverSold;
def upD = FullD crosses above OverSold;
def downK = FullK crosses below OverBought;
def downD = FullD crosses below OverBought;

plot UpSignal;
plot DownSignal;
switch (showBreakoutSignals) {
case "No":
    UpSignal = Double.NaN;
    DownSignal = Double.NaN;
case "On FullK":
    UpSignal = if upK then OverSold else Double.NaN;
    DownSignal = if downK then OverBought else Double.NaN;
case "On FullD":
    UpSignal = if upD then OverSold else Double.NaN;
    DownSignal = if downD then OverBought else Double.NaN;
case "On FullK & FullD":
    UpSignal = if upK or upD then OverSold else Double.NaN;
    DownSignal = if downK or downD then OverBought else Double.NaN;
}

UpSignal.setHiding(showBreakoutSignals == showBreakoutSignals."No");
DownSignal.setHiding(showBreakoutSignals == showBreakoutSignals."No");

FullK.SetDefaultColor(GetColor(1));
Fullk.SetLineWeight (1);
FullD.SetDefaultColor(GetColor(0));
FullD.SetLineWeight (1);
OverBought.SetDefaultColor(GetColor(1));
OverSold.SetDefaultColor(GetColor(1));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
 
I add color changes to Stochastic RSI in order to follow the trend changes. When RSI surpasses upperline, and stochastic RSI is greater than 90, it is overbought for the trend reversal or for the pullback. When RSI falls below lowerline, and stochastic RSI is smaller than 10, it is oversold for the trend reversal or for a dead cat bounce.

Ruby:
#https://www.tradingview.com/script/2FYksyrm-Dynamic-RSI/
#© Dreadblitz
declare lower;
def DZbuy = 0.1;
def DZsell = 0.1;
input period = 14;
input averageType = AverageType.WILDERS;
Def Lb = 60;
input price = close;
input length = 14;
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;
input KPeriod =5;
input DPeriod = 3;
input priceH = high;
input priceL = low;
input priceC = close;
input slowing_period = 3;
input averageType1 = AverageType.SIMPLE;
input showBreakoutSignals = {default "No", "On FullK", "On FullD", "On FullK & FullD"};

plot RSI = 50 * (ChgRatio + 1);
RSI.DefineColor("UpTrend", Color.GREEN);
RSI.DefineColor("DownTrend", Color.RED);
RSI.SetLineWeight(4);
RSI.SetPaintingStrategy(PaintingStrategy.LINE);
RSI.SetStyle(Curve.FIRM);
RSI.AssignValueColor(if RSI  > RSI  [1] then RSI.Color("UpTrend") else RSI.Color("DownTrend"));

def RSILine = 50 * (ChgRatio + 1);
def jh = highest(RSILine, Lb);
def jl = lowest(RSILine, Lb);
def jc = (wma((jh-jl)*0.5,Period) + wma(jl,Period));
def Hiline = jh - jc * DZbuy;
def Loline = jl + jc * DZsell;
def R = (4 * RSILine + 3 * RSILine[1] + 2 * RSILine[2] + RSILine[3] ) / 10;

plot a = R;
a.DefineColor("UpTrend", Color.light_GREEN);
a.DefineColor("DownTrend", Color.light_RED);
a.SetLineWeight(2);
a.SetPaintingStrategy(PaintingStrategy.LINE);
a.SetStyle(Curve.FIRM);
a.AssignValueColor(if a  >a  [1] then a.Color("UpTrend") else a.Color("DownTrend"));
plot b = Hiline;
b.AssignValueColor(if b> b[1] then Color.uptick else if b<b[1] then Color.lime else color.light_gray);
b.SetLineWeight(4);
plot c = Loline;
c.AssignValueColor(if c> c[1] then Color.uptick else if c<c[1] then Color.lime else color.light_gray);
c.SetLineWeight(4);
plot d = jc;
d.DefineColor("UpTrend", Color.cyan);
d.DefineColor("DownTrend", Color.magenta);
d.SetLineWeight(2);
d.SetPaintingStrategy(PaintingStrategy.LINE);
d.SetStyle(Curve.FIRM);
d.AssignValueColor(if RSI > d then d.Color("UpTrend") else d.Color("DownTrend"));
Addcloud(RSI, a, color.green, color.red);
Plot overbought =90;
overbought.SetDefaultColor(GetColor(2));
Plot overbought1 =80;
overbought1.SetDefaultColor(GetColor(2));
plot oversold1 = 20;
oversold1.SetDefaultColor(GetColor(6));
plot oversold = 10;
oversold.SetDefaultColor(GetColor(6));
def lowest_k = Lowest(priceL, KPeriod);
def c1 = priceC - lowest_k;
def c2 = Highest(priceH, KPeriod) - lowest_k;
def FastK = if c2 != 0 then c1 / c2 * 100 else 0;

plot FullK = MovingAverage(averageType1, FastK, slowing_period);

plot FullD = MovingAverage(averageType1, FullK, DPeriod);


def upK = FullK crosses above OverSold;
def upD = FullD crosses above OverSold;
def downK = FullK crosses below OverBought;
def downD = FullD crosses below OverBought;

plot UpSignal;
plot DownSignal;
switch (showBreakoutSignals) {
case "No":
    UpSignal = Double.NaN;
    DownSignal = Double.NaN;
case "On FullK":
    UpSignal = if upK then OverSold else Double.NaN;
    DownSignal = if downK then OverBought else Double.NaN;
case "On FullD":
    UpSignal = if upD then OverSold else Double.NaN;
    DownSignal = if downD then OverBought else Double.NaN;
case "On FullK & FullD":
    UpSignal = if upK or upD then OverSold else Double.NaN;
    DownSignal = if downK or downD then OverBought else Double.NaN;
}

UpSignal.setHiding(showBreakoutSignals == showBreakoutSignals."No");
DownSignal.setHiding(showBreakoutSignals == showBreakoutSignals."No");
Fullk.DefineColor("UpTrend", Color.cyan);
Fullk.DefineColor("DownTrend", Color.magenta);
Fullk.AssignValueColor(if Fullk > Fullk  [1] then Fullk.Color("UpTrend") else Fullk.Color("DownTrend"));
Fullk.SetLineWeight (1);
FullD.DefineColor("UpTrend", Color.cyan);
FullD.DefineColor("DownTrend", Color.magenta);
FullD.AssignValueColor(if FullD > FullD[1] then FullD.Color("UpTrend") else FullD.Color("DownTrend"));
FullD.SetLineWeight (1);
OverBought.SetDefaultColor(GetColor(1));
OverSold.SetDefaultColor(GetColor(1));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
addcloud(fullk, fulld, color.cyan, color.magenta);
 
Last edited:
what advantage does this indicator have over a regular old rsi 14 study .
The regular RSI has static OB / OS levels which some instruments may never reach.

Adding dynamic ranges based on the equity's historic range, provides more realistic overbought and oversold ranges.

PS: I don't personally use oscillators in this manner. But adding ranges to your indicators and to your candles, is always a good idea.
Knowing where price or any other indicator is within its trading range is an integral part of trading decisions.
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
517 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