Files
QuanTAlib/lib/trends_IIR/zltema/zltema.pine
T
Miha Kralj c034cbd5e5 Add Yang-Zhang Volatility (YZV) Indicator Implementation
- Introduced YZV class for calculating Yang-Zhang Volatility, a comprehensive volatility measure that incorporates overnight, open-to-close, and high-low components.
- Implemented calculation methods, including batch processing for TBarSeries and spans.
- Added documentation for YZV, detailing its mathematical foundation, performance profile, and trading applications.
- Updated volume index documentation to reflect changes in file paths.
- Refactored VWMA calculation method to use a more generic source parameter instead of price.
2026-02-02 19:47:21 -08:00

72 lines
2.5 KiB
Plaintext

// The MIT License (MIT)
// © mihakralj
//@version=6
indicator("Zero-Lag Triple EMA (ZLTEMA)", "ZLTEMA", overlay=true)
//@function Calculates ZLTEMA using zero-lag price and triple exponential smoothing with compensator
//@doc https://github.com/mihakralj/pinescript/blob/main/indicators/trends_IIR/zltema.md
//@param source Series to calculate ZLTEMA from
//@param period Smoothing period
//@param alpha Optional smoothing factor (overrides period if provided)
//@returns ZLTEMA value with zero-lag effect applied
//@optimized Uses lag compensation buffer and exponential warmup compensator on all three EMA stages for O(1) complexity
zltema(series float source, simple int period=0, simple float alpha=0) =>
if alpha <= 0 and period <= 0
runtime.error("Alpha or period must be provided")
float a1 = alpha > 0 ? alpha : 2.0 / (period + 1)
float beta1 = 1.0 - a1
float r = math.pow(1.0 / a1, 1.0 / 3.0)
float a2 = a1 * r
float a3 = a2 * r
simple int lag = math.max(1, math.round((period - 1) / 2))
var bool warmup = true
var float e = 1.0
var float ema1_raw = 0.0
var float ema2_raw = 0.0
var float ema3_raw = 0.0
var float ema1 = na
var float ema2 = na
var float ema3 = na
var priceBuffer = array.new<float>(lag + 1, na)
if not na(source)
if na(ema1)
ema1 := source
ema2 := source
ema3 := source
array.fill(priceBuffer, source)
array.shift(priceBuffer)
array.push(priceBuffer, source)
float laggedPrice = nz(array.get(priceBuffer, 0), source)
float signal = 2 * source - laggedPrice
ema1_raw := a1 * (signal - ema1_raw) + ema1_raw
if warmup
e *= beta1
float c = 1.0 / (1.0 - e)
ema1 := c * ema1_raw
ema2_raw := a2 * (ema1 - ema2_raw) + ema2_raw
ema2 := c * ema2_raw
ema3_raw := a3 * (ema2 - ema3_raw) + ema3_raw
ema3 := c * ema3_raw
warmup := e > 1e-10
else
ema1 := ema1_raw
ema2_raw := a2 * (ema1 - ema2_raw) + ema2_raw
ema2 := ema2_raw
ema3_raw := a3 * (ema2 - ema3_raw) + ema3_raw
ema3 := ema3_raw
3 * ema1 - 3 * ema2 + ema3
else
na
// ---------- Main loop ----------
// Inputs
i_period = input.int(10, "Period", minval=1)
i_source = input.source(close, "Source")
// Calculation
zltema_value = zltema(i_source, i_period)
// Plot
plot(zltema_value, "ZLTEMA", color=color.yellow, linewidth=2)