Dominant Period-Based Moving Average (DPBMA)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © peacefulLizard50262

//@version=5
indicator("My script", overlay = true)
import lastguru/DominantCycle/2 as d

ema(float source = close, float length = 9)=>
    alpha = 2 / (length + 1)
    var float smoothed = na
    smoothed := alpha * source + (1 - alpha) * nz(smoothed[1])

source = input.source(close, "Source")
harmonic = input.int(1, "Length", 1)

length(source, harmonic)=>
    cycle = math.round(d.mamaPeriod(source, 1, 2048))
    var cycles = array.new<float>(1)
    var count_cycles = array.new<int>(1)
    if not array.includes(cycles, cycle)
        array.push(cycles, cycle)
        array.push(count_cycles, 1)

    else
        index = array.indexof(cycles, cycle)
        array.set(count_cycles, index, array.get(count_cycles, index) + 1)

    max_index = array.indexof(count_cycles, array.max(count_cycles))
    max_cycle = array.get(cycles, max_index) * harmonic

length = length(source, harmonic)
aema = ema(source, length)

plot(aema)