mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-08 22:17:44 +00:00
24e86d762a
- 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.
44 lines
1.4 KiB
Plaintext
44 lines
1.4 KiB
Plaintext
// The MIT License (MIT)
|
|
// © mihakralj
|
|
//@version=6
|
|
indicator("Triangular Moving Average (TRIMA)", "TRIMA", overlay=true)
|
|
|
|
//@function Calculates TRIMA using triangular weighted smoothing with compensator
|
|
//@param source Series to calculate TRIMA from
|
|
//@param period Lookback period - FIR window size
|
|
//@returns TRIMA value, calculates from first bar using available data
|
|
//@optimized Uses triangular weighting with O(n) complexity per bar due to lookback loop
|
|
trima(series float source, simple int period) =>
|
|
if period <= 0
|
|
runtime.error("Period must be greater than 0")
|
|
int p = math.min(bar_index + 1, period)
|
|
var array<float> weights = array.new_float(1, 1.0)
|
|
var int last_p = 1
|
|
if last_p != p
|
|
weights := array.new_float(p, 0.0)
|
|
int mid = math.floor(p / 2)
|
|
for i = 0 to p - 1
|
|
array.set(weights, i, math.min(i, p - 1 - i) + 1)
|
|
last_p := p
|
|
float sum = 0.0
|
|
float weight_sum = 0.0
|
|
for i = 0 to p - 1
|
|
float price = source[i]
|
|
if not na(price)
|
|
float w = array.get(weights, i)
|
|
sum += price * w
|
|
weight_sum += w
|
|
nz(sum / weight_sum, source)
|
|
|
|
// ---------- Main loop ----------
|
|
|
|
// Inputs
|
|
i_period = input.int(10, "Period", minval=1)
|
|
i_source = input.source(close, "Source")
|
|
|
|
// Calculation
|
|
trima_value = trima(i_source, i_period)
|
|
|
|
// Plot
|
|
plot(trima_value, "TRIMA", color=color.yellow, linewidth=2)
|