Files
QuanTAlib/lib/trends_FIR/lsma/lsma.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

61 lines
2.0 KiB
Plaintext

// The MIT License (MIT)
// © mihakralj
//@version=6
indicator("Least Squares Moving Average (LSMA)", "LSMA", overlay=true)
//@function Calculates LSMA by fitting a linear regression line to price data
//@param source Series to calculate LSMA from
//@param period Lookback period for the linear regression
//@returns LSMA value, calculates from first bar using available data
//@optimized Uses circular buffer with linear regression for O(n) complexity per bar
lsma(series float source, simple int period) =>
if period <= 1
runtime.error("Period must be greater than 1")
source
else
int p = math.min(bar_index + 1, period)
if p <= 1
source
else
var array<float> buffer = array.new_float(period, na)
var int head = 0
array.set(buffer, head, source)
head := (head + 1) % period
float sum_y = 0.0
float sum_xy = 0.0
float sum_x = 0.0
float sum_x2 = 0.0
float count = 0.0
int idx = (head - 1 + period) % period
for i = 0 to p - 1
float val = array.get(buffer, idx)
if not na(val)
sum_x += i
sum_y += val
sum_xy += i * val
sum_x2 += i * i
count += 1.0
idx := (idx - 1 + period) % period
if count <= 1.0
source
else
float denom = count * sum_x2 - sum_x * sum_x
if denom == 0.0
source
else
float slope = (count * sum_xy - sum_x * sum_y) / denom
float intercept = (sum_y - slope * sum_x) / count
intercept
// ---------- Main loop ----------
// Inputs
i_period = input.int(10, "Period", minval=1)
i_source = input.source(close, "Source")
// Calculation
lsma_value = lsma(i_source, i_period)
// Plot
plot(lsma_value, "LSMA", color=color.yellow, linewidth=2)