• Get $40 off VIP by signing up for a free account! Sign Up

Divergence Scanner Help


New member
Hello - I'm trying to build a scanner in thinkscript but I am stuck. Any ideas how to make this one work? It is based off the last section of this indicator at https://usethinkscript.com/threads/rsi-or-macd-with-vwap-ma-div-for-thinkorswim.12971/#post-126221

Ideally the scan would find stocks with the buy signal.

Below is what I've got so far:

# Input Parameters
input PivotLookback = 5; # Pivot Lookback
input DivBull = yes; # Plot Bullish Bubbles
input DivBear = yes; # Plot Bearish Bubbles

# Define Data Sources
def divSrc = WildersSmoothing(RSI(Length = 14), 2);
def h = high;
def l = low;

# Function to Find Pivots
script FindPivots {
input dat = close; # Data being evaluated
input HL = 0; # High or low pivot designation: -1 for low, +1 for high
input lbL = 5; # Pivot Lookback Left
input lbR = 1; # Pivot Lookback Right
def _nan; # Placeholder for non-number returns
def _BN; # Current barnumber
def _VStop; # Confirmation that the lookforward period continues the pivot trend
def _V; # Value at the actual pivot point

_BN = BarNumber();
_nan = Double.NaN;
_VStop = if !isNaN(dat) and lbR > 0 and lbL > 0 then
fold a = 1 to lbR + 1 with b = 1 while b
do if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
if (HL > 0) {
_V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
then dat
else _nan;
} else {
_V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
then dat
else _nan;
plot result = if !IsNaN(_V) and _VStop then _V else _nan;

# Find Pivots for Highs and Lows
def pl = FindPivots(divSrc, -1, PivotLookback, PivotLookback);
def ph = FindPivots(divSrc, 1, PivotLookback, PivotLookback);

# Check for Found Pivots
def plFound = !isNaN(pl);
def phFound = !isNaN(ph);

# Find Previous High and Low Values
def vlFound = if plFound then GetValue(divSrc, 1) else Double.NaN;
def vhFound = if phFound then GetValue(divSrc, 1) else Double.NaN;

# Find Previous High and Low Prices
def plPrice = if plFound then GetValue(l, 1) else Double.NaN;
def phPrice = if phFound then GetValue(h, 1) else Double.NaN;

# Bullish Conditions
def oscHL = divSrc > vlFound and plFound[1];
def priceLL = l < plPrice;
def bullCond = DivBull and plFound and oscHL and priceLL;

# Bearish Conditions
def oscLH = divSrc < vhFound and phFound[1];
def priceHH = h > phPrice;
def bearCond = DivBear and phFound and oscLH and priceHH;

# Output Bullish and Bearish Signals
plot bullishSignal = if bullCond then low else Double.NaN;

plot bearishSignal = if bearCond then high else Double.NaN;
Last edited by a moderator:

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
373 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.