Divergence Confirmation Indicator for ThinkorSwim

BenTen

BenTen

Administrative
Staff
VIP
This indicator paints your candlesticks when a bullish divergence is found on both the RSI and the Awesome Oscillator and red when bearish divergence is found.



thinkScript Code

Code:
# Confirmed Divergence
# Assembled by BenTen at useThinkScript.com
# Converted from https://www.tradingview.com/script/PFsMAjcQ-Confirmed-Divergence/

input rsiprd = 14;
input source = close;
input averageType = AverageType.WILDERS;

def NetChgAvg = MovingAverage(averageType, source - source[1], rsiprd);
def TotChgAvg = MovingAverage(averageType, AbsValue(source - source[1]), rsiprd);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

def RSI = 50 * (ChgRatio + 1);

input length = 30;
input mult = 2.0;
input src = close;

input ob = 70;
input os = 30;
input lengthAO1 = 5;
input lengthAO2 = 34;
def rv = RSI;

def AO = simpleMovingAvg((high + low) / 2, lengthAO1) - simpleMovingAvg((high + low) / 2, lengthAO2);

input x = 5;
input z = 25;

def srcLL = src > lowest(src, x) and lowest(src, x)<lowest(src, z)[x];

def rsiHL = rv > lowest(rv, x) and lowest(rv, x) > lowest(rv, z)[x] and lowest(rv, z)<os;

def aoHL = AO > lowest(AO, x) and lowest(AO, x) > lowest(AO, z)[x] and lowest(AO, x) < 0;

def BullishDiv = srcLL and rsiHL and aoHL;

def srcHH = src < highest(src, x) and highest(src, x)>highest(src, z)[x];

def rsiLH = rv < highest(rv, x) and highest(rv, x) < highest(rv, z)[x] and highest(rv, z)>ob;

def aoLH = AO < highest(AO, x) and highest(AO, x) < highest(AO, z)[x] and highest(AO, x) > 0;

def BearishDiv = srcHH and rsiLH and aoLH;

def basis = simpleMovingAvg(source, length);
def dev = mult * stdev(source, length);

def upper = basis + dev;
def lower = basis - dev;

assignPriceColor(if BullishDiv then color.green else if BearishDiv then color.red else Color.white);
 
I

iamtom07

New member
2019 Donor
VIP
Ben, instead of painting the bars, would you display the arrow of the first bar of divergence?
Thanks,
 
BenTen

BenTen

Administrative
Staff
VIP
@iamtom07 Add the # sign in front of the assignPriceColor code and add the following script at the end of your script:

Code:
# Plot Signals
plot bullish = BullishDiv;
bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
bullish.SetDefaultColor(Color.CYAN);
bullish.SetLineWeight(1);
plot bearish = BearishDiv;
bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
bearish.SetDefaultColor(Color.CYAN);
bearish.SetLineWeight(1);
 
S

scxpertise

New member
I mainly swing trades, is this useful for longer timeframes for Daily. Do you have TOS scanners that scan for stocks in a channel b.c I like to use Iron Condor strategy. Thanks
 
BenTen

BenTen

Administrative
Staff
VIP
@scxpertise You can always backtest it by looking through your chart's timeframe with this indicator. Add this code to the end of your script and you should be able to scan for new signals. (Watch this video if you don't know how to).
 
YungTraderFromMontana

YungTraderFromMontana

Well-known member
How should I modify the settings if I want the conditions to be more lenient. I'm liking this but I want to see what happens if the divergence conditions are easier to reach.
 
A

Art

Member
I mainly swing trades, is this useful for longer timeframes for Daily. Do you have TOS scanners that scan for stocks in a channel b.c I like to use Iron Condor strategy. Thanks
Wanted to let you know that I get false signals on 4 hourly chart
 
BenTen

BenTen

Administrative
Staff
VIP
@YungTraderFromMontana I guess you can try to modify the overbought and oversold conditions.
 
RickKennedy

RickKennedy

Member
Hey @BenTen .....this is awesome! Very useful as I am a reversion to the mean trader. I'm also interested in making the conditions more lenient. I've reset OB/OS to 60/40 with some success and have also set the AO to 5, 20 instead of 5,34. There is one thing that I have a big question about tho. Here's the setup: I find that there are often strong "divergences" when price has simply met the closing price but has not actually met or exceeded (let's say) the lowest low. See the image below. At 9:10 I see a great "divergence", but price hasn't actually met the conditions. I see in your code for this that there is a lot of reference to "highest" and "lowest", but is there a way to modify the code to include conditions where price has simply met the lowest "close" and not the lowest "low"?

Thanks!

 

Similar threads

Top