mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-14 00:28: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.
48 lines
2.0 KiB
Plaintext
48 lines
2.0 KiB
Plaintext
// The MIT License (MIT)
|
|
// © mihakralj
|
|
//@version=6
|
|
indicator("Kaufman's Adaptive Moving Average (KAMA)", "KAMA", overlay=true)
|
|
|
|
//@function Calculates KAMA using adaptive smoothing based on market volatility
|
|
//@param source Series to calculate KAMA from
|
|
//@param period Length of the efficiency ratio lookback period
|
|
//@param fast_alpha Fastest EMA constant (2/(2+1))
|
|
//@param slow_alpha Slowest EMA constant (2/(30+1))
|
|
//@returns KAMA value with efficiency ratio-based adaptive smoothing
|
|
//@optimized Uses efficiency ratio calculation for O(n) complexity per bar due to lookback sum
|
|
kama(series float source, simple int period, simple float fast_alpha=0.666667, simple float slow_alpha=0.0645) =>
|
|
if period <= 0
|
|
runtime.error("Period must be greater than 0")
|
|
if fast_alpha <= 0 or slow_alpha <= 0
|
|
runtime.error("Alpha values must be greater than 0")
|
|
if fast_alpha <= slow_alpha
|
|
runtime.error("Fast alpha must be greater than slow alpha")
|
|
var float kama_state = na
|
|
float current_kama = na
|
|
if not na(source)
|
|
float change_val = math.abs(source - source[period])
|
|
float volatility_val = math.sum(math.abs(source - source[1]), period)
|
|
float er = 0.0
|
|
if not na(change_val) and not na(volatility_val) and volatility_val != 0.0
|
|
er := change_val / volatility_val
|
|
float sc = math.pow(er * (fast_alpha - slow_alpha) + slow_alpha, 2)
|
|
kama_state := na(kama_state) ? source : kama_state + sc * (source - kama_state)
|
|
current_kama := kama_state
|
|
current_kama
|
|
|
|
// ---------- Main loop ----------
|
|
|
|
// Inputs
|
|
i_period = input.int(10, "Period", minval=1)
|
|
i_source = input.source(close, "Source")
|
|
i_fast = input.int(2, "Fast EMA Period", minval=2)
|
|
i_slow = input.int(30, "Slow EMA Period", minval=2)
|
|
|
|
// Calculation
|
|
float fast_alpha = 2.0 / (float(i_fast) + 1.0)
|
|
float slow_alpha = 2.0 / (float(i_slow) + 1.0)
|
|
kama_value = kama(i_source, i_period, fast_alpha, slow_alpha)
|
|
|
|
// Plot
|
|
plot(kama_value, "KAMA", color=color.yellow, linewidth=2)
|