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);
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: