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:

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