本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MiesOnCharts
// __ __ _
// | \/ (_) ___ ___
// | |\/| | |/ _ \/ __|
// | | | | | __/\__ \
// |_| |_|_|\___||___/
//@version=6
// ============================================================
// Z-SCORE TREND OSCILLATOR
// Indicator: Standard-score normalization. z = (P − μ) / σ where
// μ, σ are the rolling mean and std-dev.
// ============================================================
indicator("Z-Score Trend Oscillator [MiesOnCharts]", shorttitle = "ZTO - Mies", overlay = false)
src = input.source(close, "Source")
len = input.int(50, "Lookback (μ, σ)", minval = 5)
smooth = input.int(3, "Smoothing (EMA of z)", minval = 1)
zEnter = input.float(1.0, "Entry threshold |z|", step = 0.1)
zExit = input.float(0.25, "Exit threshold |z|", step = 0.05)
mu = ta.sma(src, len)
sigma = ta.stdev(src, len)
zRaw = sigma > 0 ? (src - mu) / sigma : 0.0
z = ta.ema(zRaw, smooth)
// --- Hysteresis (Schmitt trigger) state ----------------------
var int regime = 0 // 1 = uptrend, -1 = downtrend, 0 = neutral
if regime <= 0 and z > zEnter
regime := 1
else if regime >= 0 and z < -zEnter
regime := -1
else if regime == 1 and z < zExit
regime := 0
else if regime == -1 and z > -zExit
regime := 0
// --- Rendering ------------------------------------------------
colUp = color.rgb(0, 255, 21)
colDn = color.rgb(255, 0, 0)
colFlat = color.new(color.gray, 20)
zCol = regime == 1 ? colUp : regime == -1 ? colDn : colFlat
plot(z, "Z-score", color = zCol, linewidth = 2)
h0 = hline(0, "Zero", color = color.new(color.gray, 50))
hu = hline(zEnter, "Upper entry", color = color.new(colUp, 60), linestyle = hline.style_dashed)
hd = hline(-zEnter, "Lower entry", color = color.new(colDn, 60), linestyle = hline.style_dashed)
hline(zExit, "Upper exit", color = color.new(color.gray, 75), linestyle = hline.style_dotted)
hline(-zExit, "Lower exit", color = color.new(color.gray, 75), linestyle = hline.style_dotted)
bgcolor(regime == 1 ? color.new(colUp, 92) : regime == -1 ? color.new(colDn, 92) : na)
barcolor(regime == 1 ? colUp : regime == -1 ? colDn : na)
upStart = regime == 1 and regime[1] != 1
dnStart = regime == -1 and regime[1] != -1
alertcondition(upStart, "Z-trend UP", "Z-score entered uptrend regime")
alertcondition(dnStart, "Z-trend DOWN", "Z-score entered downtrend regime")
alertcondition(regime == 0 and regime[1] != 0, "Z-trend NEUTRAL", "Z-score regime exited to neutral")