Files

36 lines
1.2 KiB
Plaintext
Raw Permalink Normal View History

// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
indicator("Parabolic Weighted Moving Average (PWMA)", "PWMA", overlay=true)
//@function Calculates PWMA using i² (parabolic) weights with compensator
//@param source Series to calculate PWMA from
//@param period Lookback period - FIR window size
//@returns PWMA value, calculates from first bar using available data
//@optimized Uses parabolic weighting w[i]=i² with O(n) complexity per bar due to lookback loop
pwma(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)
float psum = 0.0
float weight_sum = 0.0
for i = 0 to p - 1
float price = source[i]
if not na(price)
float w = float((p - i) * (p - i))
psum += price * w
weight_sum += w
nz(psum / weight_sum, source)
// ---------- Main loop ----------
// Inputs
i_period = input.int(10, "Period", minval=1)
i_source = input.source(close, "Source")
// Calculation
pwma_value = pwma(i_source, i_period)
// Plot
plot(pwma_value, "PWMA", color=color.yellow, linewidth=2)