RSI Divergence Indicator with Scanner for ThinkorSwim

BenTen

BenTen

Administrative
Staff
VIP
Warehouse
This indicator for ThinkorSwim will help you detect bullish and bearish RSI divergences on your chart. A lot of people asked for this one after seeing the MACD divergence indicator. Well, here you go.

The header of this script also detailed how to set up the scanner that looks for RSI divergences, which often signal possible reversals.

Both the RSI divergence indicator and scanner was made by Mobius.



thinkScript Code

Rich (BB code):
# RSI_With_Divergence
# Mobius
# V01.01.2013
# 4.15.2019
#hint:<b>RSI with Divergence</b>

# Note: Install this as a new study. Save this study using the name above (the first line of code RSI_With_Divergence).

# To use this study as a scan; DO NOT TRY TO LOAD IT DIRECTLY IN THE SCANNER, IT WILL THROW AN ERROR MESSAGE. Go to the scan tab. Delete any existing scan criteria. Click Add Study Filter. Click the window under Criteria. In that drop down menu click Custom. Delete the existing study. Click Add Condition. Click the down arrow in the Select A Condition window. Click Study. Scroll down the List till you find RSI_With_Divergence and click it. Click on the Plot window and you can choose Dhigh or Dlow in addition to the default plot RSI. If you choose either of the divergence siganls choose is True from the center column. Click on the aggregation period at the top left and set the aggregation period you want scaned. Then click Save and when the popup window shows the warning that this is a custom scan chose OK. Now put the list of stocks you wish to scan in the Scan In box and chose any list you want that to intersect with. If you wish to make this a Dynamic WatchList, save this scan with a name such as RSI_With_Div_WL then in your Gadgets box click the little gear icon, locate the name of the scan you just saved and click it. As equities match the scan criteria they will populate the list.

declare lower;

input n = 14;        #hint nRSI: Periods or length for RSI

input Over_Bought = 70; #hint Over_Bought: Over Bought line

input Over_Sold = 30;   #hint Over_Sold: Over Sold line

def o = open;

def h = high;

def l = low;

def c = close;

def x = BarNumber();

def MidLine = 50;

def NetChgAvg = ExpAverage(c - c[1], n);

def TotChgAvg = ExpAverage(AbsValue(c - c[1]), n);

def ChgRatio = if TotChgAvg != 0

                  then NetChgAvg / TotChgAvg

                  else 0;

plot RSI = 50 * (ChgRatio + 1);

RSI.AssignValueColor(if RSI < Over_Sold

                     then color.yellow

                     else if RSI > Over_Bought

                     then color.yellow

                     else createColor(25, 75, 250));

plot OverSold = Over_Sold;

plot OverBought = Over_Bought;

def bar = BarNumber();

def Currh = if RSI > OverBought

                then fold i = 1 to Floor(n / 2)

                with p = 1

                while p

                do RSI > getValue(RSI, -i)

                else 0;

def CurrPivotH = if (bar > n and

                         RSI == highest(RSI, Floor(n/2)) and

                         Currh)

                     then RSI

                     else double.NaN;

def Currl = if RSI < OverSold

                then fold j = 1 to Floor(n / 2)

                with q = 1

                while q

                do RSI < getValue(RSI, -j)

                else 0;

def CurrPivotL = if (bar > n and

                         RSI == lowest(RSI, Floor(n/2)) and

                         Currl)

                     then RSI

                     else double.NaN;

def CurrPHBar = if !isNaN(CurrPivotH)

                then bar

                else CurrPHBar[1];

def CurrPLBar = if !isNaN(CurrPivotL)

                then bar

                else CurrPLBar[1];

def PHpoint = if !isNaN(CurrPivotH)

              then CurrPivotH

              else PHpoint[1];

def priorPHBar = if PHpoint != PHpoint[1]

                 then CurrPHBar[1]

                 else priorPHBar[1];

def PLpoint = if !isNaN(CurrPivotL)

              then CurrPivotL

              else PLpoint[1];

def priorPLBar = if PLpoint != PLpoint[1]

                 then CurrPLBar[1]

                 else priorPLBar[1];

def HighPivots = bar >= highestAll(priorPHBar);

def LowPivots = bar >= highestAll(priorPLBar);

def pivotHigh = if HighPivots

                then CurrPivotH

                else double.NaN;

plot PlotHline = pivotHigh;

    PlotHline.enableApproximation();

    PlotHline.SetDefaultColor(GetColor(7));

    PlotHline.SetStyle(Curve.Short_DASH);

plot pivotLow = if LowPivots

                then CurrPivotL

                else double.NaN;

    pivotLow.enableApproximation();

    pivotLow.SetDefaultColor(GetColor(7));

    pivotLow.SetStyle(Curve.Short_DASH);

plot PivotDot = if !isNaN(pivotHigh)

                then pivotHigh

                else if !isNaN(pivotLow)

                     then pivotLow

                     else double.NaN;

    pivotDot.SetDefaultColor(GetColor(7));

    pivotDot.SetPaintingStrategy(PaintingStrategy.POINTS);

    pivotDot.SetLineWeight(3);

# End Code RSI with Divergence

Video Tutorial

 
Last edited:
K

Krishna

New member
Scan on daily bars is not displaying the results though study when plotted on the chart shows there is RSI Divergence....anything wrong with this search iam doing...
 
V

veerasareddy

New member
Warehouse
Hi @BenTen ,

Subject: RSI Divergence scanner :

Could you please help to give the scanner for the Top second Yellow divergence [SELL] and bottom 2nd yellow dot divergence [Buy].


Image :






# RSI_With_Divergence

# Mobius

# V01.01.2013

#hint:<b>RSI with Divergence</b>

# Note: Install this as a new study. Save this study using the name above (the first line of code RSI_With_Divergence).

# To use this study as a scan; DO NOT TRY TO LOAD IT DIRECTLY IN THE SCANNER, IT WILL THROW AN ERROR MESSAGE. Go to the scan tab. Delete any existing scan criteria. Click Add Study Filter. Click the window under Criteria. In that drop down menu click Custom. Delete the existing study. Click Add Condition. Click the down arrow in the Select A Condition window. Click Study. Scroll down the List till you find RSI_With_Divergence and click it. Click on the Plot window and you can choose Dhigh or Dlow in addition to the default plot RSI. If you choose either of the divergence siganls choose is True from the center column. Click on the aggregation period at the top left and set the aggregation period you want scaned. Then click Save and when the popup window shows the warning that this is a custom scan chose OK. Now put the list of stocks you wish to scan in the Scan In box and chose any list you want that to intersect with. If you wish to make this a Dynamic WatchList, save this scan with a name such as RSI_With_Div_WL then in your Gadgets box click the little gear icon, locate the name of the scan you just saved and click it. As equities match the scan criteria they will populate the list.

declare lower;

input n = 14; #hint nRSI: Periods or length for RSI

input Over_Bought = 70; #hint Over_Bought: Over Bought line

input Over_Sold = 30; #hint Over_Sold: Over Sold line



def o = open;

def h = high;

def l = low;

def c = close;

def x = BarNumber();

def MidLine = 50;

def NetChgAvg = ExpAverage(c - c[1], n);

def TotChgAvg = ExpAverage(AbsValue(c - c[1]), n);

def ChgRatio = if TotChgAvg != 0

then NetChgAvg / TotChgAvg

else 0;

plot RSI = 50 * (ChgRatio + 1);

RSI.AssignValueColor(if RSI < Over_Sold

then color.red

else if RSI > Over_Bought

then color.green

else createColor(25, 75, 250));

plot OverSold = Over_Sold;

plot OverBought = Over_Bought;

def bar = BarNumber();

def Currh = if RSI > OverBought

then fold i = 1 to Floor(n / 2)

with p = 1

while p

do RSI > getValue(RSI, -i)

else 0;

def CurrPivotH = if (bar > n and

RSI == highest(RSI, Floor(n/2)) and

Currh)

then RSI

else double.NaN;

def Currl = if RSI < OverSold

then fold j = 1 to Floor(n / 2)

with q = 1

while q

do RSI < getValue(RSI, -j)

else 0;

def CurrPivotL = if (bar > n and

RSI == lowest(RSI, Floor(n/2)) and

Currl)

then RSI

else double.NaN;

def CurrPHBar = if !isNaN(CurrPivotH)

then bar

else CurrPHBar[1];

def CurrPLBar = if !isNaN(CurrPivotL)

then bar

else CurrPLBar[1];

def PHpoint = if !isNaN(CurrPivotH)

then CurrPivotH

else PHpoint[1];

def priorPHBar = if PHpoint != PHpoint[1]

then CurrPHBar[1]

else priorPHBar[1];

def PLpoint = if !isNaN(CurrPivotL)

then CurrPivotL

else PLpoint[1];

def priorPLBar = if PLpoint != PLpoint[1]

then CurrPLBar[1]

else priorPLBar[1];

def HighPivots = bar >= highestAll(priorPHBar);

def LowPivots = bar >= highestAll(priorPLBar);

def pivotHigh = if HighPivots

then CurrPivotH

else double.NaN;

plot PlotHline = pivotHigh;

PlotHline.enableApproximation();

PlotHline.SetDefaultColor(GetColor(7));

PlotHline.SetStyle(Curve.Short_DASH);

plot pivotLow = if LowPivots

then CurrPivotL

else double.NaN;

pivotLow.enableApproximation();

pivotLow.SetDefaultColor(GetColor(7));

pivotLow.SetStyle(Curve.Short_DASH);

plot PivotDot = if !isNaN(pivotHigh)

then pivotHigh

else if !isNaN(pivotLow)

then pivotLow

else double.NaN;

pivotDot.SetDefaultColor(GetColor(7));

pivotDot.SetPaintingStrategy(PaintingStrategy.POINTS);

pivotDot.SetLineWeight(3);

# End Code RSI with Divergence
 
T

thinky

Member
Trying to set a custom scan, but the explanation in the code doesn't agree with the actual variables used.

1) Text mentions Dhigh and Dlow, while under Plot you get PlotHline, pivotLow, PivotDot.

2) Within how many bars?

Should I use the info under MACD Divergence?

MACD Divergence Scanner

Bullish
MACD_Divergence_2019()."pivotLow" is true within 6 bars
Bearish
MACD_Divergence_2019()."PlotHline" is true within 6 bars
 
Last edited:
T

Trading51

Member
VIP
Warehouse
Hello, How can i change the moving Exp Moving average to Wilders for the divergence?
 
markos

markos

Well-known member
VIP
Warehouse
Hello, How can i change the moving Exp Moving average to Wilders for the divergence?
@Trading51 Mobius makes Proprietary indicators for Money Managers and Hedge Funds among others.
If he put an Exponential MA in there, then there is a very good reason for it.
If you feel you must change it, Type Universe in the Search Bar above and you'll find links and examples of how to make changes.
Good trading!

Also, please fill out your signature so that we know more of your point of view. Thanks! :)
 
T

Trading51

Member
VIP
Warehouse
@Trading51 Mobius makes Proprietary indicators for Money Managers and Hedge Funds among others.
If he put an Exponential MA in there, then there is a very good reason for it.
If you feel you must change it, Type Universe in the Search Bar above and you'll find links and examples of how to make changes.
Good trading!

Also, please fill out your signature so that we know more of your point of view. Thanks! :)
im very familiar with divergence and exp is the wrong one, is there a specific thread on how to change it thanks
 
markos

markos

Well-known member
VIP
Warehouse
im very familiar with divergence and exp is the wrong one, is there a specific thread on how to change it thanks
If you feel you must change it, Type Universe in the Search Bar above and you'll find links and examples of how to make changes.
Take no offense to my tone, please.
 
T

Trading51

Member
VIP
Warehouse
im very familiar with divergence and exp is the wrong one, is there a specific thread on how to change it thanks
Im just learning how to use the website so forgive me for any wrong suggestion or attempts, but I did figure it out,
 
T

Trading51

Member
VIP
Warehouse
If you feel you must change it, Type Universe in the Search Bar above and you'll find links and examples of how to make changes.
Take no offense to my tone, please.
coding can be difficult but if you play around with it these small ADJUSTMENTS ARE EASY AND MAKE GOOD PRACTICES,, CHEERS MATE, sorry for the caps LOL
 
F

febreezus

New member
I set up a scan query on this but it returns nothing... Does anyone have any idea why?

The scan query condition:

RSI_Divergence()."PlotHline" is true within 3 bars

The odd thing that it starts working at "within 7 bars" and higher. Same goes if you substitute "pivotLow" for "PlotHline".
 
markos

markos

Well-known member
VIP
Warehouse
I set up a scan query on this but it returns nothing... Does anyone have any idea why?

The scan query condition:

RSI_Divergence()."PlotHline" is true within 3 bars

The odd thing that it starts working at "within 7 bars" and higher. Same goes if you substitute "pivotLow" for "PlotHline".
Very hard to say without knowing the list you scan in, time frame and other particulars. Please provide more information. There is nothing wrong with the code. Hopefully someone can help you as I will be in and out for a while.
 
F

febreezus

New member
Oh, sorry, it's really ordinary: daily time frame, scanning all stocks. With no other conditions. I thought maybe there's something in the code preventing using this condition for less than "within 7 bars"? It seems strange that there would be hundreds of hits for "within 7 bars" and zero for "within 6 bars".
 
Last edited:
markos

markos

Well-known member
VIP
Warehouse
With those conditions, I am surprised as well. Wish I could help but no real idea.
 
F

febreezus

New member
Darn. I'd really like to get an RSI divergence scan working. Thanks very much for responding, though.
 
N

NS78-TOS

New member
Hi! Just joined the website and have greatly enjoyed reading through all of the discussions and studies available - fantastic website. I just had a quick question regarding the RSI divergence indicator above. The divergence line (dotted) will appear at the close of the next candle, otherwise, the candle following the most recent high or low, correct? Also, if a new low or high is created afterward, the line will be replaced, right? Thank you in advance for your help - have a great weekend!
 
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
@NS78-TOS Hey, that's correct. The divergences are slightly delayed due to waiting for confirmation from the candles. I have not used this in a while so I can't answer the second question, but I would assume that is the case.
 

Top