declare lower; # Inputs input smoothingLength = 5; input extremeThreshold = 0.5; input showDivergence = yes; # Get Nasdaq market internals def adv = close("$ADVN/Q"); # Nasdaq Advancing Issues def dec = close("$DECN/Q"); # Nasdaq Declining Issues def unch = close("$UNCN/Q"); # Nasdaq Unchanged Issues def advVol = close("$UVOL/Q"); # Nasdaq Up Volume def decVol = close("$DVOL/Q"); # Nasdaq Down Volume # Calculate A/D Ratio (raw and smoothed) def totalIssues = adv + dec + unch; def ad_ratio = if (adv + dec) > 0 then (adv - dec) / (adv + dec) else 0; def ad_ratio_smooth = Average(ad_ratio, smoothingLength); # Calculate Volume Ratio def vol_ratio = if (advVol + decVol) > 0 then (advVol - decVol) / (advVol + decVol) else 0; # Calculate Breadth Thrust (percentage of advancing issues) def breadthThrust = if totalIssues > 0 then (adv / totalIssues) * 100 else 50; # Calculate McClellan Oscillator components def ratio = if dec != 0 then adv / dec else 10; def ema19 = ExpAverage(adv - dec, 19); def ema39 = ExpAverage(adv - dec, 39); def mcclellanOsc = ema19 - ema39; # Detect momentum shifts def momentum = ad_ratio - ad_ratio[smoothingLength]; def accelerating = momentum > momentum[1] and momentum > 0; def decelerating = momentum < momentum[1] and momentum < 0; # Detect divergences with NQ price def nqUp = close > close[1]; def nqDown = close < close[1]; def bullishDivergence = nqDown and ad_ratio > ad_ratio[1] and ad_ratio < 0; def bearishDivergence = nqUp and ad_ratio < ad_ratio[1] and ad_ratio > 0; # Plot A/D Ratio histogram plot ADRatioPlot = ad_ratio; ADRatioPlot.SetPaintingStrategy(PaintingStrategy.HISTOGRAM); ADRatioPlot.AssignValueColor( if ad_ratio > extremeThreshold then Color.GREEN else if ad_ratio > 0.3 then Color.DARK_GREEN else if ad_ratio < -extremeThreshold then Color.RED else if ad_ratio < -0.3 then Color.DARK_RED else Color.GRAY ); ADRatioPlot.SetLineWeight(3); # Plot smoothed line plot SmoothedLine = ad_ratio_smooth; SmoothedLine.SetDefaultColor(Color.CYAN); SmoothedLine.SetLineWeight(2); # Plot volume ratio line plot VolRatioLine = vol_ratio * 0.5; # Scaled to fit VolRatioLine.SetDefaultColor(Color.MAGENTA); VolRatioLine.SetLineWeight(1); VolRatioLine.SetStyle(Curve.SHORT_DASH); # Reference lines plot ExtremeBullish = extremeThreshold; ExtremeBullish.SetDefaultColor(Color.DARK_GREEN); ExtremeBullish.SetStyle(Curve.SHORT_DASH); plot ModerateBullish = 0.3; ModerateBullish.SetDefaultColor(Color.GRAY); ModerateBullish.SetStyle(Curve.SHORT_DASH); plot ZeroLine = 0; ZeroLine.SetDefaultColor(Color.WHITE); ZeroLine.SetStyle(Curve.FIRM); plot ModerateBearish = -0.3; ModerateBearish.SetDefaultColor(Color.GRAY); ModerateBearish.SetStyle(Curve.SHORT_DASH); plot ExtremeBearish = -extremeThreshold; ExtremeBearish.SetDefaultColor(Color.DARK_RED); ExtremeBearish.SetStyle(Curve.SHORT_DASH); # Market condition determination def isExtremeBullish = ad_ratio > extremeThreshold; def isExtremeBearish = ad_ratio < -extremeThreshold; def improving = ad_ratio_smooth > ad_ratio_smooth[smoothingLength]; def deteriorating = ad_ratio_smooth < ad_ratio_smooth[smoothingLength]; # Main action label AddLabel(yes, if isExtremeBullish and accelerating then "EXTREME BULLISH - Buy Dips!" else if isExtremeBullish and !accelerating then "Overbought - Take Profits" else if ad_ratio > 0.3 and improving then "Bullish Breadth - Long Bias" else if isExtremeBearish and decelerating then "EXTREME BEARISH - Sell Rips!" else if isExtremeBearish and !decelerating then "Oversold - Cover Shorts" else if ad_ratio < -0.3 and deteriorating then "Bearish Breadth - Short Bias" else if AbsValue(ad_ratio) < 0.3 then "Mixed Market - Be Selective" else "Transitioning - Wait", if isExtremeBullish and accelerating then Color.GREEN else if isExtremeBullish then Color.YELLOW else if ad_ratio > 0.3 then Color.DARK_GREEN else if isExtremeBearish and decelerating then Color.RED else if isExtremeBearish then Color.YELLOW else if ad_ratio < -0.3 then Color.DARK_RED else Color.GRAY ); # Breadth details AddLabel(yes, "A/D: " + adv + "/" + dec + " (" + Round(ad_ratio * 100, 0) + "%) | " + "Vol: " + Round(advVol/1000000, 1) + "M/" + Round(decVol/1000000, 1) + "M", if ad_ratio > 0 then Color.GREEN else Color.RED ); # Divergence warnings AddLabel(bullishDivergence and showDivergence, "BULLISH DIVERGENCE: Price ↓ Breadth ↑", Color.GREEN ); AddLabel(bearishDivergence and showDivergence, "BEARISH DIVERGENCE: Price ↑ Breadth ↓", Color.RED ); # Extreme readings alert AddLabel(isExtremeBullish or isExtremeBearish, if isExtremeBullish then "⚠ EXTREME READING: " + Round(breadthThrust, 0) + "% Advancing" else "⚠ EXTREME READING: " + Round(100 - breadthThrust, 0) + "% Declining", Color.YELLOW );