RSI Divergence Indicator For ThinkOrSwim

BenTen

Administrative
Staff member
Staff
VIP
Lifetime
This indicator for ThinkorSwim will help you detect bullish and bearish RSI divergences on your chart.
The header of this script also detailed how to set up the scanner that looks for RSI divergences, which often signal possible reversals.
There are a couple of versions for you to check out. The first by Mobius, the other by @cos251

AZuDzBB.png


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

Another version created by Drew Griffith:

Code:
# RSIDIVERGENCE

declare lower;

input nRSI = 2;
input OverBought = 95;
input OverSold = 5;
input MidLine = 50;
input nTrend = 100;
input TrendLine = {EMA, SMA, default LRL, WMA};

# Global definitions
def h = high;
def l = low;
def c = close;

def cond1 = CompoundValue(1, if IsNaN(c)
                            then cond1[1]
                            else c, c);



# RSI Wilder with Divergence Markers
def NetChgAvg = WildersAverage(c - c[1], nRSI);
def TotChgAvg = WildersAverage(AbsValue(c - c[1]), nRSI);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
plot RSI = round(50 * (ChgRatio + 1),0);
RSI.SetLineWeight(3);
RSI.AssignValueColor(if RSI < OverSold then Color.Green else if  RSI > OverBought then Color.Red else Color.Gray);

plot RSItrend;
switch (TrendLine) {
case EMA:
   RSItrend = ExpAverage(RSI, nTrend);
case SMA:
   RSItrend = Average(RSI, nTrend);
case LRL:
   RSItrend = InertiaAll(RSI, nTrend);
case WMA:
   RSItrend = WMA(RSI, nTrend);
}
RSItrend.SetLineWeight(1);
RSItrend.SetDefaultColor(Color.RED);

plot RSIOB = OverBought;
RSIOB.SetLineWeight(1);
RSIOB.SetDefaultColor(Color.Blue);
plot RSIOS = OverSold;
RSIOS.SetLineWeight(1);
RSIOS.SetDefaultColor(Color.Blue);

def lowestLow = if RSI > OverSold
               then l
               else if RSI < OverSold and
                       l < lowestLow[1]
               then l
               else lowestLow[1];
def lowestRSI = if RSI > MidLine
               then RSI
               else if RSI < MidLine and
                       RSI < lowestRSI[1]
               then RSI
               else lowestRSI[1];
def divergentLow = if RSI < OverSold and
                  l <= lowestLow[1] and
                  RSI > lowestRSI[1]
                  then OverSold
                  else Double.NaN;
plot DLow = divergentLow;
DLow.SetPaintingStrategy(PaintingStrategy.POINTS);
DLow.SetLineWeight(3);
DLow.SetDefaultColor(Color.YELLOW);

def highestHigh = if RSI < OverBought
                 then h
                 else if RSI > OverBought and
                         h > highestHigh[1]
                 then h
                 else highestHigh[1];
def highestRSI = if RSI < MidLine
                then RSI
                else if RSI > MidLine and
                        RSI > highestRSI[1]
                then RSI
                else highestRSI[1];
def divergentHigh = if RSI > OverBought and
                      h >= highestHigh[1] and
                      RSI < highestRSI[1] and
                      cond1 within 3 bars
                   then OverBought
                   else Double.NaN;

plot DHigh = divergentHigh;
DHigh.SetPaintingStrategy(PaintingStrategy.POINTS);
DHigh.SetLineWeight(3);
DHigh.SetDefaultColor(Color.YELLOW);

def AlertCond1 = DHigh == OverBought;
def AlertCond2 = DLow == OverSold;

#Alert(AlertCond1, "Divergent High", Alert.Bar, Sound.Bell);
#Alert(AlertCond1, "Divergent Low", Alert.Bar, Sound.Bell);

RSI Divergence Candles

hUqZ7kF.jpg


Version 1.1 of RSI_Div_Candles per @High_Velocity request.
(Thx @germanburrito for AddChart() logic)
Fixed/Added the following:
  1. Fixed hollow UP Candles - fix provided by @SleepyZ
  2. Adjusted OB/OS option to turn on/off
  3. Added Visual Divergence Lines - credit @SuryaKiranC

Ruby:
## RSI_DIV_CANDLES
##
##
## CREDITS
## Requested by @High_Velocity from orignal source https://www.tradingview.com/v/4VKd87zy/
##
##
## Removing the header Credit credits and description is not permitted, any modification needs to be shared.
##
## V 1.1 :    @cos251 - @SleepyZ provided fix for hollow UP candles.  @SuryaKiranC provided Divergence line identifier
##                      Added option to turn on/off OB/OS lines.
##
## V 1.0 :    @cos251 - Initial release per request from usethinkscript forum.  Logic appears to match, Candle wicks may need additional
##       :    tweaking; thanks to @germanburrito for sharing AddChart() logic

declare lower;

input useHL          = no;
input price          = close;
input fastLength     = 8;
input slowLength     = 55;
input smooth         = 10;
input overBought     = 70;
input overSold       = 30;
input showOBOS       = no;
input showDivergence = yes;

def fastRSI = reference RSI(length = fastLength, price = price);
def slowRSI = reference RSI(length = slowLength, price = price);

def smoothFastRSI = MovingAverage(AverageType.WILDERS,fastRSI,smooth);
def smoothSlowRSI = MovingAverage(AverageType.WILDERS,slowRSI,smooth);

def rsiHigh = if useHL then Max(RSI(length = fastLength, price = high),RSI(length=slowLength,price = high)) else MAX(fastRSI,slowRSI);
def rsiLow = if useHL then MIN(RSI(length=fastLength,price=low),RSI(length=slowLength,price=low)) else MIN(fastRSI,slowRSI);

def isBull = smoothFastRSI >= smoothSlowRSI;
def isExp = AbsValue(smoothFastRSI-smoothSlowRSI) >= AbsValue(smoothFastRSI[1]-smoothSlowRSI[1]);

def o = smoothSlowRSI;
def h = rsiHigh;
def l = rsiLow;
def c = smoothFastRSI;

# Plot UP candle with isBull only
def UpO1;
def UpH1;
def UpL1;
def UpC1;
if o < c and isBull
then {
    UpO1 = o;
    UpH1 = h;
    UpL1 = l;
    UpC1 = c;
} else {
    UpO1 = Double.NaN;
    UpH1 = Double.NaN;
    UpL1 = Double.NaN;
    UpC1 = Double.NaN;
}

# Plot UP candle with isBull and isExp
def UpO;
def UpH;
def UpL;
def UpC;
if o < c and isBull and isExp
then {
    UpO = o;
    UpH = h;
    UpL = l;
    UpC = c;
} else {
    UpO = Double.NaN;
    UpH = Double.NaN;
    UpL = Double.NaN;
    UpC = Double.NaN;
}

# Plot DOWN candle
def DnO;
def DnH;
def DnL;
def DnC;
if o > c
then {
    DnO = o;
    DnH = h;
    DnL = l;
    DnC = c;
} else {
    DnO = Double.NaN;
    DnH = Double.NaN;
    DnL = Double.NaN;
    DnC = Double.NaN;
}


# Plot DOWN candle with !isBull and !isExp
def DnO1;
def DnH1;
def DnL1;
def DnC1;
if o > c and !isBull and !isExp
then {
    DnO1 = o;
    DnH1 = h;
    DnL1 = l;
    DnC1 = c;
} else {
    DnO1 = Double.NaN;
    DnH1 = Double.NaN;
    DnL1 = Double.NaN;
    DnC1 = Double.NaN;
}

# Plot the new Chart
#AddChart(high = UpH1, low = UpL1, open = UpO1, close = UpC1, type = ChartType.CANDLE, growcolor = Color.PINK);
#AddChart(high = UpH, low = UpL, open = UpO, close = UpC, type = ChartType.CANDLE, growcolor = Color.GREEN);
AddChart(high = UpH1, low = UpL1, open = Upc1, close = Upo1, type = ChartType.CANDLE, growcolor = Color.PINK);
AddChart(high = UpH, low = UpL, open = Upc, close = Upo, type = ChartType.CANDLE, growcolor = Color.GREEN);
AddChart(high = DnH, low = DnL, open = DnO, close = DnC, type = ChartType.CANDLE, growcolor = Color.RED);
AddChart(high = DnH1, low = DnL1, open = DnO1, close = DnC1, type = ChartType.CANDLE, growcolor = Color.CYAN);

plot ob = if showOBOS then overBought else Double.NaN;
ob.SetDefaultColor(COLOR.RED);
plot os = if showOBOS then overSold else Double.NaN;
os.SetDefaultColor(COLOR.GREEN);

def BN = barnumber();

#Divergence Code

def RSI_H = if fastRSI > overBought then fold Ri = 1 to Floor(fastLength / 2) with Rp = 1 while Rp do fastRSI > GetValue(fastRSI, -Ri) else 0;
def RSI_PivotH = if (BN > fastLength and fastRSI == Highest(fastRSI, Floor(fastLength / 2)) and RSI_H) then fastRSI else Double.NaN;
def RSI_L = if fastRSI < overSold then fold Rj = 1 to Floor(fastLength / 2) with Rq = 1 while Rq do fastRSI < GetValue(fastRSI, -Rj) else 0;
def RSI_PivotL = if (BN > fastLength and fastRSI == Lowest(fastRSI, Floor(fastLength / 2)) and RSI_L) then fastRSI else Double.NaN;
def RSI_PHBar = if !IsNaN(RSI_PivotH) then BN else RSI_PHBar[1];
def RSI_PLBar = if !IsNaN(RSI_PivotL) then BN else RSI_PLBar[1];
def RSI_PHPoint = if !IsNaN(RSI_PivotH) then RSI_PivotH else RSI_PHPoint[1];
def RSI_LastPHBar = if RSI_PHPoint != RSI_PHPoint[1] then RSI_PHBar[1] else RSI_LastPHBar[1];
def RSI_PLPoint = if !IsNaN(RSI_PivotL) then RSI_PivotL else RSI_PLPoint[1];
def RSI_LastPLBar = if RSI_PLPoint != RSI_PLPoint[1] then RSI_PLBar[1] else RSI_LastPLBar[1];

def RSI_HighPivots = BN >= HighestAll(RSI_LastPHBar);
def RSI_LowPivots = BN >= HighestAll(RSI_LastPLBar);
def RSI_pivotHigh = if RSI_HighPivots then RSI_PivotH else Double.NaN;

plot RSI_plotHline = if showDivergence then RSI_pivotHigh else Double.NaN;
RSI_plotHline.EnableApproximation();
RSI_plotHline.SetDefaultColor(Color.LIME);
RSI_plotHline.SetStyle(Curve.SHORT_DASH);

plot RSI_pivotLow = if showDivergence and RSI_LowPivots then RSI_PivotL else Double.NaN;
RSI_pivotLow.EnableApproximation();
RSI_pivotLow.SetDefaultColor(Color.LIME);
RSI_pivotLow.SetStyle(Curve.SHORT_DASH);

plot RSI_pivotDot = if !IsNaN(RSI_pivotHigh) then RSI_pivo[ATTACH=full]13051[/ATTACH]tHigh else if !IsNaN(RSI_pivotLow) then RSI_pivotLow else Double.NaN;
RSI_pivotDot.SetDefaultColor(Color.LIME);
RSI_pivotDot.SetPaintingStrategy(PaintingStrategy.POINTS);
RSI_pivotDot.SetLineWeight(3);

AddLabel(yes,"RSI:",Color.YELLOW);

UPPER STUDY VERSION 1.0 (per request from @TradeUp)
This is the upper version as requested (pretty lean on code too).
For now only includes the candle coloring, will have to work on plotting the Divergence Lines.
BGDMrrq.jpg


Code:
## RSI_DIV_CANDLES_Upper
##
##
## CREDITS
## Requested by @High_Velocity from orignal source https://www.tradingview.com/v/4VKd87zy/
## Upper Study requested by @TradeUp
##
##
##
## V 1.0 :    @cos251 - Initial release of UPPER version per request from usethinkscript forum. Logic adopted from lower study
##       :    tweaking; thanks to @germanburrito for sharing AddChart() logic

declare upper;

input useHL          = no;
input price          = close;
input fastLength     = 14;
input slowLength     = 55;
input smooth         = 10;
#input showDivergence = yes;
input paintBars      = yes;


def fastRSI = reference RSI(length = fastLength, price = price);
def slowRSI = reference RSI(length = slowLength, price = price);

def smoothFastRSI = MovingAverage(AverageType.WILDERS,fastRSI,smooth);
def smoothSlowRSI = MovingAverage(AverageType.WILDERS,slowRSI,smooth);

def rsiHigh = if useHL then Max(RSI(length = fastLength, price = high),RSI(length=slowLength,price = high)) else MAX(fastRSI,slowRSI);
def rsiLow = if useHL then MIN(RSI(length=fastLength,price=low),RSI(length=slowLength,price=low)) else MIN(fastRSI,slowRSI);

def isBull = smoothFastRSI >= smoothSlowRSI;
def isExp = AbsValue(smoothFastRSI-smoothSlowRSI) >= AbsValue(smoothFastRSI[1]-smoothSlowRSI[1]);

def o = smoothSlowRSI;
def h = rsiHigh;
def l = rsiLow;
def c = smoothFastRSI;

AssignPriceColor(if paintBars and o < c and isBull and isExp then Color.GREEN else if paintBars and o > c and !isBull and !isExp then Color.CYAN else if paintBars and o > c and !isBull then Color.RED else if paintBars then Color.PINK else Color.CURRENT);

Video Tutorial

 
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,
 
With those conditions, I am surprised as well. Wish I could help but no real idea.
 
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!
 
@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.
 
I am looking for code snippets that plot divergence lines between price and the RSI. I mean if the price has higher highs while RSI has a lower high (or vice versa). Instead of visually verifying the divergence, I want to be able to plot it.

Here is the code that I want to use to add these lines. @tomsk helped me and I made few tweaks.
Code:
# Divergence Updown Points
# tomsk  (modfied further by [USER=2184]@venus360[/USER])
# 1.17.2020

# Plots the ratio of Up/Down points relative to the prior candle

input period = 14;
declare lower;

def upPoints   = fold i = 0 to period
                 with p
                 do p + GetValue(if close > close[1] then close - close[1] else 0, i);
def downPoints = fold j = 0 to period
                 with r
                 do r + GetValue(if close < close[1] then close - close[1] else 0, j);

def au = upPoints / period;
def ad = AbsValue(downPoints) / period;

def ratio = au / ad;

def step0 = 1 + ratio;
def step1 = 100 / step0;
plot final = 100 - step1;

# End UpDown Points Divergence

0uufIZ1.png


I manually added the lines, I want to explore if the divergence lines can be plotted using code.
 
Here is an upper divergence study that you might like. It will plot points on the bars or there is a paintbar option.
The first image shows how many signals will pop up will removal of futurebar filter.
The second image shows how divergence can work wonders sometimes and other times in a strong directional push it will throw false signals.
The third image just shows the points instead of paintbars.


Code:
#RSI_DIVERGENCE_POINTS
#DeusMecanicus

declare upper;
input Length = 14;
input Price = close;
input AverageType = AverageType.WILDERS;
input Lookback1 = 20;
input Lookback2 = 50;
input Lookback3 = 100;
input PaintBars = No;
input HidePoints = 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);

def futurebar = rsi > rsi[-1];
def futurebar1 = RSI < RSI[-1];
plot RSI_Div_Down1 = if rsi != Highest(rsi, Lookback1) and close == Highest(close, Lookback1) and futurebar then midbodyval() else Double.NaN;
RSI_Div_Down1.SetPaintingStrategy(PaintingStrategy.POINTS);
RSI_Div_Down1.AssignValueColor(Color.PINK);
RSI_Div_Down1.SetLineWeight(3);
plot RSI_Div_Down2 = if rsi != Highest(rsi, Lookback2) and close == Highest(close, Lookback2) and futurebar then midbodyval() else Double.NaN;
RSI_Div_Down2.SetPaintingStrategy(PaintingStrategy.POINTS);
RSI_Div_Down2.AssignValueColor(Color.RED);
RSI_Div_Down2.SetLineWeight(3);
plot RSI_Div_Down3 = if rsi != Highest(rsi, Lookback3) and close == Highest(close, Lookback3) and futurebar then midbodyval() else Double.NaN;
RSI_Div_Down3.SetPaintingStrategy(PaintingStrategy.POINTS);
RSI_Div_Down3.AssignValueColor(Color.DARK_RED);
RSI_Div_Down3.SetLineWeight(3);
plot RSI_Div_Up1 = if rsi != Lowest(rsi, Lookback1) and close == Lowest(close, Lookback1) and futurebar1 then midbodyval() else Double.NaN;
RSI_Div_Up1.SetPaintingStrategy(PaintingStrategy.POINTS);
RSI_Div_Up1.AssignValueColor(Color.GREEN);
RSI_Div_Up1.SetLineWeight(3);
plot RSI_Div_Up2 = if rsi != Lowest(rsi, Lookback2) and close == Lowest(close, Lookback2) and futurebar1 then midbodyval() else Double.NaN;
RSI_Div_Up2.SetPaintingStrategy(PaintingStrategy.POINTS);
RSI_Div_Up2.AssignValueColor(Color.DARK_GREEN);
RSI_Div_Up2.SetLineWeight(3);
plot RSI_Div_Up3 = if rsi != Lowest(rsi, Lookback3) and close == Lowest(close, Lookback3) and futurebar1 then midbodyval() else Double.NaN;
RSI_Div_Up3.SetPaintingStrategy(PaintingStrategy.POINTS);
RSI_Div_Up3.AssignValueColor(Color.CYAN);
RSI_Div_Up3.SetLineWeight(3);

RSI_DIV_Up1.sethiding(HidePoints);
RSI_DIV_Up2.sethiding(HidePoints);
RSI_DIV_Up3.sethiding(HidePoints);
RSI_DIV_Down1.sethiding(HidePoints);
RSI_DIV_Down2.sethiding(HidePoints);
RSI_DIV_Down3.sethiding(HidePoints);

AssignPriceColor(if !paintBars then Color.CURRENT else
if rsi != Highest(rsi, Lookback1) and close == Highest(close, Lookback1) and futurebar then Color.PINK else
if rsi != Highest(rsi, Lookback2) and close == Highest(close, Lookback2) and futurebar then Color.RED else
if rsi != Highest(rsi, Lookback3) and close == Highest(close, Lookback3) and futurebar then Color.DARK_RED else
if rsi != Lowest(rsi, Lookback1) and close == Lowest(close, Lookback1) and futurebar1 then Color.GREEN else
if rsi != Lowest(rsi, Lookback2) and close == Lowest(close, Lookback2) and futurebar1 then Color.DARK_GREEN else
if rsi != Lowest(rsi, Lookback3) and close == Lowest(close, Lookback3) and futurebar1 then Color.CYAN else Color.BLACK );
 
@BenTen I'm new to this site and before I ask my question I want to thank you and the others that contribute, this site is a godsend!

I've been looking for a SCAN like this for years but to no avail. I was able to successfully copy & paste code into studies however I was not able to utilize it for scan purposes even after following the directions contained within regarding manually entering "RSI_With_Divergence" from drop down. There is no such option. What am I doing wrong?

Thank you in advance!
 
@BenTen I'm new to this site and before I ask my question I want to thank you and the others that contribute, this site is a godsend!

I've been looking for a SCAN like this for years but to no avail. I was able to successfully copy & paste code into studies however I was not able to utilize it for scan purposes even after following the directions contained within regarding manually entering "RSI_With_Divergence" from drop down. There is no such option. What am I doing wrong?

Thank you in advance!
Yep, im on the same boat brotha
Darn thing will not spit out any tickers when i scan

be sure to let me know if you ever figure it out...
 
I'm trying to create an indicator that plots the real divergence. So when a peak is formed in the RSI and the price action. I can have both changes ratio's plotted for a visualization of the ACTUAL divergence, and not a plot of RSI attempting to find higher highs, lower lows, etc.

I have spent days failing at making this, so any feedback is appreciated. Thanks for your time.
 
@Necrosway You're better off identifying these "real" divergences yourself. I don't think there is a way to put it into an indicator.
 
@imburger Here is the modified version with up and down arrows. See if that works for you.

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.

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;

def RSI = 50 * (ChgRatio + 1);

def OverSold = Over_Sold;

def 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;

def PlotHline = pivotHigh;

def pivotLow = if LowPivots

                then CurrPivotL

                else double.NaN;

def PivotDot = if !isNaN(pivotHigh)

                then pivotHigh

                else if !isNaN(pivotLow)

                     then pivotLow

                     else double.NaN;

plot uparrow = pivotLow;
uparrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
plot downarrow = pivotHigh;
downarrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

# End Code RSI with Divergence
 
Hi BenTen,

Thanks for the upper section arrow signal.

Actually can I have the arrows, alert with sound and alert bar into the lower section together with RSI Divergence indicator


i6MV96H.jpg
 
@imburger Add the following to the bottom of your script for alerts:

Code:
# Alerts
Alert(uparrow, " ", Alert.Bar, Sound.Chimes);
Alert(downarrow, " ", Alert.Bar, Sound.Bell);
 
@chewie76 The one that you drew manually? Either by drawing it using the drawing toolkit, or you will have to code it. From what I know, divergences are best when identified manually :)
 
@chewie76 The one that you drew manually? Either by drawing it using the drawing toolkit, or you will have to code it. From what I know, divergences are best when identified manually :)
I drew them, was hoping this Mobius RSI Divergence would point them out.
 
I came across this interesting indicator today in my ThinkScript archives and decided to share it here for other UTS members and visitors. Based on the notes in the code, it seems to have been originally shared in the ThinkScript Lounge back in August 2015, and was subsequently modified by other coders. I have no idea where or when I obtained it, and I do not use it for trading. If anyone finds a way to use it, let me know how it works. :)

Thinkscript-Study.png

Think-Script-Study-2.png


Code:
# Dilbert_RsiDivergence_with_HiddenDivergence
# V1.4  - 091615  - Dilbert  -  Add hidden divergence
# V1.3  - 082715  - Dilbert  -  Add LastHighAtRsiPivotHigh and LastLowAtRsiPivotLow
# V1.2  - 082615  - zzz      -  Limit plots to == count
# V1.1  - 082615  - Original author unknown, posted to chat by Jr2146
input pricecolor = yes;
input count = 6;#Hint count: number of plots to display
#hint: recognizes and signals divergences between rsi and price.
input n=3;
#hint n: controls how the slow pivot level is recognized. n=3 means that a slow pivot level is recognized when a high/low is preceded by 3 equal or lesser/greater (resp.) high's/low's and followed by 3 lesser/greater high's/low's.

input length = 14;
input ob  = 70;
input ob2 = 78;
input os = 29;
input os2 = 10;

def highPrice = StDev(high, 10);
def lowPrice = StDev(low, 10);

def highAvgUp = ExpAverage(if high > high[1] then highPrice else 0, length);
def highAvgDown = ExpAverage(if high < high[1] then highPrice else 0, length);

def lowAvgUp = ExpAverage(if low > low[1] then lowPrice else 0, length);
def lowAvgDown = ExpAverage(if low < low[1] then lowPrice else 0, length);

def highRVI = 100 - 100 / (1 + highAvgUp / highAvgDown);
def lowRVI = 100 - 100 / (1 + lowAvgUp / lowAvgDown);

def r = (highRVI + lowRVI) / 2;


def isRsiHigh = CompoundValue(n,
    r[n] == Highest(r, n * 2 + 1) && r[n] > r && fold i = 1 to n with x = 1 while x == 1 do
        r[n] > GetValue(r, i)
    , 0
);

def isRsiLow = CompoundValue(n,
    r[n] == Lowest(r, n * 2 + 1) && r[n] < r && fold j = 1 to n with y=1 while y == 1 do
        r[n] < GetValue(r, j)
    , 0
);

def rsiPivotHigh = CompoundValue(n,
    if isRsiHigh then r[n] else rsiPivotHigh[1],
    0
);
def highAtRsiPivotHigh = CompoundValue(n,
    if isRsiHigh then Max(high[n], high[n - 1]) else highAtRsiPivotHigh[1],
    0
);
def rsiPivotLow = CompoundValue(n,
    if isRsiLow then r[n] else rsiPivotLow[1],
    0
);
def lowAtRsiPivotLow = CompoundValue(n,
    if isRsiLow then Min(low[n], low[n - 1]) else lowAtRsiPivotLow[1],
    0
);
def TheBar = BarNumber();

plot divergenceColors = Double.NaN;
divergenceColors.DefineColor( "high divergence", Color.Yellow);
divergenceColors.DefineColor( "low divergence", Color.Yellow);
divergenceColors.HideTitle();

def isFastRsiHigh = r[1] > r && r[2] <= r[1] && r[3] <= r[1];
def isFastRsiLow = r[1] < r  && r[2] >= r[1] && r[3] >= r[1];
def fastPriceHigh = Max(high, high[1]);
def fastPriceLow = Min(low, low[1]);

def isHigherFastRsiHigh = isFastRsiHigh && r[1] < rsiPivotHigh[1];
def isLowerFastRsiLow = isFastRsiLow && r[1] > rsiPivotLow[1];
def highDivergence = (isHigherFastRsiHigh && fastPriceHigh >= highAtRsiPivotHigh[1]);
def lowDivergence = (isLowerFastRsiLow && fastPriceLow <= lowAtRsiPivotLow[1]);

############################################################################################

#####
input crosscount = 2;
def cond2 = highDivergence;
rec dataCount2 = CompoundValue(1, if (cond2) == 1 then dataCount2[1] + 1 else dataCount2[1], 0);
plot CrossDn = if HighestAll(dataCount2) - dataCount2 <= crosscount - 1 and highDivergence then 1 else Double.NaN;
def cond3 = lowDivergence;
rec dataCount3 = CompoundValue(1, if (cond3) == 1 then dataCount3[1] + 1 else dataCount3[1], 0);
plot CrossUp = if HighestAll(dataCount3) - dataCount3 <= crosscount - 1 and lowDivergence then 1 else Double.NaN;
CrossUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
CrossDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
CrossUp.SetDefaultColor(Color.MAGENTA);
CrossDn.SetDefaultColor(Color.CYAN);

AssignPriceColor(
    if HighestAll(dataCount2) - dataCount2 <= crosscount - 1 and highDivergence then divergenceColors.Color("high divergence")
    else if HighestAll(dataCount3) - dataCount3 <= crosscount - 1 and lowDivergence then divergenceColors.Color("low divergence")
       else Color.CURRENT);

alert(highDivergence,"HighDivergence", sound = Sound.Ring, "alert type" = Alert.BAR);

################################
input displace = 0;
input MA1_length = 8;
input MA2_length = 21;
input price = close;
input showverticals = yes;
input number_verticals_displayed = 1;

DefineGlobalColor("RisingMA", Color.BLUE);
DefineGlobalColor("FallingMA", Color.RED);

input movingAverageType1 = {default Simple, Exponential, Weighted, Hull, Variable};
input movingAverageType2 = {default Variable, Simple, Exponential, Weighted, Hull};

def data1;

switch (movingAverageType1) {
case Simple:
    data1 = CompoundValue(1, Average(price[-displace], MA1_length), price);
case Exponential:
    data1 = CompoundValue(1, ExpAverage(price[-displace], MA1_length), price);
case Weighted:
    data1 = CompoundValue(1, WMA(price[-displace], MA1_length), price);
case Hull:
    data1 = CompoundValue(1, HullMovingAvg(price[-displace], MA1_length), price);
case Variable:
    data1 = CompoundValue(1, VariableMA(price = price, length = MA1_length), price);
}

plot DoubleMA;

switch (movingAverageType2) {
case Simple:
    DoubleMA = CompoundValue(1, Average(data1[-displace], MA2_length), data1);
case Exponential:
    DoubleMA = CompoundValue(1, ExpAverage(data1[-displace], MA2_length), data1);
case Weighted:
    DoubleMA = CompoundValue(1, WMA(data1[-displace], MA2_length), data1);
case Hull:
    DoubleMA = CompoundValue(1, HullMovingAvg(data1[-displace], MA2_length), data1);
case Variable:
    DoubleMA = CompoundValue(1, VariableMA( data1, MA2_length), data1);
}

DoubleMA.SetLineWeight(2);
DoubleMA.AssignValueColor(if DoubleMA > DoubleMA[1] then GlobalColor("RisingMA") else GlobalColor("FallingMA"));
DoubleMA.HideBubble();


DoubleMA.SetLineWeight(2);
DoubleMA.AssignValueColor(if DoubleMA > DoubleMA[1] then GlobalColor("RisingMA") else GlobalColor("FallingMA"));


# Verticals
def condgreen =  if DoubleMA > DoubleMA[1] then 1 else 0;
def count1 = number_verticals_displayed;
def cond1 = if condgreen[1] == 0 and condgreen or condgreen[1] == 1 and condgreen == 0 then 1 else Double.NaN;
rec dataCount1 = CompoundValue(1, if !IsNaN(cond1) then dataCount1[1] + 1 else dataCount1[1], 0);
AddVerticalLine(HighestAll(dataCount1) - dataCount1 <= count1 - 1 and showverticals and condgreen[1] == 0 and condgreen , "                                 Trend Rising     " + Round(DoubleMA, 2), Color.BLUE, Curve.FIRM);
AddVerticalLine(HighestAll(dataCount1) - dataCount1 <= count1 - 1 and showverticals and condgreen[1] == 1 and condgreen == 0 , "                                 Trend Falling     " + Round(DoubleMA, 2), Color.RED, Curve.FIRM);

input showcloud = yes;
input pct_above_below = 1.0;
plot UpperBand = DoubleMA * (1 + pct_above_below / 100);
UpperBand.AssignValueColor(if DoubleMA > DoubleMA[1] then GlobalColor("RisingMA") else GlobalColor("FallingMA"));
plot LowerBand = DoubleMA * (1 - pct_above_below / 100);
LowerBand.AssignValueColor(if DoubleMA > DoubleMA[1] then GlobalColor("RisingMA") else GlobalColor("FallingMA"));
input number_clouds_displayed = 3;
rec dataCount4 = CompoundValue(1, if !IsNaN(cond1) then dataCount4[1] + 1 else dataCount4[1], 0);
def dcount4 = dataCount4;
AddCloud(if HighestAll(dCount4) - dCount4 <= number_clouds_displayed - 1 and showcloud and DoubleMA > DoubleMA[1] then UpperBand else Double.NaN, LowerBand, GlobalColor("RisingMA"), GlobalColor("RisingMA"));
AddCloud(if HighestAll(dCount4) - dCount4 <= number_clouds_displayed - 1 and showcloud and DoubleMA < DoubleMA[1] then UpperBand else Double.NaN, LowerBand, GlobalColor("FallingMA"), GlobalColor("FallingMA"));


#Arrow Option
input showarrows = yes;
input arrow_mover_updown = 1;
plot greenarrow = if HighestAll(dataCount1) - dataCount1 <= count1 - 1 and showarrows and condgreen[1] == 0 and condgreen then doublema - TickSize() * arrow_mover_updown else Double.NaN;
greenarrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
greenarrow.SetDefaultColor(Color.BLUE);
greenarrow.SetLineWeight(3);
greenarrow.HideBubble();
plot redarrow = if HighestAll(dataCount1) - dataCount1 <= count1 - 1 and showarrows and condgreen[1] == 1 and condgreen == 0 then doublema + TickSize() * arrow_mover_updown else Double.NaN;
redarrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
redarrow.SetDefaultColor(Color.RED);
redarrow.SetLineWeight(3);
redarrow.HideBubble();

#Bar Color

def green = if condgreen[1] == 0 and condgreen then 1 else if green[1]==1 and !(condgreen[1] == 1 and condgreen == 0) then 1 else 0;

AssignPriceColor(if pricecolor then if Between(r, 87, 95) or between(r,5,13) then color.GREEN else if r > 95 or r < 5 then color.WHITE else if green==0 then color.red else color.BLUE else Color.CURRENT);
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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