mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-13 16:18:05 +00:00
- Updated BBWN, BBWP, CCV, CV, CVI, EWMA, GKV, HLV, HV, Jvolty, JVOLTYN, MASSI, NATR, RSV, RV, RVI, TR, UI, VOV, VR, YZV indicators with documentation links. - Added documentation links for Aberration, Acceleration Bands, Andrews' Pitchfork, Adaptive Price Zone, ATR Bands, Bollinger Bands, Center of Gravity, Donchian Channels, Decay Min-Max Channel, Detrended Synthetic Price, EACP, EBSW, HOMOD, Jurik Volatility Bands, Keltner Channel, MA Envelope, Min-Max Channel, Price Channel, Regression Channels, Standard Deviation Channel, Stoller Average Range Channel, Super Trend Bands, Ultimate Bands, Ultimate Channel, VWAP Bands, and VWAP with Standard Deviation Bands.
168 lines
6.1 KiB
Plaintext
168 lines
6.1 KiB
Plaintext
// The MIT License (MIT)
|
|
// © mihakralj
|
|
//@version=6
|
|
indicator("Jurik Moving Average", "JMA", overlay=true)
|
|
|
|
//@function Spectrally correct JMA (decompiled-style, Kositsin/AmiBroker port)
|
|
//@param source Series to calculate JMA from
|
|
//@param period Number of bars used in the calculation (>= 1)
|
|
//@param phase Phase shift (-100 to 100). Negative = smoother, positive = more leading
|
|
//@returns JMA value with Jurik-style spectral volatility adaptation
|
|
jma(series float source, simple int period, simple int phase = 0) =>
|
|
// ---- Precomputed length/phase parameters (constant per series) ----
|
|
simple float _PHASE = phase < -100 ? 0.5 : phase > 100 ? 2.5 : (phase * 0.01) + 1.5
|
|
simple float _LEN0 = period < 1.0000000002 ? 1e-10 : (period - 1.0) / 2.0
|
|
simple float _LOG_PARAM = math.max(math.log(math.sqrt(_LEN0)) / math.log(2.0) + 2.0, 0.0)
|
|
simple float _SQRT_PARAM = math.sqrt(_LEN0) * _LOG_PARAM
|
|
simple float _LEN_ADJ = _LEN0 * 0.9
|
|
simple float _LEN_DIV = _LEN_ADJ / (_LEN_ADJ + 2.0)
|
|
simple float _SQRT_DIV = _SQRT_PARAM / (_SQRT_PARAM + 1.0)
|
|
simple float _P_EXP = math.max(_LOG_PARAM - 2.0, 0.5)
|
|
|
|
// ---- Internal state (persists across bars) ----
|
|
var float paramA = na
|
|
var float paramB = na
|
|
var float lastC0 = na
|
|
var float lastC8 = na
|
|
var float lastA8 = na
|
|
var float lastJma = na
|
|
var int bars = 0
|
|
|
|
// 10-bar local deviation window
|
|
var float cycleDelta = 0.0
|
|
var int volIndex = 0
|
|
var int volCount = 0
|
|
var array<float> volWindow = array.new_float(10, 0.0)
|
|
|
|
// 128-bar volatility distribution
|
|
var int distIndex = 0
|
|
var int distCount = 0
|
|
var array<float> distWindow = array.new_float(128, 0.0)
|
|
var array<float> sorted = array.new_float(0)
|
|
|
|
float current_jma = na
|
|
|
|
if not na(source)
|
|
bars += 1
|
|
|
|
// ---- First bar: initialize anchors and filter state ----
|
|
if bars == 1
|
|
paramA := source
|
|
paramB := source
|
|
lastC0 := source
|
|
lastC8 := 0.0
|
|
lastA8 := 0.0
|
|
lastJma := source
|
|
current_jma := source
|
|
else
|
|
// 1) Local deviation vs. ParamA / ParamB
|
|
float diffA = source - paramA
|
|
float diffB = source - paramB
|
|
float absA = math.abs(diffA)
|
|
float absB = math.abs(diffB)
|
|
float absValue = absA > absB ? absA : absB
|
|
float dLocal = absValue + 1e-10
|
|
|
|
// 2) 10-bar SMA of local deviation -> highD
|
|
float oldVol = array.get(volWindow, volIndex)
|
|
cycleDelta += dLocal - oldVol
|
|
array.set(volWindow, volIndex, dLocal)
|
|
volIndex += 1
|
|
if volIndex >= 10
|
|
volIndex := 0
|
|
if volCount < 10
|
|
volCount += 1
|
|
float highD = volCount > 0 ? cycleDelta / (volCount < 10 ? volCount : 10) : dLocal
|
|
|
|
// 3) 128-bar volatility distribution + trimmed mean
|
|
array.set(distWindow, distIndex, highD)
|
|
distIndex += 1
|
|
if distIndex >= 128
|
|
distIndex := 0
|
|
if distCount < 128
|
|
distCount += 1
|
|
|
|
float dRef = highD
|
|
if distCount >= 16
|
|
int count = distCount
|
|
array.clear(sorted)
|
|
for i = 0 to count - 1
|
|
int idx = distIndex - 1 - i
|
|
if idx < 0
|
|
idx += 128
|
|
array.push(sorted, array.get(distWindow, idx))
|
|
array.sort(sorted)
|
|
|
|
int idxLo = 0
|
|
int idxHi = 0
|
|
if count >= 128
|
|
idxLo := 32
|
|
idxHi := 96
|
|
else
|
|
int slice = int(math.max(5.0, math.round(count * 0.5)))
|
|
int drop = (count - slice) / 2
|
|
idxLo := drop
|
|
idxHi := drop + slice - 1
|
|
|
|
if idxLo < 0
|
|
idxLo := 0
|
|
if idxHi >= count
|
|
idxHi := count - 1
|
|
|
|
float sum = 0.0
|
|
for i = idxLo to idxHi
|
|
sum += array.get(sorted, i)
|
|
dRef := sum / float(idxHi - idxLo + 1)
|
|
|
|
if dRef <= 0.0
|
|
dRef := dLocal
|
|
|
|
// 4) Jurik dynamic exponent
|
|
float ratio = absValue / dRef
|
|
if ratio < 0.0
|
|
ratio := 0.0
|
|
float d = math.pow(ratio, _P_EXP)
|
|
d := math.min(math.max(d, 1.0), _LOG_PARAM)
|
|
|
|
// 5) Update ParamA / ParamB via sqrtDivider ^ sqrt(d)
|
|
float adapt = math.pow(_SQRT_DIV, math.sqrt(d))
|
|
if source > paramA
|
|
paramA := source
|
|
else
|
|
paramA := source - (source - paramA) * adapt
|
|
if source < paramB
|
|
paramB := source
|
|
else
|
|
paramB := source - (source - paramB) * adapt
|
|
|
|
// 6) 2-pole IIR core (C0/C8/A8) with Jurik alpha
|
|
float prevJma = na(lastJma) ? source : lastJma
|
|
float alpha = math.pow(_LEN_DIV, d)
|
|
float alpha2 = alpha * alpha
|
|
float c0 = (1.0 - alpha) * source + alpha * lastC0
|
|
float c8 = (source - c0) * (1.0 - _LEN_DIV) + _LEN_DIV * lastC8
|
|
float a8 = (_PHASE * c8 + c0 - prevJma) * (alpha * -2.0 + alpha2 + 1.0) + alpha2 * lastA8
|
|
float jmaVal = prevJma + a8
|
|
|
|
lastC0 := c0
|
|
lastC8 := c8
|
|
lastA8 := a8
|
|
lastJma := jmaVal
|
|
current_jma := jmaVal
|
|
|
|
current_jma
|
|
|
|
|
|
// ---------- Main loop ----------
|
|
|
|
// Inputs
|
|
i_period = input.int(10, "Period", minval=1, tooltip="Number of bars used in the calculation")
|
|
i_phase = input.int(0, "Phase", tooltip="Phase shift (-100 to 100). Negative values reduce lag but may cause overshoot", minval=-100, maxval=100, step=10)
|
|
i_source = input.source(close, "Source")
|
|
|
|
// Calculation
|
|
jma_value = jma(i_source, i_period, i_phase)
|
|
|
|
// Plot
|
|
plot(jma_value, "JMA-T", color=color.yellow, linewidth=2)
|