本页源码来自 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/
// @version=6
indicator("Trend-Quality Indicator [Sepiashvili 2004]", "TQI", overlay = false)
// Inputs
int fastLength = input.int(7, "Fast EMA", minval = 1)
int slowLength = input.int(15, "Slow EMA", minval = 1)
int trendLength = input.int(4, "Trend Length (m)", minval = 1)
int noiseLength = input.int(250, "Noise Length (n)", minval = 1)
float correction = input.float(2.0, "Correction Factor (c)", minval = 0.0)
// Original method requires n > m.
if (noiseLength <= trendLength)
runtime.error("Noise Length (n) must be greater than Trend Length (m).")
// 1. Define trend semicycles using EMA crossovers.
float fastEMA = ta.ema(close, fastLength)
float slowEMA = ta.ema(close, slowLength)
bool reversal = ta.cross(fastEMA, slowEMA)
// 2. Cumulative Price Change (CPC).
float priceChange = close - close[1]
var bool started = false
var float cpc = na
if (reversal)
started := true
cpc := 0.0
else if (started)
cpc := nz(cpc[1]) + priceChange
// 3. Piecewise EMA of CPC.
// Reset the EMA whenever a new semicycle begins.
float alpha = 2.0 / (trendLength + 1.0)
var float trend = na
if (reversal)
trend := 0.0
else if (started)
trend := alpha * cpc + (1.0 - alpha) * nz(trend[1])
// 4. RMS noise.
float deviation = cpc - trend
float noise = correction * math.sqrt(ta.sma(deviation * deviation, noiseLength))
// 5. Trend-Quality Indicator.
float q = noise > 0.0 ? trend / noise : na
plot(q, "Trend Quality", style = plot.style_columns, color = q >= 0 ? color.green : color.red)
plot(trend, "CPC MA", color=color.white, display=display.none)
plot(noise, "Noise", color=color.gray, display=display.none)
hline(0, "Zero", color.gray, linestyle = hline.style_solid)
hline(1, "+1", color.gray, display=display.none)
hline(-1, "-1", color.gray, display=display.none)
hline(2, "+2", color.green)
hline(-2, "-2", color.red)
hline(5, "+5", color.rgb(0, 255, 0, 0), linestyle = hline.style_solid)
hline(-5, "-5", color.rgb(255, 0, 0), linestyle = hline.style_solid)