Files
QuanTAlib/lib/trends_FIR/hanma/hanma.pine
T
86fe32a682 SIMD Refactor: Merge simd-dev into dev (#55)
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: aider (openrouter/anthropic/claude-sonnet-4) <aider@aider.chat>
Co-authored-by: Warp <agent@warp.dev>
2026-01-18 19:02:03 -08:00

44 lines
1.4 KiB
Plaintext

// The MIT License (MIT)
// © mihakralj
//@version=6
indicator("Hanning Moving Average (HANMA)", "HANMA", overlay=true)
//@function Calculates HANMA using Hanning window weighting
//@param source Series to calculate HANMA from
//@param period Lookback period - FIR window size
//@returns HANMA value, calculates from first bar using available data
//@optimized Uses Hanning window coefficients with O(n) complexity per bar due to lookback loop
hanma(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)
for i = 0 to p - 1
float w = 0.5 * (1.0 - math.cos(2.0 * math.pi * i / (p - 1)))
array.set(weights, i, w)
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
hanma_value = hanma(i_source, i_period)
// Plot
plot(hanma_value, "HANMA", color=color.yellow, linewidth=2)