# all files / technicalindicators/lib/momentum/ ROC.js

100% Statements 34/34
100% Branches 12/12
100% Functions 5/5
100% Lines 34/34

    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
    59
    60
    61
    62
    63
    64
    65

    1×
    1×

    1×

    1×

    25×
    25×

    25×

    25×

    25×
    25×
    25×
    25×
    25×
    25×
    948×
    948×

    609×

    948×

    25×

    25×
    80×
    80×
    32×

    1×
    2×
    2×
    2×
    2×

    1×
    2×

    1×
    868×
    868×
    552×

     /**
     * Created by AAravindan on 5/9/16.
     */
    /**
     * Created by AAravindan on 5/7/16.
     */
    "use strict"

    const LinkedList = require('../Utils/FixedSizeLinkedList');
    const nf = require('../Utils/NumberFormatter');

    let ROC;

    module.exports = ROC = function(input) {

      var period = input.period
      var priceArray = input.values;

      this.format = input.format || nf;

      this.result = [];

      this.generator = (function* (){
        let index = 1;
        var pastPeriods = new LinkedList(period);;
        var tick = yield;
        var roc;
        while (true) {
          pastPeriods.push(tick)
          if(index < period){
            index++;
          }else {
            roc = ((tick - pastPeriods.lastShift) / (pastPeriods.lastShift)) * 100
          }
          tick = yield roc;
        }
      })();

      this.generator.next();

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

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

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

    ROC.prototype.nextValue = function (price) {
      var nextResult = this.generator.next(price);
      if(nextResult.value)
        return this.format(nextResult.value);
    };