index.js
function wma(close, n = 5) {
    let result = []
    let len = close.length
    if (len === 0 || n <= 0) {
        return result
    }
    let avg = close[0]
    result.push(avg)

    let T_m = close[0]
    let div = n * (n + 1) / 2
    for (let i = 1;  i < len; i++) {
        if (i < n) {
            T_m += close[i]
            let sum = 0
            let d = 0
            for (let j = 0; j <= i; j++) {
                sum += close[i - j] * (n - j)
                d += (n - j)
            }
            avg = sum / d
        } else {
            avg += (n * close[i] - T_m) / div
            T_m += close[i] - close[i - n]
        }
        result.push(avg)
    }
    return result
}
function wma(close, n = 5) {
    let result = []
    let len = close.length
    if (len === 0 || n <= 0) {
        return result
    }
    let avg = close[0]
    result.push(avg)

    let T_m = close[0]
    let div = n * (n + 1) / 2
    for (let i = 1;  i < len; i++) {
        if (i < n) {
            T_m += close[i]
            let sum = 0
            let d = 0
            for (let j = 0; j <= i; j++) {
                sum += close[i - j] * (n - j)
                d += (n - j)
            }
            avg = sum / d
        } else {
            avg += (n * close[i] - T_m) / div
            T_m += close[i] - close[i - n]
        }
        result.push(avg)
    }
    return result
}
function wma ( close , n = 5 ) { let result = [ ] let len = close . length if ( len === 0 || n <= 0 ) { return result } let avg = close [ 0 ] result . push ( avg ) let T_m = close [ 0 ] let div = n * ( n + 1 ) / 2 for ( let i = 1 ; i < len ; i ++ ) { if ( i < n ) { T_m += close [ i ] let sum = 0 let d = 0 for ( let j = 0 ; j <= i ; j ++ ) { sum += close [ i - j ] * ( n - j ) d += ( n - j ) } avg = sum / d } else { avg += ( n * close [ i ] - T_m ) / div T_m += close [ i ] - close [ i - n ] } result . push ( avg ) } return result }
Weighted moving average
Weighted moving average