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