RSI Engulfing with TDI Filter [WizardofLittleElm] For ThinkOrSwim

TRADERSAM

Active member
VIP
Author states: Buy and Sell Signal based on RSI, Engulfing Candle, and TDI

Screenshot 2025-11-09 at 8.28.04 PM.png


Here’s another indicator I converted today and thought I’d share with the community. I’ve been working on translating more tools from TradingView into Thinkorswim, and this one felt worth bringing over. If anyone has suggestions or improvements, I’m always open to feedback and learning more.

NOTE: I did switch the background shading to labels, since TOS background fills can be a little intense.

Read more about the indicator: https://www.tradingview.com/script/Mthjoa0S-RSI-Engulfing-with-TDI-Filter/

Here is the code:
Ruby:
# -------------------------------------------------
# RSI Engulfing With TDI Filter[WizardofLittleElm]
# Converted by @TraderSam
# Converted on 11/09/25
# -------------------------------------------------

# === General Inputs ===
declare upper;
input LabelSize = fontsize.medium;
input LabelLocation = location.top_left;

# === RMA Helper ===
script RMA {
    input price = close;
    input length = 14;

    def rma = if BarNumber() == 1
        then price
        else (rma[1] * (length - 1) + price) / length;

    plot out = rma;
}

# === Engulfing Candle Logic ===
def bullishcandle = close >= open[1] and close[1] < open[1];
def bearishcandle = close <= open[1] and close[1] > open[1];

# === RSI With Smoothing ===
input rsiSource = close;
input rsiLength = 14;
input rsiOverBought = 70;
input rsiOverSold = 30;

# RSI Smoothing
input usesmoothing = yes;
input smoothType = {default SMA, EMA, WMA, RMA};
input smoothLength = 3;

# Calculate RSI
def rsiraw = RSI(rsiLength, rsiSource);

# Apply Smoothing
def rsivalue = if usesmoothing then (if smoothType == smoothType.SMA then SimpleMovingAvg(rsiraw, smoothLength) else if smoothType == smoothType.EMA then ExpAverage(rsiraw, smoothLength) else if smoothType == smoothType.WMA then WMA(rsiraw, smoothlength) else if smoothType == smoothType.RMA then rma(rsiraw, smoothLength) else rsiraw) else Double.NaN;

# RSI Conditions
def isrsiob = rsivalue >= rsiOverBought;
def isrsios = rsivalue <= rsiOverSold;

# Base Entry Signals
def baseshortsignal = (isrsiob or isrsiob[1] or isrsiob[2]) and bearishcandle;
def baselongsignal = (isrsios or isrsios[1] or isrsios[2]) and bullishcandle;

# === TDI Filter ===
def tdirsi = RSI(8, close);
def tdima = SimpleMovingAvg(tdirsi, 34);
def tdioff = 1.6185 * StDev(tdirsi, 34);
def tdiup = tdima + tdioff;
def tdidn = tdima - tdioff;
def tdimid = (tdiup + tdidn) / 2;
def tdifastma = SimpleMovingAvg(tdirsi, 2);

# Filter Mode Selection
input use_TDI_filter = yes;
input FilterMode = {default Option1, Option2, Option3};

# Threshold Settings
input LongThreshold = 30;
input ShortThreshold = 70;

# Lookback Period
input tdiLookback = 3;

# Option1
def option1_validshort = tdifastma > ShortThreshold;
def option1_validlong = tdifastma < LongThreshold;

# Option 2
def option2_validshort = tdifastma > tdiup;
def option2_validlong = tdifastma < tdidn;

# Option 3
def option3_validshort = tdifastma > tdiup and tdifastma > ShortThreshold;
def option3_validlong = tdifastma < tdidn and tdifastma < LongThreshold;

# Apply Filter
def tdivalidshortnow = if FilterMode == FilterMode.Option1 then option1_validshort else if FilterMode == FilterMode.Option2 then option2_validshort else option3_validshort;
def tdivalidlongnow = if FilterMode == FilterMode.Option1 then option1_validlong else if FilterMode == FilterMode.Option2 then option2_validlong else option3_validlong;

# Apply LookBack
def tdivalidshort = tdivalidshortnow[0] or (tdiLookback >= 2 and tdivalidshortnow[1]) or (tdiLookback >= 3 and tdivalidshortnow[2]) or (tdiLookback >= 4 and tdivalidshortnow[3]) or (tdiLookback >= 5 and tdivalidshortnow[4]) or (tdiLookback >= 6 and tdivalidshortnow[5]) or (tdiLookback >= 7 and tdivalidshortnow[6]) or (tdiLookback >= 8 and tdivalidshortnow[7]) or (tdiLookback >= 9 and tdivalidshortnow[8]) or (tdiLookback >= 10 and tdivalidshortnow[9]);

def tdiValidLong = tdivalidlongnow[0] or (tdiLookback >= 2 and tdivalidlongnow[1]) or (tdiLookback >= 3 and tdivalidlongnow[2]) or (tdiLookback >= 4 and tdivalidlongnow[3]) or (tdiLookback >= 5 and tdivalidlongnow[4]) or (tdiLookback >= 6 and tdivalidlongnow[5]) or (tdiLookback >= 7 and tdivalidlongnow[6]) or (tdiLookback >= 8 and tdivalidlongnow[7]) or (tdiLookback >= 9 and tdivalidlongnow[8]) or (tdiLookback >= 10 and tdivalidlongnow[9]);

# === EMA Trend Filter ===
input useEMAFilter = yes;
input emaLength = 200;
input reverseEMA = no;

# Calculate EMA
def trendEMA = expaverage(close, emalength);

# Determine Trend Direction
def priceaboveema = close > trendema;
def pricebelowema = close < trendema;

# Apply normal or reversed logic
def emavalidlong = if reverseema then pricebelowema else priceaboveema;
def emavalidshort = if reverseema then priceaboveema else pricebelowema;

# Plot EMA;
plot EMAplot = if useEMAFilter then trendEMA else double.nan;
emaplot.setdefaultcolor(color.blue);
emaplot.setlineweight(2);

# === Final Entry Signals ===
def finalshortsignal = baseshortsignal and (if use_tdi_filter then tdivalidshort else yes) and (if useEMAFilter then emaValidShort else yes);
def finalLongSignal = baseLongSignal and (if use_TDI_Filter then tdiValidLong else yes) and (if useEMAFilter then emaValidLong else yes);

# === Plots ===
plot long = if finallongsignal then low else double.nan;
long.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
long.setdefaultcolor(color.green);
long.setlineweight(2);
plot short = if finalshortsignal then high else double.nan;
short.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_Down);
short.setdefaultcolor(color.red);
short.setlineweight(2);

AddLabel(use_tdi_filter, "SHORT" + " ", if tdivalidshort then Color.RED else Color.Dark_gray, labellocation, labelsize);
AddLabel(use_tdi_filter, "LONG" + " ", if tdivalidlong then Color.green else Color.Dark_GRAY, labellocation, labelsize);

# === Alerts ===
Alert(Finallongsignal, "Long Entry Signal", Alert.BAR, Sound.Ding);
Alert(finalshortsignal, "Short Entry Signal", Alert.BAR, Sound.Ding);
 
Last edited by a moderator:
What about calculating Engulfing like this?


Python:
def delta = close - open;
def bodyMax = Max(open, close);
def bodyMin = Min(open, close);
def isEngulfing = bodyMax > bodyMax[1] && bodyMin < bodyMin[1];
def bullishcandle = delta > 0 && IsEngulfing;
def bearishcandle = delta < 0 && IsEngulfing;
 
What about calculating Engulfing like this?


Python:
def delta = close - open;
def bodyMax = Max(open, close);
def bodyMin = Min(open, close);
def isEngulfing = bodyMax > bodyMax[1] && bodyMin < bodyMin[1];
def bullishcandle = delta > 0 && IsEngulfing;
def bearishcandle = delta < 0 && IsEngulfing;
I think this would work great because it's much more in line with a textbook engulf. I originally had it coded the way I did just because that was how it was written in the original TradingView script. I like your approach though, it definitely feels like a cleaner confirmation of a true engulfing candle. I’ll take it for a spin and see how it performs...
 
Author states: Buy and Sell Signal based on RSI, Engulfing Candle, and TDI

View attachment 26195

Here’s another indicator I converted today and thought I’d share with the community. I’ve been working on translating more tools from TradingView into Thinkorswim, and this one felt worth bringing over. If anyone has suggestions or improvements, I’m always open to feedback and learning more.

NOTE: I did switch the background shading to labels, since TOS background fills can be a little intense.

Read more about the indicator: https://www.tradingview.com/script/Mthjoa0S-RSI-Engulfing-with-TDI-Filter/

Here is the code:
Ruby:
# -------------------------------------------------
# RSI Engulfing With TDI Filter[WizardofLittleElm]
# Converted by @TraderSam
# Converted on 11/09/25
# -------------------------------------------------

# === General Inputs ===
declare upper;
input LabelSize = fontsize.medium;
input LabelLocation = location.top_left;

# === RMA Helper ===
script RMA {
    input price = close;
    input length = 14;

    def rma = if BarNumber() == 1
        then price
        else (rma[1] * (length - 1) + price) / length;

    plot out = rma;
}

# === Engulfing Candle Logic ===
def bullishcandle = close >= open[1] and close[1] < open[1];
def bearishcandle = close <= open[1] and close[1] > open[1];

# === RSI With Smoothing ===
input rsiSource = close;
input rsiLength = 14;
input rsiOverBought = 70;
input rsiOverSold = 30;

# RSI Smoothing
input usesmoothing = yes;
input smoothType = {default SMA, EMA, WMA, RMA};
input smoothLength = 3;

# Calculate RSI
def rsiraw = RSI(rsiLength, rsiSource);

# Apply Smoothing
def rsivalue = if usesmoothing then (if smoothType == smoothType.SMA then SimpleMovingAvg(rsiraw, smoothLength) else if smoothType == smoothType.EMA then ExpAverage(rsiraw, smoothLength) else if smoothType == smoothType.WMA then WMA(rsiraw, smoothlength) else if smoothType == smoothType.RMA then rma(rsiraw, smoothLength) else rsiraw) else Double.NaN;

# RSI Conditions
def isrsiob = rsivalue >= rsiOverBought;
def isrsios = rsivalue <= rsiOverSold;

# Base Entry Signals
def baseshortsignal = (isrsiob or isrsiob[1] or isrsiob[2]) and bearishcandle;
def baselongsignal = (isrsios or isrsios[1] or isrsios[2]) and bullishcandle;

# === TDI Filter ===
def tdirsi = RSI(8, close);
def tdima = SimpleMovingAvg(tdirsi, 34);
def tdioff = 1.6185 * StDev(tdirsi, 34);
def tdiup = tdima + tdioff;
def tdidn = tdima - tdioff;
def tdimid = (tdiup + tdidn) / 2;
def tdifastma = SimpleMovingAvg(tdirsi, 2);

# Filter Mode Selection
input use_TDI_filter = yes;
input FilterMode = {default Option1, Option2, Option3};

# Threshold Settings
input LongThreshold = 30;
input ShortThreshold = 70;

# Lookback Period
input tdiLookback = 3;

# Option1
def option1_validshort = tdifastma > ShortThreshold;
def option1_validlong = tdifastma < LongThreshold;

# Option 2
def option2_validshort = tdifastma > tdiup;
def option2_validlong = tdifastma < tdidn;

# Option 3
def option3_validshort = tdifastma > tdiup and tdifastma > ShortThreshold;
def option3_validlong = tdifastma < tdidn and tdifastma < LongThreshold;

# Apply Filter
def tdivalidshortnow = if FilterMode == FilterMode.Option1 then option1_validshort else if FilterMode == FilterMode.Option2 then option2_validshort else option3_validshort;
def tdivalidlongnow = if FilterMode == FilterMode.Option1 then option1_validlong else if FilterMode == FilterMode.Option2 then option2_validlong else option3_validlong;

# Apply LookBack
def tdivalidshort = tdivalidshortnow[0] or (tdiLookback >= 2 and tdivalidshortnow[1]) or (tdiLookback >= 3 and tdivalidshortnow[2]) or (tdiLookback >= 4 and tdivalidshortnow[3]) or (tdiLookback >= 5 and tdivalidshortnow[4]) or (tdiLookback >= 6 and tdivalidshortnow[5]) or (tdiLookback >= 7 and tdivalidshortnow[6]) or (tdiLookback >= 8 and tdivalidshortnow[7]) or (tdiLookback >= 9 and tdivalidshortnow[8]) or (tdiLookback >= 10 and tdivalidshortnow[9]);

def tdiValidLong = tdivalidlongnow[0] or (tdiLookback >= 2 and tdivalidlongnow[1]) or (tdiLookback >= 3 and tdivalidlongnow[2]) or (tdiLookback >= 4 and tdivalidlongnow[3]) or (tdiLookback >= 5 and tdivalidlongnow[4]) or (tdiLookback >= 6 and tdivalidlongnow[5]) or (tdiLookback >= 7 and tdivalidlongnow[6]) or (tdiLookback >= 8 and tdivalidlongnow[7]) or (tdiLookback >= 9 and tdivalidlongnow[8]) or (tdiLookback >= 10 and tdivalidlongnow[9]);

# === EMA Trend Filter ===
input useEMAFilter = yes;
input emaLength = 200;
input reverseEMA = no;

# Calculate EMA
def trendEMA = expaverage(close, emalength);

# Determine Trend Direction
def priceaboveema = close > trendema;
def pricebelowema = close < trendema;

# Apply normal or reversed logic
def emavalidlong = if reverseema then pricebelowema else priceaboveema;
def emavalidshort = if reverseema then priceaboveema else pricebelowema;

# Plot EMA;
plot EMAplot = if useEMAFilter then trendEMA else double.nan;
emaplot.setdefaultcolor(color.blue);
emaplot.setlineweight(2);

# === Final Entry Signals ===
def finalshortsignal = baseshortsignal and (if use_tdi_filter then tdivalidshort else yes) and (if useEMAFilter then emaValidShort else yes);
def finalLongSignal = baseLongSignal and (if use_TDI_Filter then tdiValidLong else yes) and (if useEMAFilter then emaValidLong else yes);

# === Plots ===
plot long = if finallongsignal then low else double.nan;
long.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
long.setdefaultcolor(color.green);
long.setlineweight(2);
plot short = if finalshortsignal then high else double.nan;
short.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_Down);
short.setdefaultcolor(color.red);
short.setlineweight(2);

AddLabel(use_tdi_filter, "SHORT" + " ", if tdivalidshort then Color.RED else Color.Dark_gray, labellocation, labelsize);
AddLabel(use_tdi_filter, "LONG" + " ", if tdivalidlong then Color.green else Color.Dark_GRAY, labellocation, labelsize);

# === Alerts ===
Alert(Finallongsignal, "Long Entry Signal", Alert.BAR, Sound.Ding);
Alert(finalshortsignal, "Short Entry Signal", Alert.BAR, Sound.Ding);
I like the large labels (fits the old saying: "Large enough for The King of England to see without his glasses on")

View attachment 26195

Here’s another indicator I converted today and thought I’d share with the community. I’ve been working on translating more tools from TradingView into Thinkorswim, and this one felt worth bringing over. If anyone has suggestions or improvements, I’m always open to feedback and learning more.

NOTE: I did switch the background shading to labels, since TOS background fills can be a little intense.

Read more about the indicator: https://www.tradingview.com/script/Mthjoa0S-RSI-Engulfing-with-TDI-Filter/

Here is the code:
Ruby:
# -------------------------------------------------
# RSI Engulfing With TDI Filter[WizardofLittleElm]
# Converted by @TraderSam
# Converted on 11/09/25
# -------------------------------------------------

# === General Inputs ===
declare upper;
input LabelSize = fontsize.medium;
input LabelLocation = location.top_left;

# === RMA Helper ===
script RMA {
    input price = close;
    input length = 14;

    def rma = if BarNumber() == 1
        then price
        else (rma[1] * (length - 1) + price) / length;

    plot out = rma;
}

# === Engulfing Candle Logic ===
def bullishcandle = close >= open[1] and close[1] < open[1];
def bearishcandle = close <= open[1] and close[1] > open[1];

# === RSI With Smoothing ===
input rsiSource = close;
input rsiLength = 14;
input rsiOverBought = 70;
input rsiOverSold = 30;

# RSI Smoothing
input usesmoothing = yes;
input smoothType = {default SMA, EMA, WMA, RMA};
input smoothLength = 3;

# Calculate RSI
def rsiraw = RSI(rsiLength, rsiSource);

# Apply Smoothing
def rsivalue = if usesmoothing then (if smoothType == smoothType.SMA then SimpleMovingAvg(rsiraw, smoothLength) else if smoothType == smoothType.EMA then ExpAverage(rsiraw, smoothLength) else if smoothType == smoothType.WMA then WMA(rsiraw, smoothlength) else if smoothType == smoothType.RMA then rma(rsiraw, smoothLength) else rsiraw) else Double.NaN;

# RSI Conditions
def isrsiob = rsivalue >= rsiOverBought;
def isrsios = rsivalue <= rsiOverSold;

# Base Entry Signals
def baseshortsignal = (isrsiob or isrsiob[1] or isrsiob[2]) and bearishcandle;
def baselongsignal = (isrsios or isrsios[1] or isrsios[2]) and bullishcandle;

# === TDI Filter ===
def tdirsi = RSI(8, close);
def tdima = SimpleMovingAvg(tdirsi, 34);
def tdioff = 1.6185 * StDev(tdirsi, 34);
def tdiup = tdima + tdioff;
def tdidn = tdima - tdioff;
def tdimid = (tdiup + tdidn) / 2;
def tdifastma = SimpleMovingAvg(tdirsi, 2);

# Filter Mode Selection
input use_TDI_filter = yes;
input FilterMode = {default Option1, Option2, Option3};

# Threshold Settings
input LongThreshold = 30;
input ShortThreshold = 70;

# Lookback Period
input tdiLookback = 3;

# Option1
def option1_validshort = tdifastma > ShortThreshold;
def option1_validlong = tdifastma < LongThreshold;

# Option 2
def option2_validshort = tdifastma > tdiup;
def option2_validlong = tdifastma < tdidn;

# Option 3
def option3_validshort = tdifastma > tdiup and tdifastma > ShortThreshold;
def option3_validlong = tdifastma < tdidn and tdifastma < LongThreshold;

# Apply Filter
def tdivalidshortnow = if FilterMode == FilterMode.Option1 then option1_validshort else if FilterMode == FilterMode.Option2 then option2_validshort else option3_validshort;
def tdivalidlongnow = if FilterMode == FilterMode.Option1 then option1_validlong else if FilterMode == FilterMode.Option2 then option2_validlong else option3_validlong;

# Apply LookBack
def tdivalidshort = tdivalidshortnow[0] or (tdiLookback >= 2 and tdivalidshortnow[1]) or (tdiLookback >= 3 and tdivalidshortnow[2]) or (tdiLookback >= 4 and tdivalidshortnow[3]) or (tdiLookback >= 5 and tdivalidshortnow[4]) or (tdiLookback >= 6 and tdivalidshortnow[5]) or (tdiLookback >= 7 and tdivalidshortnow[6]) or (tdiLookback >= 8 and tdivalidshortnow[7]) or (tdiLookback >= 9 and tdivalidshortnow[8]) or (tdiLookback >= 10 and tdivalidshortnow[9]);

def tdiValidLong = tdivalidlongnow[0] or (tdiLookback >= 2 and tdivalidlongnow[1]) or (tdiLookback >= 3 and tdivalidlongnow[2]) or (tdiLookback >= 4 and tdivalidlongnow[3]) or (tdiLookback >= 5 and tdivalidlongnow[4]) or (tdiLookback >= 6 and tdivalidlongnow[5]) or (tdiLookback >= 7 and tdivalidlongnow[6]) or (tdiLookback >= 8 and tdivalidlongnow[7]) or (tdiLookback >= 9 and tdivalidlongnow[8]) or (tdiLookback >= 10 and tdivalidlongnow[9]);

# === EMA Trend Filter ===
input useEMAFilter = yes;
input emaLength = 200;
input reverseEMA = no;

# Calculate EMA
def trendEMA = expaverage(close, emalength);

# Determine Trend Direction
def priceaboveema = close > trendema;
def pricebelowema = close < trendema;

# Apply normal or reversed logic
def emavalidlong = if reverseema then pricebelowema else priceaboveema;
def emavalidshort = if reverseema then priceaboveema else pricebelowema;

# Plot EMA;
plot EMAplot = if useEMAFilter then trendEMA else double.nan;
emaplot.setdefaultcolor(color.blue);
emaplot.setlineweight(2);

# === Final Entry Signals ===
def finalshortsignal = baseshortsignal and (if use_tdi_filter then tdivalidshort else yes) and (if useEMAFilter then emaValidShort else yes);
def finalLongSignal = baseLongSignal and (if use_TDI_Filter then tdiValidLong else yes) and (if useEMAFilter then emaValidLong else yes);

# === Plots ===
plot long = if finallongsignal then low else double.nan;
long.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
long.setdefaultcolor(color.green);
long.setlineweight(2);
plot short = if finalshortsignal then high else double.nan;
short.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_Down);
short.setdefaultcolor(color.red);
short.setlineweight(2);

AddLabel(use_tdi_filter, "SHORT" + " ", if tdivalidshort then Color.RED else Color.Dark_gray, labellocation, labelsize);
AddLabel(use_tdi_filter, "LONG" + " ", if tdivalidlong then Color.green else Color.Dark_GRAY, labellocation, labelsize);

# === Alerts ===
Alert(Finallongsignal, "Long Entry Signal", Alert.BAR, Sound.Ding);
Alert(finalshortsignal, "Short Entry Signal", Alert.BAR, Sound.Ding);
I like the large labels - large enough for The King of England to see without his glasses on!
 
I like the large labels - large enough for The King of England to see without his glasses on!
haha yeah I like them too! I learned that you could adjust the size of the labels just about a week ago and have been adding it to every script that I have labels in. 😂
 
What about calculating Engulfing like this?


Python:
def delta = close - open;
def bodyMax = Max(open, close);
def bodyMin = Min(open, close);
def isEngulfing = bodyMax > bodyMax[1] && bodyMin < bodyMin[1];
def bullishcandle = delta > 0 && IsEngulfing;
def bearishcandle = delta < 0 && IsEngulfing;
Ok been playing around with your logic a little and I really like it. It does make the script fires a lot less but when you get a signal they are normally very high quality... I am thinking about making an input to choose if you want the script to be strict(your engulfing logic) or a little looser(original logic). Thoughts?
 
Ok been playing around with your logic a little and I really like it. It does make the script fires a lot less but when you get a signal they are normally very high quality... I am thinking about making an input to choose if you want the script to be strict(your engulfing logic) or a little looser(original logic). Thoughts?
Well, if you really want to be strict about it, check out this concept I worked on a while back. This is how I’ve seen engulfing logic play out, but you can adapt it to your own style.

Python:
input PP_trendSetup = 3;
input PP_lenght = 20;
#
def isAscending = IsAscending(close, PP_trendSetup);
def isDescending = IsDescending(close, PP_trendSetup);
#
def delta = close - open;
#
def bodyMax = Max(open, close);
def bodyMin = Min(open, close);
#
def isEngulfing = bodyMax > bodyMax[1] && bodyMin < bodyMin[1];
#
def isDoji = IsDoji(PP_lenght, 0.05);
#
def bullishcandle = IsDescending[1] && ((delta[1] < 0) or isDoji[1]) && delta > 0 && IsEngulfing;
def bearishcandle = IsAscending[1] && ((delta[1] > 0) or isDoji[1]) && delta < 0 && IsEngulfing;
 
Well, if you really want to be strict about it, check out this concept I worked on a while back. This is how I’ve seen engulfing logic play out, but you can adapt it to your own style.

Python:
input PP_trendSetup = 3;
input PP_lenght = 20;
#
def isAscending = IsAscending(close, PP_trendSetup);
def isDescending = IsDescending(close, PP_trendSetup);
#
def delta = close - open;
#
def bodyMax = Max(open, close);
def bodyMin = Min(open, close);
#
def isEngulfing = bodyMax > bodyMax[1] && bodyMin < bodyMin[1];
#
def isDoji = IsDoji(PP_lenght, 0.05);
#
def bullishcandle = IsDescending[1] && ((delta[1] < 0) or isDoji[1]) && delta > 0 && IsEngulfing;
def bearishcandle = IsAscending[1] && ((delta[1] > 0) or isDoji[1]) && delta < 0 && IsEngulfing;

I’ll take a look when I get home to my computer
 
Well, if you really want to be strict about it, check out this concept I worked on a while back. This is how I’ve seen engulfing logic play out, but you can adapt it to your own style.

Python:
input PP_trendSetup = 3;
input PP_lenght = 20;
#
def isAscending = IsAscending(close, PP_trendSetup);
def isDescending = IsDescending(close, PP_trendSetup);
#
def delta = close - open;
#
def bodyMax = Max(open, close);
def bodyMin = Min(open, close);
#
def isEngulfing = bodyMax > bodyMax[1] && bodyMin < bodyMin[1];
#
def isDoji = IsDoji(PP_lenght, 0.05);
#
def bullishcandle = IsDescending[1] && ((delta[1] < 0) or isDoji[1]) && delta > 0 && IsEngulfing;
def bearishcandle = IsAscending[1] && ((delta[1] > 0) or isDoji[1]) && delta < 0 && IsEngulfing;
Thoughts?
Code:
# ------------------------------------------------------
# RSI Engulfing With TDI Filter[WizardofLittleElm]
# Converted by @TraderSam
# Converted on 11/09/25
# 11/10/25 - Added a little bit more dynamic engulfing logic
# ------------------------------------------------------

# === General Inputs ===
declare upper;
input LabelSize = fontsize.medium;
input LabelLocation = location.top_left;
input EngulfingType = {default SuperStrict, Strict, Loose};

# === RMA Helper ===
script RMA {
    input price = close;
    input length = 14;

    def rma = if BarNumber() == 1
        then price
        else (rma[1] * (length - 1) + price) / length;

    plot out = rma;
}

# === Engulfing Candle Logic ===
def delta = close - open;
def bodyMax = Max(open, close);
def bodyMin = Min(open, close);
def isEngulfing = bodyMax > bodyMax[1] && bodyMin < bodyMin[1];
def bullishcandle = delta > 0 && IsEngulfing;
def bearishcandle = delta < 0 && IsEngulfing;

def bullishcandleLoose = close >= open[1] and close[1] < open[1];
def bearishcandleLoose = close <= open[1] and close[1] > open[1];

input PP_trendSetup = 3;
input PP_lenght = 20;
def isAscending = IsAscending(close, PP_trendSetup);
def isDescending = IsDescending(close, PP_trendSetup);
def isDoji = IsDoji(PP_lenght, 0.05);
def bullishcandleSuperStrict = IsDescending[1] && ((delta[1] < 0) or isDoji[1]) && delta > 0 && IsEngulfing;
def bearishcandleSuperStrict = IsAscending[1] && ((delta[1] > 0) or isDoji[1]) && delta < 0 && IsEngulfing;

# === RSI With Smoothing ===
input rsiSource = close;
input rsiLength = 14;
input rsiOverBought = 70;
input rsiOverSold = 30;

# RSI Smoothing
input usesmoothing = yes;
input smoothType = {default SMA, EMA, WMA, RMA};
input smoothLength = 3;

# Calculate RSI
def rsiraw = RSI(rsiLength, rsiSource);

# Apply Smoothing
def rsivalue = if usesmoothing then (if smoothType == smoothType.SMA then SimpleMovingAvg(rsiraw, smoothLength) else if smoothType == smoothType.EMA then ExpAverage(rsiraw, smoothLength) else if smoothType == smoothType.WMA then WMA(rsiraw, smoothlength) else if smoothType == smoothType.RMA then rma(rsiraw, smoothLength) else rsiraw) else Double.NaN;

# RSI Conditions
def isrsiob = rsivalue >= rsiOverBought;
def isrsios = rsivalue <= rsiOverSold;

# Base Entry Signals
def baseshortsignal = (isrsiob or isrsiob[1] or isrsiob[2]) and if engulfingtype == engulfingtype.strict then bearishcandle else if engulfingtype == engulfingtype.SuperStrict then bearishcandlesuperstrict else bearishcandleloose;
def baselongsignal = (isrsios or isrsios[1] or isrsios[2]) and if engulfingtype == engulfingtype.strict then bullishcandle else if engulfingtype == engulfingtype.SuperStrict then bullishcandlesuperstrict else bullishcandleloose;

# === TDI Filter ===
def tdirsi = RSI(8, close);
def tdima = SimpleMovingAvg(tdirsi, 34);
def tdioff = 1.6185 * StDev(tdirsi, 34);
def tdiup = tdima + tdioff;
def tdidn = tdima - tdioff;
def tdimid = (tdiup + tdidn) / 2;
def tdifastma = SimpleMovingAvg(tdirsi, 2);

# Filter Mode Selection
input use_TDI_filter = yes;
input FilterMode = {default Option1, Option2, Option3};

# Threshold Settings
input LongThreshold = 30;
input ShortThreshold = 70;

# Lookback Period
input tdiLookback = 3;

# Option1
def option1_validshort = tdifastma > ShortThreshold;
def option1_validlong = tdifastma < LongThreshold;

# Option 2
def option2_validshort = tdifastma > tdiup;
def option2_validlong = tdifastma < tdidn;

# Option 3
def option3_validshort = tdifastma > tdiup and tdifastma > ShortThreshold;
def option3_validlong = tdifastma < tdidn and tdifastma < LongThreshold;

# Apply Filter
def tdivalidshortnow = if FilterMode == FilterMode.Option1 then option1_validshort else if FilterMode == FilterMode.Option2 then option2_validshort else option3_validshort;
def tdivalidlongnow = if FilterMode == FilterMode.Option1 then option1_validlong else if FilterMode == FilterMode.Option2 then option2_validlong else option3_validlong;

# Apply LookBack
def tdivalidshort = tdivalidshortnow[0] or (tdiLookback >= 2 and tdivalidshortnow[1]) or (tdiLookback >= 3 and tdivalidshortnow[2]) or (tdiLookback >= 4 and tdivalidshortnow[3]) or (tdiLookback >= 5 and tdivalidshortnow[4]) or (tdiLookback >= 6 and tdivalidshortnow[5]) or (tdiLookback >= 7 and tdivalidshortnow[6]) or (tdiLookback >= 8 and tdivalidshortnow[7]) or (tdiLookback >= 9 and tdivalidshortnow[8]) or (tdiLookback >= 10 and tdivalidshortnow[9]);

def tdiValidLong = tdivalidlongnow[0] or (tdiLookback >= 2 and tdivalidlongnow[1]) or (tdiLookback >= 3 and tdivalidlongnow[2]) or (tdiLookback >= 4 and tdivalidlongnow[3]) or (tdiLookback >= 5 and tdivalidlongnow[4]) or (tdiLookback >= 6 and tdivalidlongnow[5]) or (tdiLookback >= 7 and tdivalidlongnow[6]) or (tdiLookback >= 8 and tdivalidlongnow[7]) or (tdiLookback >= 9 and tdivalidlongnow[8]) or (tdiLookback >= 10 and tdivalidlongnow[9]);

# === EMA Trend Filter ===
input useEMAFilter = yes;
input emaLength = 200;
input reverseEMA = no;

# Calculate EMA
def trendEMA = expaverage(close, emalength);

# Determine Trend Direction
def priceaboveema = close > trendema;
def pricebelowema = close < trendema;

# Apply normal or reversed logic
def emavalidlong = if reverseema then pricebelowema else priceaboveema;
def emavalidshort = if reverseema then priceaboveema else pricebelowema;

# Plot EMA;
plot EMAplot = if useEMAFilter then trendEMA else double.nan;
emaplot.setdefaultcolor(color.blue);
emaplot.setlineweight(2);

# === Final Entry Signals ===
def finalshortsignal = baseshortsignal and (if use_tdi_filter then tdivalidshort else yes) and (if useEMAFilter then emaValidShort else yes);
def finalLongSignal = baseLongSignal and (if use_TDI_Filter then tdiValidLong else yes) and (if useEMAFilter then emaValidLong else yes);

# === Plots ===
plot long = if finallongsignal then low else double.nan;
long.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
long.setdefaultcolor(color.green);
long.setlineweight(2);
plot short = if finalshortsignal then high else double.nan;
short.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_Down);
short.setdefaultcolor(color.red);
short.setlineweight(2);

AddLabel(use_tdi_filter, "SHORT" + " ", if tdivalidshort then Color.RED else Color.Dark_gray, labellocation, labelsize);
AddLabel(use_tdi_filter, "LONG" + " ", if tdivalidlong then Color.green else Color.Dark_GRAY, labellocation, labelsize);

# === Alerts ===
Alert(Finallongsignal, "Long Entry Signal", Alert.BAR, Sound.Ding);
Alert(finalshortsignal, "Short Entry Signal", Alert.BAR, Sound.Ding);
 
Good, what about using all of them? Let’s use color-coded levels to show which signal fired.

here you go: 🍻

Python:
# === Engulfing Candle Logic ===
def delta = close - open;
def bodyMax = Max(open, close);
def bodyMin = Min(open, close);
def IsEngulfing = bodyMax > bodyMax[1] && bodyMin < bodyMin[1];

def isAsc = IsAscending(close, PP_trendSetup);
def isDesc = IsDescending(close, PP_trendSetup);
def isDoji  = IsDoji(PP_length, 0.05);

def priorDownOrDoji = (delta[1] < 0) or isDoji[1];
def priorUpOrDoji   = (delta[1] > 0) or isDoji[1];

# Classic engulfing
def bullishCandle = (delta > 0) && IsEngulfing;
def bearishCandle = (delta < 0) && IsEngulfing;

# "Loose" variant (compares to prior open)
def bullishCandleLoose = (close >= open[1]) && (close[1] < open[1]);
def bearishCandleLoose = (close <= open[1]) && (close[1] > open[1]);

# Super-strict: require trend context on the *prior* bar
def bullishCandleSuperStrict = isDesc[1] && priorDownOrDoji && (delta > 0) && IsEngulfing;
def bearishCandleSuperStrict = isAsc[1] && priorUpOrDoji && (delta < 0) && IsEngulfing;

# (2 = bull super, 1 = bull, 0 = none, -1 = bear, -2 = bear super)
def engulfingSignal =
    if bullishCandleSuperStrict then  2
    else if bearishCandleSuperStrict then -2
    else if bullishCandle then  1
    else if bearishCandle then -1
    else 0;
 
Good, what about using all of them? Let’s use color-coded levels to show which signal fired.

here you go: 🍻

Python:
# === Engulfing Candle Logic ===
def delta = close - open;
def bodyMax = Max(open, close);
def bodyMin = Min(open, close);
def IsEngulfing = bodyMax > bodyMax[1] && bodyMin < bodyMin[1];

def isAsc = IsAscending(close, PP_trendSetup);
def isDesc = IsDescending(close, PP_trendSetup);
def isDoji  = IsDoji(PP_length, 0.05);

def priorDownOrDoji = (delta[1] < 0) or isDoji[1];
def priorUpOrDoji   = (delta[1] > 0) or isDoji[1];

# Classic engulfing
def bullishCandle = (delta > 0) && IsEngulfing;
def bearishCandle = (delta < 0) && IsEngulfing;

# "Loose" variant (compares to prior open)
def bullishCandleLoose = (close >= open[1]) && (close[1] < open[1]);
def bearishCandleLoose = (close <= open[1]) && (close[1] > open[1]);

# Super-strict: require trend context on the *prior* bar
def bullishCandleSuperStrict = isDesc[1] && priorDownOrDoji && (delta > 0) && IsEngulfing;
def bearishCandleSuperStrict = isAsc[1] && priorUpOrDoji && (delta < 0) && IsEngulfing;

# (2 = bull super, 1 = bull, 0 = none, -1 = bear, -2 = bear super)
def engulfingSignal =
    if bullishCandleSuperStrict then  2
    else if bearishCandleSuperStrict then -2
    else if bullishCandle then  1
    else if bearishCandle then -1
    else 0;
Engulfing Candle Scanner https://tos.mx/!ve6XeJve Engulfing Candle Indicator https://tos.mx/!2C3G7lHl
1762837562827.png
 
Hi,
I got the scanner setup but I do have a question about the results with the columns color coded. Is this a saved setup? Just confused on how to get those columns....
Thank you for your help in advance.
Tom
Yes they are custom for the most part and are posted in the VIP forums
 
Good, what about using all of them? Let’s use color-coded levels to show which signal fired.

here you go: 🍻

Python:
# === Engulfing Candle Logic ===
def delta = close - open;
def bodyMax = Max(open, close);
def bodyMin = Min(open, close);
def IsEngulfing = bodyMax > bodyMax[1] && bodyMin < bodyMin[1];

def isAsc = IsAscending(close, PP_trendSetup);
def isDesc = IsDescending(close, PP_trendSetup);
def isDoji  = IsDoji(PP_length, 0.05);

def priorDownOrDoji = (delta[1] < 0) or isDoji[1];
def priorUpOrDoji   = (delta[1] > 0) or isDoji[1];

# Classic engulfing
def bullishCandle = (delta > 0) && IsEngulfing;
def bearishCandle = (delta < 0) && IsEngulfing;

# "Loose" variant (compares to prior open)
def bullishCandleLoose = (close >= open[1]) && (close[1] < open[1]);
def bearishCandleLoose = (close <= open[1]) && (close[1] > open[1]);

# Super-strict: require trend context on the *prior* bar
def bullishCandleSuperStrict = isDesc[1] && priorDownOrDoji && (delta > 0) && IsEngulfing;
def bearishCandleSuperStrict = isAsc[1] && priorUpOrDoji && (delta < 0) && IsEngulfing;

# (2 = bull super, 1 = bull, 0 = none, -1 = bear, -2 = bear super)
def engulfingSignal =
    if bullishCandleSuperStrict then  2
    else if bearishCandleSuperStrict then -2
    else if bullishCandle then  1
    else if bearishCandle then -1
    else 0;
That part is showing error .......
def isAsc = IsAscending(close, PP_trendSetup);
def isDesc = IsDescending(close, PP_trendSetup);
def isDoji = IsDoji(PP_length, 0.05);
 
Hi,

You need to declare the PP_trendSetup and PP_length first. Check the original script.
Yes that is yours.

Code:
# === Engulfing Candle Logic ===
def delta = close - open;
def bodyMax = Max(open, close);
def bodyMin = Min(open, close);
def IsEngulfing = bodyMax > bodyMax[1] && bodyMin < bodyMin[1];

def isAsc = IsAscending(close, PP_trendSetup);
def isDesc = IsDescending(close, PP_trendSetup);
def isDoji = IsDoji(PP_length, 0.05);

def priorDownOrDoji = (delta[1] < 0) or isDoji[1];
def priorUpOrDoji = (delta[1] > 0) or isDoji[1];

# Classic engulfing
def bullishCandle = (delta > 0) && IsEngulfing;
def bearishCandle = (delta < 0) && IsEngulfing;

# "Loose" variant (compares to prior open)
def bullishCandleLoose = (close >= open[1]) && (close[1] < open[1]);
def bearishCandleLoose = (close <= open[1]) && (close[1] > open[1]);

# Super-strict: require trend context on the prior bar
def bullishCandleSuperStrict = isDesc[1] && priorDownOrDoji && (delta > 0) && IsEngulfing;
def bearishCandleSuperStrict = isAsc[1] && priorUpOrDoji && (delta < 0) && IsEngulfing;

# (2 = bull super, 1 = bull, 0 = none, -1 = bear, -2 = bear super)
def engulfingSignal =
if bullishCandleSuperStrict then 2
else if bearishCandleSuperStrict then -2
else if bullishCandle then 1
else if bearishCandle then -1
else 0;
 
Last edited by a moderator:
Yes that is yours.

Code:
# === Engulfing Candle Logic ===
def delta = close - open;
def bodyMax = Max(open, close);
def bodyMin = Min(open, close);
def IsEngulfing = bodyMax > bodyMax[1] && bodyMin < bodyMin[1];

def isAsc = IsAscending(close, PP_trendSetup);
def isDesc = IsDescending(close, PP_trendSetup);
def isDoji = IsDoji(PP_length, 0.05);

def priorDownOrDoji = (delta[1] < 0) or isDoji[1];
def priorUpOrDoji = (delta[1] > 0) or isDoji[1];

# Classic engulfing
def bullishCandle = (delta > 0) && IsEngulfing;
def bearishCandle = (delta < 0) && IsEngulfing;

# "Loose" variant (compares to prior open)
def bullishCandleLoose = (close >= open[1]) && (close[1] < open[1]);
def bearishCandleLoose = (close <= open[1]) && (close[1] > open[1]);

# Super-strict: require trend context on the prior bar
def bullishCandleSuperStrict = isDesc[1] && priorDownOrDoji && (delta > 0) && IsEngulfing;
def bearishCandleSuperStrict = isAsc[1] && priorUpOrDoji && (delta < 0) && IsEngulfing;

# (2 = bull super, 1 = bull, 0 = none, -1 = bear, -2 = bear super)
def engulfingSignal =
if bullishCandleSuperStrict then 2
else if bearishCandleSuperStrict then -2
else if bullishCandle then 1
else if bearishCandle then -1
else 0;
@rewardiaz is saying that you must add this script:
Ruby:
# === Engulfing Candle Logic ===
def delta = close - open;
def bodyMax = Max(open, close);
def bodyMin = Min(open, close);
def IsEngulfing = bodyMax > bodyMax[1] && bodyMin < bodyMin[1];

def isAsc = IsAscending(close, PP_trendSetup);
def isDesc = IsDescending(close, PP_trendSetup);
def isDoji = IsDoji(PP_length, 0.05);

def priorDownOrDoji = (delta[1] < 0) or isDoji[1];
def priorUpOrDoji = (delta[1] > 0) or isDoji[1];

# Classic engulfing
def bullishCandle = (delta > 0) && IsEngulfing;
def bearishCandle = (delta < 0) && IsEngulfing;

# "Loose" variant (compares to prior open)
def bullishCandleLoose = (close >= open[1]) && (close[1] < open[1]);
def bearishCandleLoose = (close <= open[1]) && (close[1] > open[1]);

# Super-strict: require trend context on the prior bar
def bullishCandleSuperStrict = isDesc[1] && priorDownOrDoji && (delta > 0) && IsEngulfing;
def bearishCandleSuperStrict = isAsc[1] && priorUpOrDoji && (delta < 0) && IsEngulfing;

# (2 = bull super, 1 = bull, 0 = none, -1 = bear, -2 = bear super)
def engulfingSignal =
if bullishCandleSuperStrict then 2
else if bearishCandleSuperStrict then -2
else if bullishCandle then 1
else if bearishCandle then -1
else 0;

to the original code to make it work. Like this:
Ruby:
# ------------------------------------------------------
# RSI Engulfing With TDI Filter[WizardofLittleElm]
# Converted by @TraderSam
# Converted on 11/09/25
# 11/10/25 - Added a little bit more dynamic engulfing logic
# ------------------------------------------------------

# === General Inputs ===
declare upper;
input LabelSize = fontsize.medium;
input LabelLocation = location.top_left;
input EngulfingType = {default SuperStrict, Strict, Loose};

# === RMA Helper ===
script RMA {
    input price = close;
    input length = 14;

    def rma = if BarNumber() == 1
        then price
        else (rma[1] * (length - 1) + price) / length;

    plot out = rma;
}

# === Engulfing Candle Logic ===
def delta = close - open;
def bodyMax = Max(open, close);
def bodyMin = Min(open, close);
def isEngulfing = bodyMax > bodyMax[1] && bodyMin < bodyMin[1];
def bullishcandle = delta > 0 && IsEngulfing;
def bearishcandle = delta < 0 && IsEngulfing;

def bullishcandleLoose = close >= open[1] and close[1] < open[1];
def bearishcandleLoose = close <= open[1] and close[1] > open[1];

input PP_trendSetup = 3;
input PP_lenght = 20;

def delta = close - open;
def bodyMax = Max(open, close);
def bodyMin = Min(open, close);
def IsEngulfing = bodyMax > bodyMax[1] && bodyMin < bodyMin[1];

def isAsc = IsAscending(close, PP_trendSetup);
def isDesc = IsDescending(close, PP_trendSetup);
def isDoji = IsDoji(PP_length, 0.05);

def priorDownOrDoji = (delta[1] < 0) or isDoji[1];
def priorUpOrDoji = (delta[1] > 0) or isDoji[1];

# Classic engulfing
def bullishCandle = (delta > 0) && IsEngulfing;
def bearishCandle = (delta < 0) && IsEngulfing;

# "Loose" variant (compares to prior open)
def bullishCandleLoose = (close >= open[1]) && (close[1] < open[1]);
def bearishCandleLoose = (close <= open[1]) && (close[1] > open[1]);

# Super-strict: require trend context on the prior bar
def bullishCandleSuperStrict = isDesc[1] && priorDownOrDoji && (delta > 0) && IsEngulfing;
def bearishCandleSuperStrict = isAsc[1] && priorUpOrDoji && (delta < 0) && IsEngulfing;

# (2 = bull super, 1 = bull, 0 = none, -1 = bear, -2 = bear super)
def engulfingSignal =
if bullishCandleSuperStrict then 2
else if bearishCandleSuperStrict then -2
else if bullishCandle then 1
else if bearishCandle then -1
else 0;

# === RSI With Smoothing ===
input rsiSource = close;
input rsiLength = 14;
input rsiOverBought = 70;
input rsiOverSold = 30;

# RSI Smoothing
input usesmoothing = yes;
input smoothType = {default SMA, EMA, WMA, RMA};
input smoothLength = 3;

# Calculate RSI
def rsiraw = RSI(rsiLength, rsiSource);

# Apply Smoothing
def rsivalue = if usesmoothing then (if smoothType == smoothType.SMA then SimpleMovingAvg(rsiraw, smoothLength) else if smoothType == smoothType.EMA then ExpAverage(rsiraw, smoothLength) else if smoothType == smoothType.WMA then WMA(rsiraw, smoothlength) else if smoothType == smoothType.RMA then rma(rsiraw, smoothLength) else rsiraw) else Double.NaN;

# RSI Conditions
def isrsiob = rsivalue >= rsiOverBought;
def isrsios = rsivalue <= rsiOverSold;

# Base Entry Signals
def baseshortsignal = (isrsiob or isrsiob[1] or isrsiob[2]) and if engulfingtype == engulfingtype.strict then bearishcandle else if engulfingtype == engulfingtype.SuperStrict then bearishcandlesuperstrict else bearishcandleloose;
def baselongsignal = (isrsios or isrsios[1] or isrsios[2]) and if engulfingtype == engulfingtype.strict then bullishcandle else if engulfingtype == engulfingtype.SuperStrict then bullishcandlesuperstrict else bullishcandleloose;

# === TDI Filter ===
def tdirsi = RSI(8, close);
def tdima = SimpleMovingAvg(tdirsi, 34);
def tdioff = 1.6185 * StDev(tdirsi, 34);
def tdiup = tdima + tdioff;
def tdidn = tdima - tdioff;
def tdimid = (tdiup + tdidn) / 2;
def tdifastma = SimpleMovingAvg(tdirsi, 2);

# Filter Mode Selection
input use_TDI_filter = yes;
input FilterMode = {default Option1, Option2, Option3};

# Threshold Settings
input LongThreshold = 30;
input ShortThreshold = 70;

# Lookback Period
input tdiLookback = 3;

# Option1
def option1_validshort = tdifastma > ShortThreshold;
def option1_validlong = tdifastma < LongThreshold;

# Option 2
def option2_validshort = tdifastma > tdiup;
def option2_validlong = tdifastma < tdidn;

# Option 3
def option3_validshort = tdifastma > tdiup and tdifastma > ShortThreshold;
def option3_validlong = tdifastma < tdidn and tdifastma < LongThreshold;

# Apply Filter
def tdivalidshortnow = if FilterMode == FilterMode.Option1 then option1_validshort else if FilterMode == FilterMode.Option2 then option2_validshort else option3_validshort;
def tdivalidlongnow = if FilterMode == FilterMode.Option1 then option1_validlong else if FilterMode == FilterMode.Option2 then option2_validlong else option3_validlong;

# Apply LookBack
def tdivalidshort = tdivalidshortnow[0] or (tdiLookback >= 2 and tdivalidshortnow[1]) or (tdiLookback >= 3 and tdivalidshortnow[2]) or (tdiLookback >= 4 and tdivalidshortnow[3]) or (tdiLookback >= 5 and tdivalidshortnow[4]) or (tdiLookback >= 6 and tdivalidshortnow[5]) or (tdiLookback >= 7 and tdivalidshortnow[6]) or (tdiLookback >= 8 and tdivalidshortnow[7]) or (tdiLookback >= 9 and tdivalidshortnow[8]) or (tdiLookback >= 10 and tdivalidshortnow[9]);

def tdiValidLong = tdivalidlongnow[0] or (tdiLookback >= 2 and tdivalidlongnow[1]) or (tdiLookback >= 3 and tdivalidlongnow[2]) or (tdiLookback >= 4 and tdivalidlongnow[3]) or (tdiLookback >= 5 and tdivalidlongnow[4]) or (tdiLookback >= 6 and tdivalidlongnow[5]) or (tdiLookback >= 7 and tdivalidlongnow[6]) or (tdiLookback >= 8 and tdivalidlongnow[7]) or (tdiLookback >= 9 and tdivalidlongnow[8]) or (tdiLookback >= 10 and tdivalidlongnow[9]);

# === EMA Trend Filter ===
input useEMAFilter = yes;
input emaLength = 200;
input reverseEMA = no;

# Calculate EMA
def trendEMA = expaverage(close, emalength);

# Determine Trend Direction
def priceaboveema = close > trendema;
def pricebelowema = close < trendema;

# Apply normal or reversed logic
def emavalidlong = if reverseema then pricebelowema else priceaboveema;
def emavalidshort = if reverseema then priceaboveema else pricebelowema;

# Plot EMA;
plot EMAplot = if useEMAFilter then trendEMA else double.nan;
emaplot.setdefaultcolor(color.blue);
emaplot.setlineweight(2);

# === Final Entry Signals ===
def finalshortsignal = baseshortsignal and (if use_tdi_filter then tdivalidshort else yes) and (if useEMAFilter then emaValidShort else yes);
def finalLongSignal = baseLongSignal and (if use_TDI_Filter then tdiValidLong else yes) and (if useEMAFilter then emaValidLong else yes);

# === Plots ===
plot long = if finallongsignal then low else double.nan;
long.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
long.setdefaultcolor(color.green);
long.setlineweight(2);
plot short = if finalshortsignal then high else double.nan;
short.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_Down);
short.setdefaultcolor(color.red);
short.setlineweight(2);

AddLabel(use_tdi_filter, "SHORT" + " ", if tdivalidshort then Color.RED else Color.Dark_gray, labellocation, labelsize);
AddLabel(use_tdi_filter, "LONG" + " ", if tdivalidlong then Color.green else Color.Dark_GRAY, labellocation, labelsize);

# === Alerts ===
Alert(Finallongsignal, "Long Entry Signal", Alert.BAR, Sound.Ding);
Alert(finalshortsignal, "Short Entry Signal", Alert.BAR, Sound.Ding);
 

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
1568 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