mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-17 01:58:06 +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.
46 lines
1.4 KiB
Plaintext
46 lines
1.4 KiB
Plaintext
// The MIT License (MIT)
|
|
// © mihakralj
|
|
//@version=6
|
|
indicator("Commodity Channel Index (CCI)", "CCI", overlay=false)
|
|
|
|
//@function Calculates Commodity Channel Index using circular buffer for efficiency
|
|
//@param length Lookback period for calculations
|
|
//@returns CCI value measuring price deviation from its moving average
|
|
cci(simple int length) =>
|
|
if length <= 0
|
|
runtime.error("Length must be greater than 0")
|
|
float tp = (high + low + close) / 3.0
|
|
var int p = math.max(1, length)
|
|
var array<float> buffer = array.new_float(p, na)
|
|
var int head = 0, var float sum = 0.0, var int count = 0
|
|
float oldest = array.get(buffer, head)
|
|
if not na(oldest)
|
|
sum -= oldest
|
|
count -= 1
|
|
if not na(tp)
|
|
sum += tp
|
|
count += 1
|
|
array.set(buffer, head, tp)
|
|
head := (head + 1) % p
|
|
float sma = count > 0 ? sum / count : tp
|
|
float dev_sum = 0.0
|
|
int dev_count = 0
|
|
for i = 0 to p - 1
|
|
float val = array.get(buffer, i)
|
|
if not na(val)
|
|
dev_sum += math.abs(val - sma)
|
|
dev_count += 1
|
|
float mean_dev = dev_count > 0 ? dev_sum / dev_count : 0.0
|
|
mean_dev > 0.0 ? (tp - sma) / (0.015 * mean_dev) : 0.0
|
|
|
|
// ---------- Main loop ----------
|
|
|
|
// Inputs
|
|
i_length = input.int(20, "Length", minval=1)
|
|
|
|
// Calculation
|
|
cci_value = cci(i_length)
|
|
|
|
// Plot
|
|
plot(cci_value, "CCI", color=color.yellow, linewidth=2)
|