feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
// Licensed under the Apache License, Version 2.0
|
2026-01-18 19:02:03 -08:00
|
|
|
// © mihakralj
|
|
|
|
|
//@version=6
|
|
|
|
|
indicator("Bollinger Band Width (BBW)", "BBW", overlay=false)
|
|
|
|
|
|
|
|
|
|
//@function Calculates Bollinger Band Width as the difference between upper and lower bands
|
|
|
|
|
//@param source Series to calculate Bollinger Bands from
|
|
|
|
|
//@param period Lookback period for calculations
|
|
|
|
|
//@param multiplier Standard deviation multiplier for band width
|
|
|
|
|
//@returns BBW value representing the width between Bollinger Bands
|
|
|
|
|
//@optimized for performance and dirty data
|
|
|
|
|
bbw(series float source, simple int period, simple float multiplier) =>
|
|
|
|
|
var int p = math.max(1, period), var int head = 0, var int count = 0
|
|
|
|
|
var array<float> buffer = array.new_float(p, na)
|
|
|
|
|
var float sum = 0.0, var float sumSq = 0.0
|
|
|
|
|
float oldest = array.get(buffer, head)
|
|
|
|
|
if not na(oldest)
|
|
|
|
|
sum -= oldest
|
|
|
|
|
sumSq -= oldest * oldest
|
|
|
|
|
count -= 1
|
|
|
|
|
float current_val = nz(source)
|
|
|
|
|
sum += current_val
|
|
|
|
|
sumSq += current_val * current_val
|
|
|
|
|
count += 1
|
|
|
|
|
array.set(buffer, head, current_val)
|
|
|
|
|
head := (head + 1) % p
|
|
|
|
|
float basis = nz(sum / count, source)
|
|
|
|
|
float dev = count > 1 ? multiplier * math.sqrt(math.max(0.0, sumSq / count - basis * basis)) : 0.0
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
basis != 0.0 ? 2 * dev / basis : 0.0
|
2026-01-18 19:02:03 -08:00
|
|
|
|
|
|
|
|
// ---------- Main loop ----------
|
|
|
|
|
|
|
|
|
|
// Inputs
|
|
|
|
|
i_period = input.int(20, "Period", minval=1)
|
|
|
|
|
i_source = input.source(close, "Source")
|
|
|
|
|
i_multiplier = input.float(2.0, "StdDev Multiplier", minval=0.001)
|
|
|
|
|
|
|
|
|
|
// Calculation
|
|
|
|
|
bbw_value = bbw(i_source, i_period, i_multiplier)
|
|
|
|
|
|
|
|
|
|
// Plot
|
|
|
|
|
plot(bbw_value, "BBW", color=color.yellow, linewidth=2)
|