RSI Divergence Upper and Lower For ThinkOrSwim

rwfarrell

Member
MOD NOTE: This indicator will show the dots but the divergence lines are INFREQUENT..
Posts complaining that the INFREQUENT divergence lines are not appearing will be deleted.


I wanted to share this since I received it from Mobius. In order to have RSI divergence lines on the chart here is the upper study script. I added the aggregation period to both the upper and lower studies.

Upper Study
Ruby:
# RSI Divergence Upper Study Plots
# Mobius
# V01
# Note: Peak and trough points will always be painted on last 2 RSI low values and on last 2 RSI high values. The bridge will only plot when there is divergence.
input agg = AggregationPeriod.fifteen_min;
input length = 14;
input over_Bought = 70;
input over_Sold = 30;
input averageType = AverageType.WILDERS;

def h = high(period = agg);
def l = low(period = agg);
def c = close(period = agg);

#def h = high;
#def l = low;
#def c = close;
def bar = barNumber();
def NetChgAvg = MovingAverage(averageType, c - c[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(c - c[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);
def OverSold = over_Sold;
def OverBought = over_Bought;
# Divergence Sub-Routine
def indicator = RSI; # indicator to diverge
def upper = OverBought;# crossing above begins routine on the rising side.
def lower = OverSold; # crossing below begins routine on the falling side.
def h_ind_val = if indicator < upper
then h_ind_val[1]
else if indicator crosses above upper
then indicator
else if indicator > upper and indicator > h_ind_val[1]
then indicator
else h_ind_val[1];
def h_ind_bar = if indicator == h_ind_val
then bar
else h_ind_bar[1];
def h_ind_price = if bar == h_ind_bar
then h
else h_ind_price[1];
def prev_h_ind_bar = if indicator crosses above upper
then h_ind_bar[1]
else prev_h_ind_bar[1];
def prev_h_ind_val = if bar == highestAll(prev_h_ind_bar)
then indicator
else prev_h_ind_val[1];
def prev_h_price = if bar == highestAll(prev_h_ind_bar)
then h
else prev_h_price[1];
def high_div_true = h_ind_val < prev_h_ind_val and
h_ind_price > prev_h_price;
plot high_div_line = if high_div_true and bar == highestAll(h_ind_bar)
then h_ind_price
else if bar == highestAll(prev_h_ind_bar)
then prev_h_price
else double.nan;
high_div_line.EnableApproximation();
high_div_line.HideBubble();
high_div_line.HideTitle();
plot h_point = if bar == highestAll(h_ind_bar)
then h_ind_price
else double.nan;
h_point.SetStyle(Curve.Points);
h_point.SetLineWeight(2);
h_point.SetDefaultColor(Color.yellow);
h_point.HideBubble();
h_point.HideTitle();
plot prev_h_point = if bar == highestAll(prev_h_ind_bar)
then prev_h_price
else double.nan;
prev_h_point.SetStyle(Curve.Points);
prev_h_point.SetLineWeight(2);
prev_h_point.SetDefaultColor(Color.yellow);
prev_h_point.HideBubble();
prev_h_point.HideTitle();
addLabel(0, "bar = " + bar +
" h_ind_bar = " + highestAll(h_ind_bar) +
" h_ind_value = " + h_ind_val +
" h_ind_price = " + h_ind_price +
" prev_h_ind_bar = " + highestAll(prev_h_ind_bar) +
" prev_h_ind = " + prev_h_ind_val +
" prev_h_price = " + prev_h_price);

def l_ind_val = if indicator > lower
then l_ind_val[1]
else if indicator crosses below lower
then indicator
else if indicator < lower and indicator < l_ind_val[1]
then indicator
else l_ind_val[1];
def l_ind_bar = if indicator == l_ind_val
then bar
else l_ind_bar[1];
def l_ind_price = if bar == l_ind_bar
then l
else l_ind_price[1];
def prev_l_ind_bar = if indicator crosses below lower
then l_ind_bar[1]
else prev_l_ind_bar[1];
def prev_l_ind_val = if bar == highestAll(prev_l_ind_bar)
then indicator
else prev_l_ind_val[1];
def prev_l_price = if bar == highestAll(prev_l_ind_bar)
then l
else prev_l_price[1];
def low_div_true = l_ind_val > prev_l_ind_val and
l_ind_price < prev_l_price;
plot low_div_line = if low_div_true and bar == highestAll(l_ind_bar)
then l_ind_price
else if bar == highestAll(prev_l_ind_bar)
then prev_l_price
else double.nan;
low_div_line.EnableApproximation();
low_div_line.HideBubble();
low_div_line.HideTitle();
plot l_point = if bar == highestAll(l_ind_bar)
then l_ind_price
else double.nan;
l_point.SetStyle(Curve.Points);
l_point.SetLineWeight(2);
l_point.SetDefaultColor(Color.yellow);
l_point.HideBubble();
l_point.HideTitle();
plot prev_l_point = if bar == highestAll(prev_l_ind_bar)
then prev_l_Price
else double.nan;
prev_l_point.SetStyle(Curve.Points);
prev_l_point.SetLineWeight(2);
prev_l_point.SetDefaultColor(Color.yellow);
prev_l_point.HideBubble();
prev_l_point.HideTitle();
addLabel(0, "bar = " + bar +
" l_ind_bar = " + highestAll(l_ind_bar) +
" l_ind_value = " + l_ind_val +
" l_ind_price = " + l_ind_price +
" prev_l_ind_bar = " + highestAll(prev_l_ind_bar) +
" prev_l_ind = " + prev_l_ind_val +
" prev_l_price = " + prev_l_price);
# End Code

Lower Study
Ruby:
# Divergence Sub-Routine

declare lower;
input agg = AggregationPeriod.FOUR_HOURS;
input length = 14;
input over_Bought = 70;
input over_Sold = 30;
input averageType = AverageType.WILDERS;

def h = high(period = agg);
def l = low(period = agg);
def c = close(period = agg);

def bar = BarNumber();
def NetChgAvg = MovingAverage(averageType, c - c[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(c - c[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);
def indicator = RSI; # indicator to diverge
def upper = over_Bought;# crossing above begins routine on the rising side.
def lower = over_Sold; # crossing below begins routine on the falling side.
def h_ind_val = if indicator < upper
then h_ind_val[1]
else if indicator crosses above upper
then indicator
else if indicator > upper and indicator > h_ind_val[1]
then indicator
else h_ind_val[1];
def h_ind_bar = if indicator == h_ind_val
then bar
else h_ind_bar[1];
def h_ind_price = if bar == h_ind_bar
then h
else h_ind_price[1];
def prev_h_ind_bar = if indicator crosses above upper
then h_ind_bar[1]
else prev_h_ind_bar[1];
def prev_h_ind_val = if bar == HighestAll(prev_h_ind_bar)
then indicator
else prev_h_ind_val[1];
def prev_h_price = if bar == HighestAll(prev_h_ind_bar)
then h
else prev_h_price[1];
def high_div_true = h_ind_val < prev_h_ind_val and
h_ind_price > prev_h_price;
plot high_div_line = if high_div_true and bar == HighestAll(h_ind_bar)
then indicator
else if bar == HighestAll(prev_h_ind_bar)
then indicator
else Double.NaN;
high_div_line.EnableApproximation();
high_div_line.HideBubble();
high_div_line.HideTitle();
plot h_point = if bar == HighestAll(h_ind_bar)
then indicator
else Double.NaN;
h_point.SetStyle(Curve.POINTS);
h_point.SetLineWeight(2);
h_point.SetDefaultColor(Color.YELLOW);
h_point.HideBubble();
h_point.HideTitle();
plot prev_h_point = if bar == HighestAll(prev_h_ind_bar)
then indicator
else Double.NaN;
prev_h_point.SetStyle(Curve.POINTS);
prev_h_point.SetLineWeight(2);
prev_h_point.SetDefaultColor(Color.YELLOW);
prev_h_point.HideBubble();
prev_h_point.HideTitle();
AddLabel(0, "bar = " + bar +
" h_ind_bar = " + HighestAll(h_ind_bar) +
" h_ind_value = " + h_ind_val +
" h_ind_price = " + h_ind_price +
" prev_h_ind_bar = " + HighestAll(prev_h_ind_bar) +
" prev_h_ind = " + prev_h_ind_val +
" prev_h_price = " + prev_h_price);

def l_ind_val = if indicator > lower
then l_ind_val[1]
else if indicator crosses below lower
then indicator
else if indicator < lower and indicator < l_ind_val[1]
then indicator
else l_ind_val[1];
def l_ind_bar = if indicator == l_ind_val
then bar
else l_ind_bar[1];
def l_ind_price = if bar == l_ind_bar
then l
else l_ind_price[1];
def prev_l_ind_bar = if indicator crosses below lower
then l_ind_bar[1]
else prev_l_ind_bar[1];
def prev_l_ind_val = if bar == HighestAll(prev_l_ind_bar)
then indicator
else prev_l_ind_val[1];
def prev_l_price = if bar == HighestAll(prev_l_ind_bar)
then l
else prev_l_price[1];
def low_div_true = l_ind_val > prev_l_ind_val and
l_ind_price < prev_l_price;
plot low_div_line = if low_div_true and bar == HighestAll(l_ind_bar)
then indicator
else if bar == HighestAll(prev_l_ind_bar)
then indicator
else Double.NaN;
low_div_line.EnableApproximation();
low_div_line.HideBubble();
low_div_line.HideTitle();
plot l_point = if bar == HighestAll(l_ind_bar)
then indicator
else Double.NaN;
l_point.SetStyle(Curve.POINTS);
l_point.SetLineWeight(2);
l_point.SetDefaultColor(Color.YELLOW);
l_point.HideBubble();
l_point.HideTitle();
plot prev_l_point = if bar == HighestAll(prev_l_ind_bar)
then indicator
else Double.NaN;
prev_l_point.SetStyle(Curve.POINTS);
prev_l_point.SetLineWeight(2);
prev_l_point.SetDefaultColor(Color.YELLOW);
prev_l_point.HideBubble();
prev_l_point.HideTitle();
AddLabel(0, "bar = " + bar +
" l_ind_bar = " + HighestAll(l_ind_bar) +
" l_ind_value = " + l_ind_val +
" l_ind_price = " + l_ind_price +
" prev_l_ind_bar = " + HighestAll(prev_l_ind_bar) +
" prev_l_ind = " + prev_l_ind_val +
" prev_l_price = " + prev_l_price);
# End Code
 
Last edited by a moderator:

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

There is a note in the upper study that Mobius provided.

# Note: Peak and trough points will always be painted on last 2 RSI low values and on last 2 RSI high values. The bridge will only plot when there is divergence.

The bridge will only form when there is divergence. The points will populate on the chart.
 
Last edited by a moderator:
oh. just overlaying it over a normal RSI indicator worked. i didn't realize this didnt contain an rsi plot at all. best !
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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