Upper Scan Script (ToS)
# ==========================================
# Title: M&T Stoplight Master Scanner
# Description: Optimized scanner for Full Green, Full Red, Turning signals, and isolated Proxy logic.
# Usage:
# - For your primary trading timeframe, set 'useConfirmationOnly' to NO.
# - To scan specifically for Proxy shifts, select the Proxy modes in 'scanMode'.
# Created By: TheInvestorToolkit.com
# ==========================================
# ==========================================
# 1. INPUTS
# ==========================================
input scanMode = {default "Full Green", "Full Red", "Turning Bullish", "Turning Bearish", "Proxy Just Flipped Bullish", "Proxy Just Flipped Bearish", "Proxy Bullish Fading", "Proxy Bearish Fading"};
# Toggle to ignore MA/Proxy for Higher Timeframe filters
input useConfirmationOnly = no;
input priceSource = close;
input maType = {default "HWEMA", "SMA", "EMA", "Hull"};
input maLength = 20;
input proxyLength = 20;
# ==========================================
# 2. MA CALCULATIONS
# ==========================================
def maVal = if maType == maType.SMA then Average(priceSource, maLength)
else if maType == maType.EMA then ExpAverage(priceSource, maLength)
else if maType == maType.Hull then HullMovingAvg(priceSource, maLength)
else HullMovingAvg(WMA(priceSource, maLength), maLength);
def maUp = maVal > maVal[1];
def maDn = maVal < maVal[1];
# --- REVISED LOGIC (Alignment with Indicator) ---
# Turning Bullish (Yellow): Downsloping MA but price has closed ABOVE it.
def isTurningBull = close > maVal and maDn;
# Turning Bearish (Orange): Upsloping MA but price has closed BELOW it.
def isTurningBear = close < maVal and maUp;
# Standard Bull/Bear
def isPmaBull = close > maVal and maUp;
def isPmaBear = close < maVal and maDn;
# ==========================================
# 3. PROXY CALCULATIONS
# ==========================================
def logClose = Log(close);
# Optimization: Manual sum for scanner stability
def rocSum = fold i = 1 to proxyLength with s = 0 do s + (logClose - logClose[i]);
def rocAverage = rocSum / proxyLength;
def proxyBull = rocAverage >= 0;
def proxyBear = rocAverage < 0;
def proxyRising = rocAverage > rocAverage[1];
def proxyFalling = rocAverage < rocAverage[1];
# --- Proxy States —
def proxyJustFlippedBull = proxyBull and !proxyBull[1];
def proxyJustFlippedBear = proxyBear and !proxyBear[1];
# Fading logic: Bullish but momentum is slowing (falling) or Bearish but momentum is improving (rising)
def proxyBullFading = proxyBull and proxyFalling;
def proxyBearFading = proxyBear and proxyRising;
# ==========================================
# 4. OPEN LOGIC
# ==========================================
def currentOpen = open;
def isGreenOpen = close > currentOpen;
def isRedOpen = close < currentOpen;
# ==========================================
# 5. SCAN LOGIC
# ==========================================
# Full logic for the execution timeframe
def fullGreen = isPmaBull and maUp and (proxyBull and proxyRising) and isGreenOpen;
def fullRed = isPmaBear and maDn and (proxyBear and proxyFalling) and isRedOpen;
# Simplified logic for higher timeframe confirmation
def confirmGreen = isGreenOpen;
def confirmRed = isRedOpen;
# Final logic choice based on user input
def finalGreen = if useConfirmationOnly then confirmGreen else fullGreen;
def finalRed = if useConfirmationOnly then confirmRed else fullRed;
# Turning Signals (PtoMA based on the revised logic)
def signalTurningBull = isTurningBull and !isTurningBull[1];
def signalTurningBear = isTurningBear and !isTurningBear[1];
# ==========================================
# 6. FINAL OUTPUT
# ==========================================
plot scan = if scanMode == scanMode."Full Green" then finalGreen
else if scanMode == scanMode."Full Red" then finalRed
else if scanMode == scanMode."Turning Bullish" then signalTurningBull
else if scanMode == scanMode."Turning Bearish" then signalTurningBear
else if scanMode == scanMode."Proxy Just Flipped Bullish" then proxyJustFlippedBull
else if scanMode == scanMode."Proxy Just Flipped Bearish" then proxyJustFlippedBear
else if scanMode == scanMode."Proxy Bullish Fading" then proxyBullFading
else if scanMode == scanMode."Proxy Bearish Fading" then proxyBearFading
else 0;