Perfected FlowScript
EMA Guppy
EMA Guppy (GMMA) — Perfected Edition is indexed as 100_ema_guppy.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
onchart
Axis
false
Size
16.1 KB
Lines
472
Native Signal Candidates
5 source-level chart marker triggers extracted from guarded FlowScript v2 blocks.
Bull Cross
line 382:9
Trigger
(showCrossSignals) && (crossUpEvent)
Value
src
Deps
crossUpEvent:var, showCrossSignals:input
Bear Cross
line 392:9
Trigger
(showCrossSignals) && (crossDnEvent)
Value
src
Deps
crossDnEvent:var, showCrossSignals:input
Compression
line 405:9
Trigger
(showCompression) && (compressionEvent)
Value
src
Deps
compressionEvent:var, showCompression:input
Expansion
line 415:9
Trigger
(showCompression) && (expansionEvent)
Value
src
Deps
expansionEvent:var, showCompression:input
Unlabeled shape
line 431:5
Trigger
(isLastBar)
Value
src
Deps
isLastBar:builtin
100_ema_guppy.flowscript
source path: kscripts/perfected/100_ema_guppy.flowscript · sha256:7548830e6e00def6e338766d439a970049d50e2a556a3ec82c143ed25311e74b
//@version=2
// ============================================================================
// EMA Guppy (GMMA) — Perfected Edition
// Author: BackQuant (@BackQuant) | Enhanced by kScript Perfection Engineer
// Tags: TREND, #guppy, #gmma, #ribbon, #compression, #expansion, #crossover
//
// Production-grade Guppy Multiple Moving Average (GMMA) with two clusters of
// 6 EMAs each (short-term traders + long-term investors), ribbon fill between
// groups, compression/expansion detection, color-coded trend regimes, and
// shape markers for crossover events.
//
// Academic foundations:
// 1. Guppy (1990s-2000s), "Guppy Multiple Moving Average (GMMA)": short
// cluster (3,5,8,10,12,15) = trader sentiment; long cluster (30,35,40,
// 45,50,60) = investor behavior. Gap widening = trend strength;
// compression = momentum stall. [^9]
// 2. Manasa & Reddy (2025), "Trend Analysis by Weighted and Exponential
// Moving Average": EMA offers more stability than WMA; combined
// multi-horizon approach yields robust decisions. [^16]
// 3. Hurst, Ooi & Pedersen (2014), "A Century of Evidence on Trend-
// Following Investing": time-series momentum profitable since 1880
// across asset classes; trend-following performs well in bear markets. [^15]
// 4. Zakamulin (2023), "Optimal Trend-Following with Transaction Costs":
// optimal trend-following resembles simple MA crossover; provides
// mathematical justification for MA-based rules. [^27]
// 5. Brock, Lakonishok & LeBaron (1992), "Simple Technical Trading Rules":
// MA crossover rules significantly outperformed buy-and-hold over
// 90 years of DJIA data. [^23]
// ============================================================================
define(
title="EMA Guppy (GMMA) — Perfected Edition",
position="onchart",
axis=false
)
// -------------------------------------------------------------------------
// 1. USER INPUTS — grouped with constraints
// -------------------------------------------------------------------------
// --- Short Cluster ---
var showShort = input(
name="showShort",
type="boolean",
defaultValue=true,
label="Show Short Cluster (3-15)",
group="Short Cluster"
)
var shortWidth = input(
name="shortWidth",
type="number",
defaultValue=1,
label="Short Line Width",
constraints={min: 1, max: 5, step: 1},
group="Short Cluster"
)
var shortColorStart = input(
name="shortColorStart",
type="color",
defaultValue="#ffeb3b",
label="Short Start Color (fast)",
group="Short Cluster"
)
var shortColorEnd = input(
name="shortColorEnd",
type="color",
defaultValue="#9c27b0",
label="Short End Color (slow)",
group="Short Cluster"
)
// --- Long Cluster ---
var showLong = input(
name="showLong",
type="boolean",
defaultValue=true,
label="Show Long Cluster (30-60)",
group="Long Cluster"
)
var longWidth = input(
name="longWidth",
type="number",
defaultValue=2,
label="Long Line Width",
constraints={min: 1, max: 5, step: 1},
group="Long Cluster"
)
var longColorStart = input(
name="longColorStart",
type="color",
defaultValue="#3f51b5",
label="Long Start Color (fast)",
group="Long Cluster"
)
var longColorEnd = input(
name="longColorEnd",
type="color",
defaultValue="#8bc34a",
label="Long End Color (slow)",
group="Long Cluster"
)
// --- Ribbon & Fill ---
var showRibbonFill = input(
name="showRibbonFill",
type="boolean",
defaultValue=true,
label="Show Ribbon Fill (short vs long)",
group="Ribbon & Fill"
)
var showClusterFill = input(
name="showClusterFill",
type="boolean",
defaultValue=true,
label="Show Cluster Internal Fill",
group="Ribbon & Fill"
)
// --- Compression / Expansion ---
var showCompression = input(
name="showCompression",
type="boolean",
defaultValue=true,
label="Show Compression Markers",
group="Compression / Expansion"
)
var compLookback = input(
name="compLookback",
type="number",
defaultValue=20,
label="Compression Lookback",
constraints={min: 5, max: 100, step: 1},
group="Compression / Expansion"
)
var compThreshold = input(
name="compThreshold",
type="number",
defaultValue=0.3,
label="Compression Threshold (ratio)",
constraints={min: 0.05, max: 1.0, step: 0.05},
group="Compression / Expansion"
)
// --- Crossover Signals ---
var showCrossSignals = input(
name="showCrossSignals",
type="boolean",
defaultValue=true,
label="Show Crossover Shapes",
group="Crossover Signals"
)
// --- Colors ---
var signalUpColor = input(
name="signalUpColor",
type="color",
defaultValue="#00e676",
label="Bullish Crossover Color",
group="Colors"
)
var signalDnColor = input(
name="signalDnColor",
type="color",
defaultValue="#ff5252",
label="Bearish Crossover Color",
group="Colors"
)
var compColor = input(
name="compColor",
type="color",
defaultValue="#ff9800",
label="Compression Color",
group="Colors"
)
// -------------------------------------------------------------------------
// 2. DATA SOURCES & NaN GUARD
// -------------------------------------------------------------------------
timeseries bars = ohlcv(symbol=currentSymbol, exchange=currentExchange)
timeseries src = bars.close
if (isnan(src[0])) {
return
}
// -------------------------------------------------------------------------
// 3. EMA CLUSTER COMPUTATION
// -------------------------------------------------------------------------
// Short-term cluster (traders): 3, 5, 8, 10, 12, 15
var ema3 = ema(source=src, period=3)
var ema5 = ema(source=src, period=5)
var ema8 = ema(source=src, period=8)
var ema10 = ema(source=src, period=10)
var ema12 = ema(source=src, period=12)
var ema15 = ema(source=src, period=15)
// Long-term cluster (investors): 30, 35, 40, 45, 50, 60
var ema30 = ema(source=src, period=30)
var ema35 = ema(source=src, period=35)
var ema40 = ema(source=src, period=40)
var ema45 = ema(source=src, period=45)
var ema50 = ema(source=src, period=50)
var ema60 = ema(source=src, period=60)
// Cluster midlines
var shortMid = (ema3 + ema5 + ema8 + ema10 + ema12 + ema15) / 6
var longMid = (ema30 + ema35 + ema40 + ema45 + ema50 + ema60) / 6
// -------------------------------------------------------------------------
// 4. REGIME DETECTION
// -------------------------------------------------------------------------
// Bullish: shortMid > longMid AND price > shortMid (both clusters aligned up)
// Bearish: shortMid < longMid AND price < shortMid (both clusters aligned down)
// Compression: short cluster spread / long cluster spread < threshold
var trendUp = shortMid > longMid
var trendDn = shortMid < longMid
var priceAboveShort = src > ema15
var priceBelowShort = src < ema3
var alignedUp = trendUp && priceAboveShort
var alignedDn = trendDn && priceBelowShort
var alignedStrong = trendUp && ema3 > ema15 && ema30 > ema60
var alignedStrongDn = trendDn && ema3 < ema15 && ema30 < ema60
var regimeColor = alignedUp ? signalUpColor : (alignedDn ? signalDnColor : "#9e9e9e")
// -------------------------------------------------------------------------
// 5. COMPRESSION / EXPANSION DETECTION
// -------------------------------------------------------------------------
// Short spread = slowest short - fastest short
// Long spread = slowest long - fastest long
// Compression ratio = shortSpread / longSpread (or absolute)
var shortSpread = ema15 - ema3
var longSpread = ema60 - ema30
var absShortSpread = math.abs(shortSpread)
var absLongSpread = math.abs(longSpread)
// Normalize spread by mid price for cross-instrument comparability
var normShortSpread = absShortSpread / src
var normLongSpread = absLongSpread / src
// Compression: both spreads are historically narrow
var shortSpreadMin = lowest(source=normShortSpread, period=compLookback)
var longSpreadMin = lowest(source=normLongSpread, period=compLookback)
var shortSpreadMax = highest(source=normShortSpread, period=compLookback)
var longSpreadMax = highest(source=normLongSpread, period=compLookback)
var shortCompRatio = shortSpreadMax > 0 ? normShortSpread / shortSpreadMax : 1
var longCompRatio = longSpreadMax > 0 ? normLongSpread / longSpreadMax : 1
var isCompression = shortCompRatio < compThreshold && longCompRatio < compThreshold
var isExpansion = normShortSpread > shortSpreadMin * 3 && normLongSpread > longSpreadMin * 3
// -------------------------------------------------------------------------
// 6. STATIC STATE TRACKING
// -------------------------------------------------------------------------
static prevTrendUp = false
static prevTrendDn = false
static prevCompression = false
static prevExpansion = false
var crossUpEvent = false
var crossDnEvent = false
var compressionEvent = false
var expansionEvent = false
if (!isLiveUpdate) {
if (trendUp && !prevTrendUp) {
crossUpEvent = true
}
if (trendDn && !prevTrendDn) {
crossDnEvent = true
}
if (isCompression && !prevCompression) {
compressionEvent = true
}
if (isExpansion && !prevExpansion) {
expansionEvent = true
}
prevTrendUp = trendUp
prevTrendDn = trendDn
prevCompression = isCompression
prevExpansion = isExpansion
}
// -------------------------------------------------------------------------
// 7. PLOTTING: SHORT CLUSTER
// -------------------------------------------------------------------------
if (showShort) {
plotLine(value=ema3, width=shortWidth, colors=[shortColorStart], label=["EMA 3"], desc=["Short-term trader EMA (fastest)"])
plotLine(value=ema5, width=shortWidth, colors=[shortColorStart], label=["EMA 5"], desc=["Short-term trader EMA"])
plotLine(value=ema8, width=shortWidth, colors=[shortColorEnd], label=["EMA 8"], desc=["Short-term trader EMA"])
plotLine(value=ema10, width=shortWidth, colors=[shortColorEnd], label=["EMA 10"], desc=["Short-term trader EMA"])
plotLine(value=ema12, width=shortWidth, colors=[shortColorEnd], label=["EMA 12"], desc=["Short-term trader EMA"])
plotLine(value=ema15, width=shortWidth, colors=[shortColorEnd], label=["EMA 15"], desc=["Short-term trader EMA (slowest)"])
}
// Short cluster internal fill (between fastest and slowest short)
if (showShort && showClusterFill) {
var shortFillBull = ema3 > ema15
plotLine(
value=[ema3, ema15],
width=1,
colors=[transparency(shortColorStart, 30), transparency(shortColorEnd, 30)],
colorIndex=shortFillBull ? 0 : 1,
fill=true,
label=["Short Cluster", "Short Cluster"],
desc=["Short cluster top edge (EMA 3)", "Short cluster bottom edge (EMA 15)"]
)
}
// -------------------------------------------------------------------------
// 8. PLOTTING: LONG CLUSTER
// -------------------------------------------------------------------------
if (showLong) {
plotLine(value=ema30, width=longWidth, colors=[longColorStart], label=["EMA 30"], desc=["Long-term investor EMA (fastest)"])
plotLine(value=ema35, width=longWidth, colors=[longColorStart], label=["EMA 35"], desc=["Long-term investor EMA"])
plotLine(value=ema40, width=longWidth, colors=[longColorStart], label=["EMA 40"], desc=["Long-term investor EMA"])
plotLine(value=ema45, width=longWidth, colors=[longColorEnd], label=["EMA 45"], desc=["Long-term investor EMA"])
plotLine(value=ema50, width=longWidth, colors=[longColorEnd], label=["EMA 50"], desc=["Long-term investor EMA"])
plotLine(value=ema60, width=longWidth, colors=[longColorEnd], label=["EMA 60"], desc=["Long-term investor EMA (slowest)"])
}
// Long cluster internal fill (between fastest and slowest long)
if (showLong && showClusterFill) {
var longFillBull = ema30 > ema60
plotLine(
value=[ema30, ema60],
width=1,
colors=[transparency(longColorStart, 30), transparency(longColorEnd, 30)],
colorIndex=longFillBull ? 0 : 1,
fill=true,
label=["Long Cluster", "Long Cluster"],
desc=["Long cluster top edge (EMA 30)", "Long cluster bottom edge (EMA 60)"]
)
}
// -------------------------------------------------------------------------
// 9. PLOTTING: RIBBON FILL BETWEEN GROUPS
// -------------------------------------------------------------------------
if (showRibbonFill) {
// Fill between short cluster mid and long cluster mid
var ribbonBull = shortMid > longMid
plotLine(
value=[shortMid, longMid],
width=1,
colors=[transparency(signalUpColor, 20), transparency(signalDnColor, 20)],
colorIndex=ribbonBull ? 0 : 1,
fill=true,
label=["Short Mid", "Long Mid"],
desc=["Short cluster midline (avg of 3/5/8/10/12/15)", "Long cluster midline (avg of 30/35/40/45/50/60)"]
)
}
// -------------------------------------------------------------------------
// 10. PLOTTING: MIDLINE & REGIME LABEL
// -------------------------------------------------------------------------
// Short and long midlines (always visible for reference)
plotLine(
value=shortMid,
width=1,
colors=["#ffffff60"],
label=["Short Mid"],
desc=["Average of short cluster EMAs"]
)
plotLine(
value=longMid,
width=1,
colors=["#ffffff60"],
label=["Long Mid"],
desc=["Average of long cluster EMAs"]
)
// -------------------------------------------------------------------------
// 11. SHAPE MARKERS
// -------------------------------------------------------------------------
if (showCrossSignals) {
if (crossUpEvent) {
plotShape(
value=src,
shape="triangleup",
colorIndex=signalUpColor,
width=2,
label=["Bull Cross"],
desc=["Short cluster crossed above long cluster — bullish GMMA alignment"]
)
}
if (crossDnEvent) {
plotShape(
value=src,
shape="triangledown",
colorIndex=signalDnColor,
width=2,
label=["Bear Cross"],
desc=["Short cluster crossed below long cluster — bearish GMMA alignment"]
)
}
}
if (showCompression) {
if (compressionEvent) {
plotShape(
value=src,
shape="rectangle",
colorIndex=compColor,
width=2,
label=["Compression"],
desc=["Both clusters compressed — potential breakout imminent"]
)
}
if (expansionEvent) {
plotShape(
value=src,
shape="circle",
colorIndex=signalUpColor,
width=1,
label=["Expansion"],
desc=["Cluster expansion detected — trend strengthening"]
)
}
}
// --- Regime label on last bar ---
if (isLastBar) {
var regimeText = alignedUp ? "BULLISH TREND" : (alignedDn ? "BEARISH TREND" : (isCompression ? "COMPRESSION" : "NEUTRAL"))
var regimeTextColor = alignedUp ? signalUpColor : (alignedDn ? signalDnColor : (isCompression ? compColor : "#9e9e9e"))
plotPriceLabel(
price=src,
text=regimeText,
type="callout",
color="#ffffff",
backgroundColor=regimeTextColor,
size=10,
fontWeight=700
)
}
// -------------------------------------------------------------------------
// 12. REGIME TABLE (last bar only)
// -------------------------------------------------------------------------
if (isLastBar) {
var trendText = alignedUp ? "BULLISH" : (alignedDn ? "BEARISH" : "NEUTRAL")
var trendTextColor = alignedUp ? signalUpColor : (alignedDn ? signalDnColor : "#9e9e9e")
plotTable(
data=[
["EMA Guppy (GMMA) — Perfected", trendText],
["Short Mid", shortMid[0]],
["Long Mid", longMid[0]],
["Spread Ratio", shortCompRatio[0]],
["Compression", isCompression ? "YES" : "NO"],
["Expansion", isExpansion ? "YES" : "NO"],
["Strong Trend", alignedStrong ? "YES" : (alignedStrongDn ? "DOWN" : "NO")]
],
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
)
}