mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-14 16:48:04 +00:00
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: aider (openrouter/anthropic/claude-sonnet-4) <aider@aider.chat> Co-authored-by: Warp <agent@warp.dev>
50 lines
1.7 KiB
Plaintext
50 lines
1.7 KiB
Plaintext
// The MIT License (MIT)
|
|
// © mihakralj
|
|
//@version=6
|
|
indicator("Zero-Lag EMA (ZLEMA)", "ZLEMA", overlay=true)
|
|
|
|
//@function Calculates ZLEMA using zero-lag price and exponential smoothing with compensator
|
|
//@doc https://github.com/mihakralj/pinescript/blob/main/indicators/trends_IIR/zlema.md
|
|
//@param source Series to calculate ZLEMA from
|
|
//@param period Smoothing period
|
|
//@param alpha Optional smoothing factor (overrides period if provided)
|
|
//@returns ZLEMA value with zero-lag effect applied
|
|
//@optimized Uses lag compensation buffer and exponential warmup compensator for O(1) complexity
|
|
zlema(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 a = alpha > 0 ? alpha : 2.0 / (period + 1)
|
|
float beta = 1.0 - a
|
|
simple int lag = math.max(1, math.round((period - 1) / 2))
|
|
var bool warmup = true
|
|
var float e = 1.0
|
|
var float zlema = 0.0
|
|
var float result = source
|
|
var priceBuffer = array.new<float>(lag + 1, 0.0)
|
|
if not na(source)
|
|
array.shift(priceBuffer)
|
|
array.push(priceBuffer, source)
|
|
float laggedPrice = array.get(priceBuffer, 0)
|
|
float signal = 2 * source - laggedPrice
|
|
zlema := a * (signal - zlema) + zlema
|
|
if warmup
|
|
e *= beta
|
|
float c = 1.0 / (1.0 - e)
|
|
result := c * zlema
|
|
warmup := e > 1e-10
|
|
else
|
|
result := zlema
|
|
result
|
|
|
|
// ---------- Main loop ----------
|
|
|
|
// Inputs
|
|
i_period = input.int(10, "Period", minval=1)
|
|
i_source = input.source(close, "Source")
|
|
|
|
// Calculation
|
|
zlema_value = zlema(i_source, i_period)
|
|
|
|
// Plot
|
|
plot(zlema_value, "ZLEMA", color=color.yellow, linewidth=2)
|