Files
QuanTAlib/lib/trends_FIR/sma/sma.pine
T
Miha Kralj 24e86d762a Add documentation links for various volatility indicators and channels
- 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.
2026-02-18 11:55:48 -08:00

41 lines
1.2 KiB
Plaintext

// The MIT License (MIT)
// © mihakralj
//@version=6
indicator("Simple Moving Average (SMA)", "SMA", overlay=true)
//@function Calculates SMA using simple smoothing with compensator
//@param source Series to calculate SMA from
//@param period Lookback period - FIR window size
//@returns SMA value, calculates from first bar using available data
//@optimized Uses circular buffer and running sum for O(1) complexity
sma(series float source, simple int period) =>
if period <= 0
runtime.error("Period must be greater than 0")
int p = period
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
else
count += 1
float current = nz(source)
sum += current
array.set(buffer, head, current)
head := (head + 1) % p
sum / math.max(1, count)
// ---------- Main loop ----------
// Inputs
i_period = input.int(10, "Period", minval=1)
i_source = input.source(close, "Source")
// Calculation
sma_value = sma(i_source, i_period)
// Plot
plot(sma_value, "SMA", color=color.yellow, linewidth=2)