declare lower; # Triple RSI Momentum Enhanced - Stay in Strong Trends # Inputs input fastRSILength = 5; input mediumRSILength = 11; input slowRSILength = 24; input overboughtLevel = 70; input oversoldLevel = 30; input extremeOverbought = 80; input extremeOversold = 20; input momentumHoldLevel = 65; # Stay in trade above/below these levels input momentumExitLevel = 35; input showMomentumQuality = yes; # Calculate RSIs def fastRSI = RSI(length = fastRSILength); def medRSI = RSI(length = mediumRSILength); def slowRSI = RSI(length = slowRSILength); # Average RSI for composite view def avgRSI = (fastRSI + medRSI + slowRSI) / 3; # Count how many RSIs are overbought/oversold def overboughtCount = (if fastRSI > overboughtLevel then 1 else 0) + (if medRSI > overboughtLevel then 1 else 0) + (if slowRSI > overboughtLevel then 1 else 0); def oversoldCount = (if fastRSI < oversoldLevel then 1 else 0) + (if medRSI < oversoldLevel then 1 else 0) + (if slowRSI < oversoldLevel then 1 else 0); def extremeOBCount = (if fastRSI > extremeOverbought then 1 else 0) + (if medRSI > extremeOverbought then 1 else 0) + (if slowRSI > extremeOverbought then 1 else 0); def extremeOSCount = (if fastRSI < extremeOversold then 1 else 0) + (if medRSI < extremeOversold then 1 else 0) + (if slowRSI < extremeOversold then 1 else 0); # Momentum persistence - tracks how long momentum has been strong def bullishPersistence = if avgRSI > momentumHoldLevel then bullishPersistence[1] + 1 else 0; def bearishPersistence = if avgRSI < momentumExitLevel then bearishPersistence[1] + 1 else 0; # Strong momentum hold conditions def holdLongCondition = avgRSI > momentumHoldLevel or (avgRSI > 50 and overboughtCount >= 2); def holdShortCondition = avgRSI < momentumExitLevel or (avgRSI < 50 and oversoldCount >= 2); # Exit warnings - only when momentum truly breaks def bullExitWarning = bullishPersistence[1] >= 3 and avgRSI < 50 and fastRSI < medRSI; def bearExitWarning = bearishPersistence[1] >= 3 and avgRSI > 50 and fastRSI > medRSI; # Momentum alignment def perfectBullishMomentum = overboughtCount == 3; def strongBullishMomentum = overboughtCount >= 2 or (avgRSI > 65 and fastRSI > medRSI and medRSI > slowRSI); def perfectBearishMomentum = oversoldCount == 3; def strongBearishMomentum = oversoldCount >= 2 or (avgRSI < 35 and fastRSI < medRSI and medRSI < slowRSI); # RSI momentum (rate of change) def fastRSIMom = fastRSI - fastRSI[3]; def medRSIMom = medRSI - medRSI[3]; def slowRSIMom = slowRSI - slowRSI[3]; def momentumAccelerating = fastRSIMom > 5 and medRSIMom > 3; def momentumDecelerating = fastRSIMom < -5 and medRSIMom < -3; # Divergence detection def priceHigher = high > Highest(high[1], 5); def priceLower = low < Lowest(low[1], 5); # Bearish divergence: Price makes higher high, RSI makes lower high def bearishDivergence = priceHigher and fastRSI < Highest(fastRSI[1], 5) and fastRSI > 60; def strongBearishDiv = bearishDivergence and medRSI < Highest(medRSI[1], 5) and slowRSI < Highest(slowRSI[1], 5); # Bullish divergence: Price makes lower low, RSI makes higher low def bullishDivergence = priceLower and fastRSI > Lowest(fastRSI[1], 5) and fastRSI < 40; def strongBullishDiv = bullishDivergence and medRSI > Lowest(medRSI[1], 5) and slowRSI > Lowest(slowRSI[1], 5); # Hidden divergences (trend continuation) def hiddenBullish = !priceLower and high > high[5] and fastRSI < fastRSI[5] and fastRSI > 50; def hiddenBearish = !priceHigher and low < low[5] and fastRSI > fastRSI[5] and fastRSI < 50; # Enhanced momentum score with persistence factor def persistenceFactor = if bullishPersistence > 0 then Min(20, bullishPersistence * 2) else if bearishPersistence > 0 then -Min(20, bearishPersistence * 2) else 0; def momentumScore = if perfectBullishMomentum and extremeOBCount >= 2 then 100 else if perfectBullishMomentum then 80 + persistenceFactor else if strongBullishMomentum and holdLongCondition then 60 + persistenceFactor else if holdLongCondition then 40 + persistenceFactor else if perfectBearishMomentum and extremeOSCount >= 2 then -100 else if perfectBearishMomentum then -80 + persistenceFactor else if strongBearishMomentum and holdShortCondition then -60 + persistenceFactor else if holdShortCondition then -40 + persistenceFactor else if bullExitWarning then 20 else if bearExitWarning then -20 else (avgRSI - 50) * 2; # Plot momentum strength histogram plot MomentumHist = momentumScore; MomentumHist.SetPaintingStrategy(PaintingStrategy.HISTOGRAM); MomentumHist.SetLineWeight(3); MomentumHist.AssignValueColor( if momentumScore >= 80 then Color.GREEN else if momentumScore >= 60 then Color.DARK_GREEN else if momentumScore >= 40 and holdLongCondition then Color.LIGHT_GREEN else if momentumScore <= -80 then Color.RED else if momentumScore <= -60 then Color.DARK_RED else if momentumScore <= -40 and holdShortCondition then Color.LIGHT_RED else if bullExitWarning or bearExitWarning then Color.YELLOW else Color.GRAY ); # Reference lines plot OverboughtLine = overboughtLevel; OverboughtLine.SetDefaultColor(Color.DARK_GREEN); OverboughtLine.SetStyle(Curve.SHORT_DASH); plot MomentumHoldLine = momentumHoldLevel; MomentumHoldLine.SetDefaultColor(Color.GREEN); MomentumHoldLine.SetStyle(Curve.SHORT_DASH); plot MidLine = 50; MidLine.SetDefaultColor(Color.WHITE); MidLine.SetStyle(Curve.FIRM); plot MomentumExitLine = momentumExitLevel; MomentumExitLine.SetDefaultColor(Color.RED); MomentumExitLine.SetStyle(Curve.SHORT_DASH); plot OversoldLine = oversoldLevel; OversoldLine.SetDefaultColor(Color.DARK_RED); OversoldLine.SetStyle(Curve.SHORT_DASH); # Momentum quality dots plot QualityDots = if showMomentumQuality then if holdLongCondition and bullishPersistence > 5 then 90 else if holdShortCondition and bearishPersistence > 5 then 10 else Double.NaN else Double.NaN; QualityDots.SetPaintingStrategy(PaintingStrategy.POINTS); QualityDots.SetDefaultColor(Color.CYAN); QualityDots.SetLineWeight(3); # Main action label AddLabel(yes, if perfectBullishMomentum and extremeOBCount >= 2 and !bullExitWarning then "EXTREME BULL MOMENTUM (" + bullishPersistence + " bars) - Let It Run! 🚀" else if holdLongCondition and bullishPersistence >= 5 then "STRONG TREND HOLDING (" + bullishPersistence + " bars) - Stay Long!" else if strongBullishMomentum and !bullExitWarning then "Strong Bull Momentum (" + overboughtCount + "/3) - Hold Position" else if bullExitWarning then "⚠️ BULL MOMENTUM BROKEN - Exit Long!" else if perfectBearishMomentum and extremeOSCount >= 2 and !bearExitWarning then "EXTREME BEAR MOMENTUM (" + bearishPersistence + " bars) - Let It Run! 📉" else if holdShortCondition and bearishPersistence >= 5 then "STRONG TREND HOLDING (" + bearishPersistence + " bars) - Stay Short!" else if strongBearishMomentum and !bearExitWarning then "Strong Bear Momentum (" + oversoldCount + "/3) - Hold Position" else if bearExitWarning then "⚠️ BEAR MOMENTUM BROKEN - Exit Short!" else if strongBearishDiv then "STRONG BEARISH DIVERGENCE - Exit Longs!" else if strongBullishDiv then "STRONG BULLISH DIVERGENCE - Exit Shorts!" else if bearishDivergence then "Bearish Divergence Warning" else if bullishDivergence then "Bullish Divergence Warning" else if avgRSI > 60 then "Bullish Bias" else if avgRSI < 40 then "Bearish Bias" else "Neutral Momentum", if (perfectBullishMomentum or perfectBearishMomentum) and !bullExitWarning and !bearExitWarning then Color.UPTICK else if holdLongCondition and bullishPersistence >= 5 then Color.GREEN else if holdShortCondition and bearishPersistence >= 5 then Color.RED else if bullExitWarning or bearExitWarning then Color.YELLOW else if strongBearishDiv or strongBullishDiv then Color.ORANGE else if strongBullishMomentum or strongBearishMomentum then Color.LIGHT_GREEN else Color.WHITE ); # RSI values and momentum persistence AddLabel(yes, "RSI: " + Round(avgRSI, 0) + " (F:" + Round(fastRSI, 0) + " M:" + Round(medRSI, 0) + " S:" + Round(slowRSI, 0) + ") | " + (if bullishPersistence > 0 then "Bull Persist: " + bullishPersistence + " bars" else if bearishPersistence > 0 then "Bear Persist: " + bearishPersistence + " bars" else "No Persistence"), if avgRSI > momentumHoldLevel then Color.LIGHT_GREEN else if avgRSI < momentumExitLevel then Color.LIGHT_RED else Color.LIGHT_GRAY ); # Hidden divergences AddLabel(hiddenBullish, "Hidden Bullish Div - Add to Longs! ↗", Color.GREEN ); AddLabel(hiddenBearish, "Hidden Bearish Div - Add to Shorts! ↘", Color.RED ); # Momentum quality and acceleration AddLabel(yes, (if momentumAccelerating then "Momentum Accelerating ⚡ " else if momentumDecelerating then "Momentum Slowing ⚠️ " else "") + "Spread: " + Round(Max(fastRSI, Max(medRSI, slowRSI)) - Min(fastRSI, Min(medRSI, slowRSI)), 0) + (if overboughtCount == 3 and extremeOBCount >= 2 then " | EXTREME!" else if oversoldCount == 3 and extremeOSCount >= 2 then " | EXTREME!" else ""), if momentumAccelerating then Color.CYAN else if momentumDecelerating then Color.YELLOW else Color.LIGHT_GRAY );