Chop Stopper EMA Px

ChartTrader-X · study · 65 行 · 点赞 173 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ChartTrader-X
 
//@version=6
indicator('Chop Stopper EMA', shorttitle = 'Chop Free EMA', overlay = true)
 
// ── Inputs ──
emaPeriod = input.int(34, title = 'EMA Length', minval = 1, group = 'Settings')
lockBars = input.int(5, title = 'Confirmation Bars (N)', minval = 1, maxval = 100, group = 'Settings', tooltip = 'Consecutive closes on the same side of the EMA required to CONFIRM a trend flip.')
buildBars = input.int(1, title = 'Neutral Trigger Bars (n)', minval = 1, maxval = 100, group = 'Settings', tooltip = 'Consecutive closes on the same side needed to show the neutral/building color before full confirmation. Must be less than Confirmation Bars (N) to have any visible effect.')
 
upColor = input.color(color.lime, title = 'Bullish Color', group = 'Colors')
downColor = input.color(color.red, title = 'Bearish Color', group = 'Colors')
buildColor = input.color(color.black, title = 'Neutral/Building Color', group = 'Colors')
 
// ── Core calculation ──
emaLine = ta.ema(close, emaPeriod)
 
closeAbove = close > emaLine
closeBelow = close < emaLine
 
// Consecutive-close run counters (nz() guards against na on the first bar)
var int runAbove = 0
var int runBelow = 0
runAbove := closeAbove ? nz(runAbove[1]) + 1 : 0
runBelow := closeBelow ? nz(runBelow[1]) + 1 : 0
 
// ── Confirmation and persistent trend state ──
longLocked = runAbove >= lockBars
shortLocked = runBelow >= lockBars
 
var int trendState = 0 // 1 = long, -1 = short, 0 = undefined
if longLocked
    trendState := 1
    trendState
else if shortLocked
    trendState := -1
    trendState
 
flipLong = trendState == 1 and trendState[1] != 1
flipShort = trendState == -1 and trendState[1] != -1
 
// ── Building (neutral) phase: partial run that hasn't hit lockBars yet ──
// Clamp so the window is never empty if buildBars >= lockBars.
buildBarsEff = math.min(buildBars, lockBars - 1)
 
// The > 0 checks stop an idle side (run = 0) from counting as "building"
// when lockBars = 1 forces buildBarsEff to 0.
isBuilding = runAbove > 0 and runAbove >= buildBarsEff and runAbove < lockBars or runBelow > 0 and runBelow >= buildBarsEff and runBelow < lockBars
 
// ── Visuals ──
lineColor = isBuilding ? buildColor : trendState == 1 ? upColor : trendState == -1 ? downColor : buildColor
plot(emaLine, title = 'EMA', color = lineColor, linewidth = 2)
 
plotshape(flipLong, title = 'Trend -> LONG', style = shape.triangleup, location = location.belowbar, color = color.new(upColor, 0), size = size.small)
plotshape(flipShort, title = 'Trend -> SHORT', style = shape.triangledown, location = location.abovebar, color = color.new(downColor, 0), size = size.small)
 
candleColor = isBuilding ? color.new(buildColor, 70) : trendState == 1 ? color.new(upColor, 70) : trendState == -1 ? color.new(downColor, 70) : na
barcolor(candleColor, title = 'Candle Tint')
 
bgcolor(trendState == 1 ? color.new(upColor, 92) : trendState == -1 ? color.new(downColor, 92) : na, title = 'Trend Zone')
 
// ── Alerts ──
alertcondition(flipLong, title = 'Chop Stopper: flipped LONG', message = 'Chop Stopper EMA flipped LONG')
alertcondition(flipShort, title = 'Chop Stopper: flipped SHORT', message = 'Chop Stopper EMA flipped SHORT')

← 返回列表