mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-13 08:08:05 +00:00
- Updated the name and description of the Hilbert Trendline (HTIT) to "Ehlers Hilbert Transform Instantaneous Trend (HTIT)". - Changed the name and description of the MESA Adaptive Moving Average (MAMA) to "Ehlers MESA Adaptive Moving Average". - Modified the Center of Gravity (CG) indicator to "Ehlers Center of Gravity (CG)". - Renamed the Detrended Synthetic Price (DSP) to "Ehlers Detrended Synthetic Price (DSP)". - Updated the Autocorrelation Periodogram (EACP) to "Ehlers Autocorrelation Periodogram (EACP)". - Changed the Homodyne Discriminator (HOMOD) to "Ehlers Homodyne Discriminator (HOMOD)". - Updated the Hilbert Transform Dominant Cycle Period and Phase indicators to include "Ehlers" in their names. - Renamed the Hilbert Transform Phasor Components to "Ehlers Hilbert Transform Phasor Components (HT_PHASOR)". - Updated the SineWave indicator to "Ehlers Hilbert Transform SineWave (HT_SINE)". - Changed the Phasor Analysis indicator to "Ehlers Hilbert Transform Phasor Components (HT_PHASOR)". - Updated the SSF-Based Detrended Synthetic Price to "Ehlers SSF Detrended Synthetic Price (SSFDSP)". - Renamed the Ultimate Channel to "Ehlers Ultimate Channel (UCHANNEL)". - Added new indicators: Moving Average Variable Period (MAVP), Ehlers Predictive Moving Average (PMA), Ehlers Reverse EMA (REVERSEEMA), and Ehlers Trendflex Indicator (TRENDFLEX). - Updated various SVG badges to reflect changes in classes, comments, source files, lines of code, methods, and public types.
52 lines
1.9 KiB
Plaintext
52 lines
1.9 KiB
Plaintext
// The MIT License (MIT)
|
|
// © mihakralj
|
|
//@version=6
|
|
indicator("Moving Average Variable Period (MAVP)", "MAVP", overlay=true)
|
|
|
|
//@function Calculates EMA with per-bar variable period (TA-Lib MAVP concept)
|
|
//@param source Series to smooth
|
|
//@param period Per-bar effective period (clamped to min_period..max_period)
|
|
//@param min_period Minimum allowed period
|
|
//@param max_period Maximum allowed period
|
|
//@returns EMA value with variable alpha = 2/(period+1), compensated warmup
|
|
//@optimized Uses adaptive warmup compensator that tracks cumulative (1-alpha) product for O(1) per bar
|
|
mavp(series float source, series float period, simple int min_period, simple int max_period) =>
|
|
if min_period < 1
|
|
runtime.error("min_period must be >= 1")
|
|
if max_period < min_period
|
|
runtime.error("max_period must be >= min_period")
|
|
var float ema = 0.0
|
|
var float e = 1.0
|
|
var bool warmup = true
|
|
var float result = source
|
|
float p = math.max(min_period, math.min(max_period, nz(period, min_period)))
|
|
float a = 2.0 / (p + 1.0)
|
|
float beta = 1.0 - a
|
|
ema := a * (nz(source) - ema) + ema
|
|
if warmup
|
|
e *= beta
|
|
float c = 1.0 / (1.0 - e)
|
|
result := c * ema
|
|
warmup := e > 1e-10
|
|
else
|
|
result := ema
|
|
result
|
|
|
|
// ---------- Main loop ----------
|
|
|
|
// Inputs
|
|
i_period = input.int(10, "Period", minval=1, tooltip="Base period for the variable-period EMA")
|
|
i_min = input.int(2, "Min Period", minval=1, tooltip="Minimum allowed period")
|
|
i_max = input.int(30, "Max Period", minval=2, tooltip="Maximum allowed period")
|
|
i_source = input.source(close, "Source")
|
|
|
|
// Per-bar period series: fixed here, replace with any series for adaptive behavior
|
|
// In the C# implementation, this is an external per-bar series input
|
|
float per_bar_period = float(i_period)
|
|
|
|
// Calculation
|
|
mavp_value = mavp(i_source, per_bar_period, i_min, i_max)
|
|
|
|
// Plot
|
|
plot(mavp_value, "MAVP", color=color.yellow, linewidth=2)
|