Moody
New member
Code:
# -------------------------------------------------
# RSI Engulfing With TDI Filter [WizardofLittleElm]
# Converted by @TRADERSAM
# Modified by Copilot for selectable period input
# -------------------------------------------------
# === General Inputs ===
input LabelSize = fontsize.medium;
input LabelLocation = location.top_left;
# === Custom Timeframe Input ===
input period = AggregationPeriod.FIVE_MIN;
# Choices include:
# AggregationPeriod.MIN
# AggregationPeriod.FIVE_MIN
# AggregationPeriod.FIFTEEN_MIN
# AggregationPeriod.THIRTY_MIN
# AggregationPeriod.HOUR
# AggregationPeriod.FOUR_HOURS
# AggregationPeriod.DAY
# AggregationPeriod.WEEK
# AggregationPeriod.MONTH
# === 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(period = period) >= open(period = period)[1]
and close(period = period)[1] < open(period = period)[1];
def bearishcandle = close(period = period) <= open(period = period)[1]
and close(period = period)[1] > open(period = period)[1];
# === RSI With Smoothing ===
input rsiLength = 14;
input rsiOverBought = 70;
input rsiOverSold = 30;
input usesmoothing = yes;
input smoothType = {default SMA, EMA, WMA, RMA};
input smoothLength = 3;
# Calculate RSI with custom period
def rsiraw = RSI(rsiLength, close(period = period));
# 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 rsiraw;
# 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(period = period));
def tdima = SimpleMovingAvg(tdirsi, 34);
def tdioff = 1.6185 * StDev(tdirsi, 34);
def tdiup = tdima + tdioff;
def tdidn = tdima - tdioff;
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 with custom period
def trendEMA = ExpAverage(close(period = period), emaLength);
# Determine Trend Direction
def priceaboveema = close(period = period) > trendEMA;
def pricebelowema = close(period = period) < 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(period = period) else Double.NaN;
long.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
long.SetDefaultColor(Color.GREEN);
long.SetLineWeight(2);
plot short = if finalshortsignal then high(period = period) 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: