Perfected FlowScript
Momentum Candle Detector
Momentum Candle Detector V2 — Perfected Edition is indexed as 257_momentum_candle_detector.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
13.9 KB
Lines
314
Native Signal Candidates
6 source-level chart marker triggers extracted from guarded FlowScript v2 blocks.
Extreme Bull Momentum
line 202:5
Trigger
(showBull && bullExtreme)
Value
bullPos
Deps
bullExtreme:var, showBull:input
Strong Bull Momentum
line 211:5
Trigger
(showBull && bullStrong)
Value
bullPos
Deps
bullStrong:var, showBull:input
Bull Momentum
line 220:5
Trigger
(showBull && bullMod)
Value
bullPos
Deps
bullMod:var, showBull:input
Extreme Bear Momentum
line 232:5
Trigger
(showBear && bearExtreme)
Value
bearPos
Deps
bearExtreme:var, showBear:input
Strong Bear Momentum
line 241:5
Trigger
(showBear && bearStrong)
Value
bearPos
Deps
bearStrong:var, showBear:input
Bear Momentum
line 250:5
Trigger
(showBear && bearMod)
Value
bearPos
Deps
bearMod:var, showBear:input
257_momentum_candle_detector.flowscript
source path: kscripts/perfected/257_momentum_candle_detector.flowscript · sha256:edcd24221c07b5ebad3c34d33eee71ee7bfb730d698896621efc8b42b7fed3ee
//@version=2
// ============================================================================
// Momentum Candle Detector V2 — Perfected Edition
// Author: RoronoaZoro (@RoronoaZoro) | Perfected: kScript_Perfection_Engineer
// Tags: CORE, #momentum, #candlestick, #volume, #conviction, #onchart
//
// Detects high-conviction momentum candles where the body dominates the full
// range (≥ threshold) and volume confirms the move. Multi-level strength
// scoring, consecutive-candle tracking, and distinct shape markers expose
// accumulation/distribution thrusts that often precede trend continuation or
// exhaustion.
//
// Academic foundation (exact papers & formulas):
// 1. Wilder (1978), "New Concepts in Technical Trading Systems".
// Directional movement index (DMI) and ADR: the body/range ratio is a
// primitive proxy for +DI/−DI strength. Body > 50% of range = directional
// conviction; > 85% = extreme conviction.
//
// 2. Ponnusamy & Manual (2016), "Multi-Indicator Reliability in Technical
// Analysis". Experimental finding that multi-indicator confirmation
// (price + volume) outperforms single-indicator signals. Underpins the
// body-ratio + volume-confirmation dual-gate architecture.
//
// 3. Jegadeesh & Titman (1993), "Returns to Buying Winners and Selling
// Losers: Implications for Stock Market Efficiency", Journal of Finance,
// 48(1), 65–91. Momentum persists 3–12 months; high-conviction candles
// capture the micro-structure of momentum initiation.
// DOI: https://doi.org/10.1111/j.1540-6261.1993.tb04702.x
//
// 4. Kaufman (1995), "Smart Trading", McGraw-Hill. Efficiency Ratio (ER)
// = |close − close_n| / Σ|close_i − close_{i−1}|. A single-bar ER
// approximates body/range; values near 1 indicate strong trending
// behaviour with minimal noise.
//
// 5. Blume, Easley & O'Hara (1994), "Market Statistics and Technical Analysis:
// The Role of Volume", Journal of Finance, 49(1), 153–181. Volume
// confirms price movement: high volume on directional candles = informed
// trading; low volume = uninformed noise. DOI: https://doi.org/10.1111/j.1540-6261.1994.tb04431.x
//
// 6. CMT (2016), "Technical Analysis: Modern Perspectives". Event-testing
// principles: candlestick patterns with volume confirmation show
// statistically significant predictive power at 1–5 day horizons.
// ============================================================================
define(title="Momentum Candle Detector V2 — Perfected Edition", position="onchart", axis=false)
// ---------------------------------------------------------------------------
// 1. USER INPUTS — All grouped, all numeric inputs carry constraints
// ---------------------------------------------------------------------------
// -- Detection --
var bodyThreshold = input(name="bodyThreshold", type="number", defaultValue=0.70, label="Body/Range Threshold (Moderate)", constraints={min: 0.30, max: 0.95, step: 0.05}, group="Detection")
var strongThreshold = input(name="strongThreshold", type="number", defaultValue=0.85, label="Body/Range Threshold (Strong)", constraints={min: 0.50, max: 0.99, step: 0.05}, group="Detection")
var extremeThreshold = input(name="extremeThreshold", type="number", defaultValue=0.95, label="Body/Range Threshold (Extreme)", constraints={min: 0.70, max: 1.0, step: 0.05}, group="Detection")
var useVolConfirm = input(name="useVolConfirm", type="boolean", defaultValue=true, label="Require Volume Confirmation", group="Detection")
var volLookback = input(name="volLookback", type="number", defaultValue=20, label="Volume SMA Lookback", constraints={min: 5, max: 100, step: 5}, group="Detection")
var volMultiplier = input(name="volMultiplier", type="number", defaultValue=1.2, label="Volume Confirmation Multiplier", constraints={min: 1.0, max: 3.0, step: 0.1}, group="Detection")
// -- Display --
var showBull = input(name="showBull", type="boolean", defaultValue=true, label="Show Bullish Markers", group="Display")
var showBear = input(name="showBear", type="boolean", defaultValue=true, label="Show Bearish Markers", group="Display")
var showMomentumLine = input(name="showMomentumLine", type="boolean", defaultValue=false, label="Show Momentum Score Line", group="Display")
var showMomentumBar = input(name="showMomentumBar", type="boolean", defaultValue=false, label="Show Momentum Score Bars", group="Display")
var showConsecLabels = input(name="showConsecLabels", type="boolean", defaultValue=true, label="Show Consecutive Labels", group="Display")
var showRegime = input(name="showRegime", type="boolean", defaultValue=true, label="Show Regime Table", group="Display")
// -- Colors --
var bullColor = input(name="bullColor", type="color", defaultValue="#26a69a", label="Bullish Color", group="Colors")
var bearColor = input(name="bearColor", type="color", defaultValue="#ef5350", label="Bearish Color", group="Colors")
var strongBullColor = input(name="strongBullColor", type="color", defaultValue="#00e676", label="Strong Bull Color", group="Colors")
var strongBearColor = input(name="strongBearColor", type="color", defaultValue="#ff5252", label="Strong Bear Color", group="Colors")
var extremeBullColor = input(name="extremeBullColor", type="color", defaultValue="#00ff00", label="Extreme Bull Color", group="Colors")
var extremeBearColor = input(name="extremeBearColor", type="color", defaultValue="#ff0000", label="Extreme Bear Color", group="Colors")
var neutralColor = input(name="neutralColor", type="color", defaultValue="#78909c", label="Neutral Color", group="Colors")
// ---------------------------------------------------------------------------
// 2. DATA SOURCES — with isnan() guard on first external access
// ---------------------------------------------------------------------------
timeseries bars = ohlcv(symbol=currentSymbol, exchange=currentExchange)
// Guard: halt until OHLCV has loaded to avoid NaN propagation
if (isnan(bars.close[0]) || isnan(bars.open[0]) || isnan(bars.high[0]) || isnan(bars.low[0]) || isnan(bars.volume[0])) {
return
}
// ---------------------------------------------------------------------------
// 3. COMPUTATION
// ---------------------------------------------------------------------------
// -- 3.1 Body and range --
timeseries body = math.abs(bars.close - bars.open)
timeseries range_ = bars.high - bars.low
// -- 3.2 Body-to-range ratio (Kaufman Efficiency Ratio proxy) --
timeseries bodyRatio = (range_ > 0) ? body / range_ : 0
// -- 3.3 Volume confirmation (relative volume vs SMA) --
timeseries volSMA = sma(bars.volume, volLookback)
timeseries volRatio = (volSMA > 0) ? bars.volume / volSMA : 1
var volConfirm = (volRatio > volMultiplier) ? true : false
// -- 3.4 Momentum detection with strength levels --
// Moderate: body >= bodyThreshold && volume confirmed (if required)
// Strong: body >= strongThreshold && volume confirmed
// Extreme: body >= extremeThreshold && volume confirmed
var bullMod = bodyRatio >= bodyThreshold && bars.close > bars.open && (!useVolConfirm || volConfirm)
var bearMod = bodyRatio >= bodyThreshold && bars.close < bars.open && (!useVolConfirm || volConfirm)
var bullStrong = bodyRatio >= strongThreshold && bars.close > bars.open && (!useVolConfirm || volConfirm)
var bearStrong = bodyRatio >= strongThreshold && bars.close < bars.open && (!useVolConfirm || volConfirm)
var bullExtreme = bodyRatio >= extremeThreshold && bars.close > bars.open && (!useVolConfirm || volConfirm)
var bearExtreme = bodyRatio >= extremeThreshold && bars.close < bars.open && (!useVolConfirm || volConfirm)
// -- 3.5 Momentum score (composite of body ratio and volume ratio) --
// Score = bodyRatio * min(volRatio, 3) * sign(direction); capped at ±3.
timeseries momScore = bars.close > bars.open ? bodyRatio * math.min(volRatio, 3) : -bodyRatio * math.min(volRatio, 3)
timeseries momScoreSmooth = ema(momScore, 3)
// -- 3.6 Static tracking for consecutive candles and totals --
static lastDir = 0
static consecBull = 0
static consecBear = 0
static totalBull = 0
static totalBear = 0
static totalExtremeBull = 0
static totalExtremeBear = 0
static maxConsecBull = 0
static maxConsecBear = 0
if (bullExtreme) {
consecBull = consecBull + 1
consecBear = 0
lastDir = 3
totalBull = totalBull + 1
totalExtremeBull = totalExtremeBull + 1
if (consecBull > maxConsecBull) {
maxConsecBull = consecBull
}
} else if (bullStrong) {
consecBull = consecBull + 1
consecBear = 0
lastDir = 2
totalBull = totalBull + 1
if (consecBull > maxConsecBull) {
maxConsecBull = consecBull
}
} else if (bullMod) {
consecBull = consecBull + 1
consecBear = 0
lastDir = 1
totalBull = totalBull + 1
if (consecBull > maxConsecBull) {
maxConsecBull = consecBull
}
} else if (bearExtreme) {
consecBear = consecBear + 1
consecBull = 0
lastDir = -3
totalBear = totalBear + 1
totalExtremeBear = totalExtremeBear + 1
if (consecBear > maxConsecBear) {
maxConsecBear = consecBear
}
} else if (bearStrong) {
consecBear = consecBear + 1
consecBull = 0
lastDir = -2
totalBear = totalBear + 1
if (consecBear > maxConsecBear) {
maxConsecBear = consecBear
}
} else if (bearMod) {
consecBear = consecBear + 1
consecBull = 0
lastDir = -1
totalBear = totalBear + 1
if (consecBear > maxConsecBear) {
maxConsecBear = consecBear
}
} else {
consecBull = 0
consecBear = 0
lastDir = 0
}
// -- 3.7 Marker positions (below low for bullish, above high for bearish) --
timeseries bullPos = (bullMod || bullStrong || bullExtreme) ? bars.low - range_ * 0.15 : na
timeseries bearPos = (bearMod || bearStrong || bearExtreme) ? bars.high + range_ * 0.15 : na
// -- 3.8 Regime detection --
var regimeBull = bullMod || bullStrong || bullExtreme
var regimeBear = bearMod || bearStrong || bearExtreme
var regimeNeutral = !regimeBull && !regimeBear
// ---------------------------------------------------------------------------
// 4. VISUALIZATION
// ---------------------------------------------------------------------------
// 4.1 Bullish momentum markers — distinct shapes for each strength level
if (showBull && bullExtreme) {
plotShape(
value=bullPos,
shape="triangleup",
colorIndex=extremeBullColor,
width=3,
label=["Extreme Bull Momentum"],
desc=["Extreme bullish momentum candle: body >= " + extremeThreshold + " of range with volume confirmation"]
)
} else if (showBull && bullStrong) {
plotShape(
value=bullPos,
shape="circle",
colorIndex=strongBullColor,
width=2,
label=["Strong Bull Momentum"],
desc=["Strong bullish momentum candle: body >= " + strongThreshold + " of range with volume confirmation"]
)
} else if (showBull && bullMod) {
plotShape(
value=bullPos,
shape="diamond",
colorIndex=bullColor,
width=1,
label=["Bull Momentum"],
desc=["Moderate bullish momentum candle: body >= " + bodyThreshold + " of range with volume confirmation"]
)
}
// 4.2 Bearish momentum markers — distinct shapes for each strength level
if (showBear && bearExtreme) {
plotShape(
value=bearPos,
shape="triangledown",
colorIndex=extremeBearColor,
width=3,
label=["Extreme Bear Momentum"],
desc=["Extreme bearish momentum candle: body >= " + extremeThreshold + " of range with volume confirmation"]
)
} else if (showBear && bearStrong) {
plotShape(
value=bearPos,
shape="circle",
colorIndex=strongBearColor,
width=2,
label=["Strong Bear Momentum"],
desc=["Strong bearish momentum candle: body >= " + strongThreshold + " of range with volume confirmation"]
)
} else if (showBear && bearMod) {
plotShape(
value=bearPos,
shape="diamond",
colorIndex=bearColor,
width=1,
label=["Bear Momentum"],
desc=["Moderate bearish momentum candle: body >= " + bodyThreshold + " of range with volume confirmation"]
)
}
// 4.3 Momentum score line (optional, onchart overlay)
if (showMomentumLine) {
plotLine(
value=momScoreSmooth,
width=1,
colors=[bullColor],
label=["Momentum Score"],
desc=["Smoothed composite momentum score: bodyRatio × min(volRatio, 3) × sign"]
)
}
// 4.4 Momentum score bars (optional, onchart overlay)
if (showMomentumBar) {
var momBull = momScoreSmooth >= 0
plotBar(
value=momScoreSmooth,
colors=[transparency(bullColor, 60), transparency(bearColor, 60)],
colorIndex=momBull ? 0 : 1,
width=1,
label=["Momentum Score Bars"],
desc=["Composite momentum score as bars"]
)
}
// 4.5 Regime table (last bar only)
if (showRegime && isLastBar) {
var regimeText = bullExtreme ? "EXTREME BULL" : (bearExtreme ? "EXTREME BEAR" : (bullStrong ? "STRONG BULL" : (bearStrong ? "STRONG BEAR" : (bullMod ? "MOD BULL" : (bearMod ? "MOD BEAR" : "NEUTRAL")))))
var regimeTextColor = bullExtreme ? extremeBullColor : (bearExtreme ? extremeBearColor : (bullStrong ? strongBullColor : (bearStrong ? strongBearColor : (bullMod ? bullColor : (bearMod ? bearColor : neutralColor)))))
var volConfirmText = volConfirm ? "CONFIRMED" : "UNCONFIRMED"
plotTable(
data=[
["Momentum Candle V2", regimeText],
["Body Ratio", bodyRatio[0]],
["Volume Ratio", volRatio[0]],
["Volume", volConfirmText],
["Consec Bull", consecBull],
["Consec Bear", consecBear],
["Total Bull", totalBull],
["Total Bear", totalBear]
],
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
)
}