# Divergence Scanner Help

#### Tidegolf

##### New member
VIP
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;
bullishSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
bullishSignal.SetDefaultColor(Color.GREEN);

plot bearishSignal = if bearCond then high else Double.NaN;
bearishSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
bearishSignal.SetDefaultColor(Color.RED);

Last edited by a moderator:
what exactly is the buy signal?

### Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
373 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• 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?