OPEN-SOURCE SCRIPT

SMT Divergence + IFVG Strategy with Buy/Sell Signals

//version=6
indicator("SMT Divergence + IFVG Strategy with Buy/Sell Signals", overlay=true)

// Input parameters
higherTimeframe = input.timeframe("60", "Higher Timeframe") // Corrected timeframe to "60" for hourly
defaultStopLossPct = input.float(15.0, "Stop Loss %", step=0.1) // Updated to 15% stop loss
defaultTakeProfitPct = input.float(30.0, "Take Profit %", step=0.1) // Added 30% take profit percentage

// Retrieve higher timeframe trend
htfClose = request.security(syminfo.tickerid, higherTimeframe, close)
higherTrend = ta.ema(htfClose, 50) > ta.ema(htfClose, 200) // Higher timeframe bias: bullish if EMA50 > EMA200

// Custom Divergence Detection
rsiPeriod = input.int(14, "RSI Period")
rsi = ta.rsi(close, rsiPeriod)
lookback = input.int(5, "Divergence Lookback")

// Detect bullish divergence: Price makes a lower low, RSI makes a higher low
priceLow = ta.lowest(close, lookback)
pricePrevLow = ta.lowest(close[lookback], lookback)
rsiLow = ta.lowest(rsi, lookback)
rsiPrevLow = ta.lowest(rsi[lookback], lookback)
bullishDivergence = (priceLow < pricePrevLow) and (rsiLow > rsiPrevLow)

// Detect bearish divergence: Price makes a higher high, RSI makes a lower high
priceHigh = ta.highest(close, lookback)
pricePrevHigh = ta.highest(close[lookback], lookback)
rsiHigh = ta.highest(rsi, lookback)
rsiPrevHigh = ta.highest(rsi[lookback], lookback)
bearishDivergence = (priceHigh > pricePrevHigh) and (rsiHigh < rsiPrevHigh)

smtDiv = bullishDivergence or bearishDivergence

// Inverse Fair Value Gap (IFVG)
gapThreshold = input.float(0.5, "IFVG Threshold (%)", step=0.1)
priceChange = math.abs(close - close[1])
previousHigh = ta.valuewhen(priceChange > gapThreshold * close[1], high, 1)
previousLow = ta.valuewhen(priceChange > gapThreshold * close[1], low, 1)
ifvgZone = not na(previousHigh) and not na(previousLow) and (close > previousHigh or close < previousLow)

// Entry Signal
entryConditionBuy = higherTrend and bullishDivergence and ifvgZone
entryConditionSell = not higherTrend and bearishDivergence and ifvgZone

// Plotting Buy and Sell Signals
if entryConditionBuy
label.new(bar_index, high, "BUY", style=label.style_label_up, color=color.green, textcolor=color.white, size=size.small)
stopLossLevel = close * (1 - defaultStopLossPct / 100)
takeProfitLevel = close * (1 + defaultTakeProfitPct / 100)
line.new(bar_index, stopLossLevel, bar_index + 1, stopLossLevel, color=color.red, width=1, extend=extend.right)
line.new(bar_index, takeProfitLevel, bar_index + 1, takeProfitLevel, color=color.green, width=1, extend=extend.right)

if entryConditionSell
label.new(bar_index, low, "SELL", style=label.style_label_down, color=color.red, textcolor=color.white, size=size.small)
stopLossLevel = close * (1 + defaultStopLossPct / 100)
takeProfitLevel = close * (1 - defaultTakeProfitPct / 100)
line.new(bar_index, stopLossLevel, bar_index + 1, stopLossLevel, color=color.red, width=1, extend=extend.right)
line.new(bar_index, takeProfitLevel, bar_index + 1, takeProfitLevel, color=color.green, width=1, extend=extend.right)

// Alerts
alertcondition(entryConditionBuy, title="Buy Signal", message="Buy signal detected. Target 30%, Stop Loss 15%.")
alertcondition(entryConditionSell, title="Sell Signal", message="Sell signal detected. Target 30%, Stop Loss 15%.")
Bands and ChannelsBill Williams IndicatorsCandlestick analysis

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Disclaimer