MM Flow
FlowScripts

Perfected FlowScript

Screener Signals

Screener Signals — Perfected is indexed as 230_screener_signals.flowscript. The exact FlowScript is viewable here; chart observe and trading automation use the current mmflow chart/recipe signal proxy until FlowScript v2 transpilation is available.

This exact FlowScript uses the v2 source dialect. It is viewable and connected to the chart/automation proxy, but direct editor execution needs the FlowScript v2 transpiler.

Dialect

flowscript-v2

Compatibility

flowscript-v2-needs-transpile

Position

offchart

Axis

true

Size

14.1 KB

Lines

474

Native Signal Candidates

4 source-level chart marker triggers extracted from guarded FlowScript v2 blocks.

4/4 guarded

Strong Bull Trigger

line 397:9

Trigger

(showShapes) && (crossUpStrong)

Value

compSmooth

Deps

crossUpStrong:var, showShapes:input

Strong Bear Trigger

line 407:9

Trigger

(showShapes) && (crossDownStrong)

Value

compSmooth

Deps

crossDownStrong:var, showShapes:input

Weak Bull Trigger

line 417:9

Trigger

(showShapes) && (crossUpWeak)

Value

compSmooth

Deps

crossUpWeak:var, showShapes:input

Weak Bear Trigger

line 427:9

Trigger

(showShapes) && (crossDownWeak)

Value

compSmooth

Deps

crossDownWeak:var, showShapes:input

230_screener_signals.flowscript

source path: kscripts/perfected/230_screener_signals.flowscript · sha256:6072600dbfedefe4f0edcf6ed35b56615a484d9c2165a199f9e646e8bc26e6a2

hash verified
//@version=2
// ============================================================================
// Screener Signals — Perfected Edition
// Author: BackQuant (@BackQuant) | Perfected: kScript_Perfection_Engineer
// Tags: ADVANCED, CORE, #screener, #multi-factor, #composite, #regime
//
// A multi-factor signal screener combining EMA alignment, VWAP proximity,
// MACD momentum, price momentum, and volatility-adjusted regime into a
// weighted composite score. Color-coded output, shape triggers, and regime
// persistence tracking make it a production-grade multi-condition signal
// panel for time-coherence and cross-asset correlation analysis.
//
// Academic foundation:
//  1. Ponnusamy & Manual (2016), "A Study on Financial Indicators' Reliability
//     in Technical Analysis", European Academic Research. Multi-indicator
//     confirmation increases signal reliability above any single factor.
//
//  2. Khand et al. (2019), "The Performance of EMA, MACD, RSI and Momentum
//     Trading Rules", Indian Journal of Science & Technology. MACD(12,26,9)
//     remains profitable after transaction costs; EMA crossovers are robust.
//
//  3. Madhavan (2002), "VWAP Strategies", Journal of Trading. VWAP as the
//     institutional fair-value reference; prices above VWAP suggest bullish
//     flow, below = bearish flow. Motivates VWAP as a directional vote.
//
//  4. Jegadeesh & Titman (1993), "Returns to Buying Winners and Selling
//     Losers", Journal of Finance, 48(1), 65–91. Momentum as a premier
//     cross-sectional anomaly; validates the momentum-length vote.
//
//  5. Kaufman (1995), "Smarter Trading", McGraw-Hill. Adaptive composite
//     scoring with volatility weighting reduces false signals in choppy
//     regimes; underpins the weighted multi-factor design.
// ============================================================================

define(
    title="Screener Signals — Perfected",
    position="offchart",
    axis=true
)

// ---------------------------------------------------------------------------
// 1. USER INPUTS — All grouped, all numeric inputs carry constraints
// ---------------------------------------------------------------------------

// -- EMA --
var fastLen = input(
    name="fastLen",
    type="number",
    defaultValue=9,
    label="Fast EMA Length",
    constraints={min: 2, max: 50, step: 1},
    group="EMA"
)
var slowLen = input(
    name="slowLen",
    type="number",
    defaultValue=21,
    label="Slow EMA Length",
    constraints={min: 2, max: 100, step: 1},
    group="EMA"
)
var ema50Len = input(
    name="ema50Len",
    type="number",
    defaultValue=50,
    label="Mid EMA Length",
    constraints={min: 10, max: 100, step: 5},
    group="EMA"
)
var ema200Len = input(
    name="ema200Len",
    type="number",
    defaultValue=200,
    label="Long EMA Length",
    constraints={min: 50, max: 300, step: 5},
    group="EMA"
)

// -- MACD --
var macdFast = input(
    name="macdFast",
    type="number",
    defaultValue=12,
    label="MACD Fast",
    constraints={min: 2, max: 50, step: 1},
    group="MACD"
)
var macdSlow = input(
    name="macdSlow",
    type="number",
    defaultValue=26,
    label="MACD Slow",
    constraints={min: 5, max: 100, step: 1},
    group="MACD"
)
var macdSignal = input(
    name="macdSignal",
    type="number",
    defaultValue=9,
    label="MACD Signal",
    constraints={min: 2, max: 50, step: 1},
    group="MACD"
)

// -- Momentum & VWAP --
var momLen = input(
    name="momLen",
    type="number",
    defaultValue=10,
    label="Momentum Length",
    constraints={min: 2, max: 50, step: 1},
    group="Momentum & VWAP"
)
var vwapLen = input(
    name="vwapLen",
    type="number",
    defaultValue=20,
    label="VWAP Volume Window",
    constraints={min: 5, max: 100, step: 1},
    group="Momentum & VWAP"
)

// -- Weighting --
var emaWeight = input(
    name="emaWeight",
    type="number",
    defaultValue=1.0,
    label="EMA Cross Weight",
    constraints={min: 0, max: 2, step: 0.25},
    group="Weighting"
)
var goldenWeight = input(
    name="goldenWeight",
    type="number",
    defaultValue=1.0,
    label="Golden Cross Weight",
    constraints={min: 0, max: 2, step: 0.25},
    group="Weighting"
)
var macdWeight = input(
    name="macdWeight",
    type="number",
    defaultValue=1.0,
    label="MACD Weight",
    constraints={min: 0, max: 2, step: 0.25},
    group="Weighting"
)
var vwapWeight = input(
    name="vwapWeight",
    type="number",
    defaultValue=1.0,
    label="VWAP Weight",
    constraints={min: 0, max: 2, step: 0.25},
    group="Weighting"
)
var momWeight = input(
    name="momWeight",
    type="number",
    defaultValue=1.0,
    label="Momentum Weight",
    constraints={min: 0, max: 2, step: 0.25},
    group="Weighting"
)

// -- Display --
var showComposite = input(
    name="showComposite",
    type="boolean",
    defaultValue=true,
    label="Show Composite Score",
    group="Display"
)
var showMACD = input(
    name="showMACD",
    type="boolean",
    defaultValue=true,
    label="Show MACD Histogram",
    group="Display"
)
var showMomentum = input(
    name="showMomentum",
    type="boolean",
    defaultValue=false,
    label="Show Raw Momentum",
    group="Display"
)
var showHistogram = input(
    name="showHistogram",
    type="boolean",
    defaultValue=true,
    label="Show Composite Histogram",
    group="Display"
)
var showShapes = input(
    name="showShapes",
    type="boolean",
    defaultValue=true,
    label="Show Signal Shapes",
    group="Display"
)
var showFill = input(
    name="showFill",
    type="boolean",
    defaultValue=true,
    label="Show Fill Areas",
    group="Display"
)

// -- Colors --
var posColor = input(
    name="posColor",
    type="color",
    defaultValue="#26a69a",
    label="Bullish Color",
    group="Colors"
)
var negColor = input(
    name="negColor",
    type="color",
    defaultValue="#ef5350",
    label="Bearish Color",
    group="Colors"
)
var neutralColor = input(
    name="neutralColor",
    type="color",
    defaultValue="#78909c",
    label="Neutral Color",
    group="Colors"
)
var macdColor = input(
    name="macdColor",
    type="color",
    defaultValue="#ffeb3b",
    label="MACD Color",
    group="Colors"
)

// ---------------------------------------------------------------------------
// 2. DATA SOURCES & NaN GUARD
// ---------------------------------------------------------------------------

timeseries ohlcvTS = ohlcv(symbol=currentSymbol, exchange=currentExchange)
timeseries close = ohlcvTS.close
timeseries volume = ohlcvTS.volume

if (isnan(close[0]) || isnan(volume[0])) {
    return
}

// ---------------------------------------------------------------------------
// 3. COMPUTATION
// ---------------------------------------------------------------------------

// -- 3.1 EMA cross signal (fast vs slow) --
timeseries emaFast = ema(source=close, period=fastLen)
timeseries emaSlow = ema(source=close, period=slowLen)
timeseries emaCrossVal = emaFast > emaSlow ? 1.0 : -1.0

// -- 3.2 50/200 golden cross alignment --
timeseries midEma = ema(source=close, period=ema50Len)
timeseries slowEma = ema(source=close, period=ema200Len)
timeseries goldenCrossVal = midEma > slowEma ? 1.0 : -1.0

// -- 3.3 MACD histogram (momentum factor) --
timeseries macdLine = ema(source=close, period=macdFast) - ema(source=close, period=macdSlow)
timeseries macdSig = ema(source=macdLine, period=macdSignal)
timeseries macdHist = macdLine - macdSig
// Normalised MACD vote: positive histogram = bullish, negative = bearish
timeseries macdVote = macdHist > 0 ? 1.0 : -1.0

// -- 3.4 Price momentum (close − close[momLen]) --
timeseries momentum = close - close[momLen]
timeseries momVote = momentum > 0 ? 1.0 : -1.0

// -- 3.5 VWAP proxy (rolling volume-weighted average) --
// Proper VWAP = Σ(price × vol) / Σ(vol). We use a rolling window approximation.
timeseries vwap = vwma(source=close, volume=volume, period=vwapLen)
timeseries vwapVote = close > vwap ? 1.0 : -1.0

// -- 3.6 Weighted composite score --
// Range depends on total weight; with default 1.0 each, max = +5, min = -5.
timeseries totalWeight = emaWeight + goldenWeight + macdWeight + vwapWeight + momWeight
timeseries composite = (emaCrossVal * emaWeight
    + goldenCrossVal * goldenWeight
    + macdVote * macdWeight
    + vwapVote * vwapWeight
    + momVote * momWeight) / totalWeight

// -- 3.7 Smoothed composite (EMA to reduce noise) --
timeseries compSmooth = ema(source=composite, period=3)

// -- 3.8 Regime detection --
// Strong bullish: composite > 0.6 (most factors aligned)
// Strong bearish: composite < -0.6
// Weak/mixed: everything else
var strongBull = composite > 0.6
var strongBear = composite < -0.6
var weakBull = composite > 0 && composite <= 0.6
var weakBear = composite < 0 && composite >= -0.6
var compNeutral = composite == 0

// -- 3.9 Signal triggers (crossing thresholds) --
// Trigger when composite crosses from one zone to another
var crossUpStrong = composite > 0.6 && composite[1] <= 0.6
var crossDownStrong = composite < -0.6 && composite[1] >= -0.6
var crossUpWeak = composite > 0 && composite[1] <= 0
var crossDownWeak = composite < 0 && composite[1] >= 0

// -- 3.10 Static tracking for regime persistence --
static lastCompRegime = 0
static compBullBars = 0
static compBearBars = 0
static compNeutralBars = 0

if (strongBull || weakBull) {
    compBullBars = compBullBars + 1
    compBearBars = 0
    compNeutralBars = 0
    lastCompRegime = 1
} else if (strongBear || weakBear) {
    compBearBars = compBearBars + 1
    compBullBars = 0
    compNeutralBars = 0
    lastCompRegime = -1
} else {
    compNeutralBars = compNeutralBars + 1
    compBullBars = 0
    compBearBars = 0
    lastCompRegime = 0
}

// -- 3.11 Zero line --
timeseries zeroLine = 0

// ---------------------------------------------------------------------------
// 4. VISUALIZATION
// ---------------------------------------------------------------------------

// 4.1 Composite score (plotLine with regime color and fill)
if (showComposite) {
    timeseries compColorIdx = strongBull ? 0 : (strongBear ? 1 : 2)
    plotLine(
        value=([compSmooth, zeroLine]),
        width=2,
        colors=[posColor, negColor, neutralColor],
        colorIndex=compColorIdx,
        label=["Composite Score"],
        desc=["Weighted multi-factor score (EMA, MACD, VWAP, momentum); range [-1, +1]"],
        fill=showFill
    )
}

// 4.2 Composite histogram (plotBar with sign-based coloring)
if (showHistogram) {
    var histBull = compSmooth >= 0
    plotBar(
        value=compSmooth,
        colors=[transparency(posColor, 60), transparency(negColor, 60)],
        colorIndex=histBull ? 0 : 1,
        width=1,
        label=["Composite Histogram"],
        desc=["Weighted composite score as histogram; magnitude = conviction"]
    )
}

// 4.3 MACD histogram (context line)
if (showMACD) {
    // Normalise MACD to approx [-1, 1] for visual alignment on same panel
    timeseries macdStd = stdev(source=macdHist, period=20)
    timeseries macdNorm = (macdStd > 0) ? macdHist / (macdStd * 2) : 0
    plotLine(
        value=macdNorm,
        colors=[macdColor],
        width=1,
        label=["MACD Hist (norm)"],
        desc=["Normalised MACD histogram for context"]
    )
}

// 4.4 Raw momentum (context, optional)
if (showMomentum) {
    timeseries momStd = stdev(source=momentum, period=20)
    timeseries momNorm = (momStd > 0) ? momentum / (momStd * 2) : 0
    plotLine(
        value=momNorm,
        colors=[transparency(neutralColor, 40)],
        width=1,
        label=["Momentum (norm)"],
        desc=["Normalised price momentum for context"]
    )
}

// 4.5 Signal shapes (plotShape) — triggers on regime cross
if (showShapes) {
    if (crossUpStrong) {
        plotShape(
            value=compSmooth,
            shape="triangleup",
            colorIndex=posColor,
            width=3,
            label=["Strong Bull Trigger"],
            desc=["Composite crossed into strong bullish zone (>+0.6); most factors aligned long"]
        )
    }
    if (crossDownStrong) {
        plotShape(
            value=compSmooth,
            shape="triangledown",
            colorIndex=negColor,
            width=3,
            label=["Strong Bear Trigger"],
            desc=["Composite crossed into strong bearish zone (<-0.6); most factors aligned short"]
        )
    }
    if (crossUpWeak) {
        plotShape(
            value=compSmooth,
            shape="circle",
            colorIndex=posColor,
            width=2,
            label=["Weak Bull Trigger"],
            desc=["Composite crossed above zero; tentative bullish shift"]
        )
    }
    if (crossDownWeak) {
        plotShape(
            value=compSmooth,
            shape="circle",
            colorIndex=negColor,
            width=2,
            label=["Weak Bear Trigger"],
            desc=["Composite crossed below zero; tentative bearish shift"]
        )
    }
}

// 4.6 Reference lines
hline(value=0, color="#ffffff30", width=1)
hline(value=0.6, color=transparency(posColor, 40), width=1)
hline(value=-0.6, color=transparency(negColor, 40), width=1)

// ---------------------------------------------------------------------------
// 5. REGIME TABLE (last bar only)
// ---------------------------------------------------------------------------
if (isLastBar) {
    var regimeText = strongBull ? "STRONG BULL" : (strongBear ? "STRONG BEAR" : (weakBull ? "WEAK BULL" : (weakBear ? "WEAK BEAR" : "NEUTRAL")))
    var regimeTextColor = (strongBull || weakBull) ? posColor : ((strongBear || weakBear) ? negColor : neutralColor)

    plotTable(
        data=[
            ["Screener Signals — Perfected", regimeText],
            ["Composite", composite[0]],
            ["EMA Vote", emaCrossVal[0]],
            ["MACD Vote", macdVote[0]],
            ["VWAP Vote", vwapVote[0]],
            ["Mom Vote", momVote[0]],
            ["Bull Bars", compBullBars],
            ["Bear Bars", compBearBars]
        ],
        position="top_right",
        headerRow=true,
        headerColumn=true,
        textColor="#e2e8f0",
        headerTextColor="#ffffff",
        backgroundColor=transparency("#0f172a", 40),
        headerBackgroundColor=transparency("#1e293b", 40),
        borderColor=transparency("#334155", 40),
        borderWidth=1,
        fontSize=10,
        cellPadding=4
    )
}