# all files / technicalindicators/lib/moving_averages/ WMA.js

97.37% Statements 37/38
83.33% Branches 10/12
100% Functions 5/5
97.37% Lines 37/38

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    1×
    1×
    1×

    1×
    3×
    3×
    3×
    3×
    3×
    3×
    3×
    3×

    3×
    57×
    27×

    30×
    30×
    30×
    270×

    30×
    27×
    27×

    3×

    3×
    36×
    36×
    20×

    1×
    1×

    1×
    1×
    1×

    1×
    1×

    1×
    18×
    18×
     "use strict"
    let WMA;
    const LinkedList = require('linkedlist');
    const nf = require('./../Utils/NumberFormatter');

    module.exports = WMA = function(input) {
      var period = input.period;
      var priceArray = input.values;
      var initial = undefined;
      this.format = input.format || nf;
      this.result = [];
      this.generator = (function* (initial){
        let data = new LinkedList();
        let denominator = period * (period + 1)/2;

        while (true) {
          if((data.length) < period ){
            data.push(yield)
          }else {
            data.resetCursor();
            let result = 0;
            for(let i=1; i<=period; i++){
              result = result + (data.next() * i/(denominator))
            }
            var next = yield result;
            data.shift();
            data.push(next);
          }
        }
      })(initial);

      this.generator.next();

      priceArray.forEach((tick, index) => {
        var result = this.generator.next(tick)
        if(result.value){
          this.result.push(this.format(result.value));
        }
      });
    };

    WMA.calculate = function(input) {
      if(Iinput.reversedInput) {
        input.values.reverse();
      }
      let result = (new WMA(input)).result;
      input.reversedInput ? result.reverse():undefined;
      return result;
    };

    WMA.prototype.getResult = function () {
      return this.result;
    };

    WMA.prototype.nextValue = function (price) {
      let result = this.generator.next(price).value;
      return result ? this.format(result) : undefined;
    };