Daily Reset CWEMA/CWTEMA
// 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("Daily Reset CWEMA", overlay = true)

source = input.source(close, 'Source')
length = input.int(20, "Length", 1)
style = input.string("CWTEMA", "Style", ["CWEMA","CWTEMA"])

newday = timeframe.change("D")
bs_nd = ta.barssince(newday) + 1
v_len = (timeframe.isdaily or timeframe.isweekly or timeframe.ismonthly) ? length : bs_nd < length ? bs_nd : length

ema(src) => //{
    alpha = 2 / (bs_nd + 1)
    sum   = 0.0
    sum  := na(sum[1]) ? src : alpha * src + (1 - alpha) * nz(sum[1])

// Define the triple exponential moving average function
tema(src) =>
    // Compute the weights for each value in the moving average
    weights = (v_len + 1) / 2 - math.abs(v_len - src)
    // Calculate the triple exponential moving average
    tema = 3 * ema(src) - 3 * ema(ema(src)) + ema(ema(ema(src)))

// Define the weighted moving average function
wma(src, weights) =>
    // Compute the weighted sum of the source data
    weightedSum = math.sum(src * weights, v_len)
    // Compute the sum of the weights
    sumWeights = math.sum(weights, v_len)
    // Divide the weighted sum by the sum of the weights to calculate the weighted average
    weightedSum / sumWeights

cwema(src) =>
    // Compute the weights for each value in the moving average
    weights = (v_len + 1) / 2 - math.abs(v_len - src)
    // Calculate the weighted moving average
    wma(ema(src), weights)

cwtema(src) =>
    // Compute the weights for each value in the moving average
    weights = (v_len + 1) / 2 - math.abs(v_len - src)
    // Calculate the weighted moving average
    wma(tema(src), weights)

ma(src, len, style)=>
    switch style
        "CWEMA" => cwema(src)
        "CWTEMA" => cwtema(src)

plot(ma(source, length, style))
