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

100% Statements 41/41
100% Branches 18/18
100% Functions 5/5
100% Lines 37/37

    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

    1×
    1×
    1×

    1×

    1×

    4×
    4×
    4×

    184×
    116×
    48×
    44×

    4×

    4×
    4×
    4×
    252×
    252×
    252×
    252×
    252×

    4×

    4×
    189×
    189×
    33×

    1×
    2×
    2×
    2×
    2×

    1×
    1×

    1×
    63×
    63×
    11×

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

    const ROC = require('./ROC.js');
    const EMA = require('../moving_averages/EMA.js');
    const nf  = require('../Utils/NumberFormatter');

    let TRIX;

    module.exports = TRIX = function(input) {

      let priceArray  = input.values;
      let period      = input.period;
      let format = input.format || nf;

      let ema              = new EMA({ period : period, values : [], format : (v) => {return v}});
      let emaOfema         = new EMA({ period : period, values : [], format : (v) => {return v}});
      let emaOfemaOfema    = new EMA({ period : period, values : [], format : (v) => {return v}});
      let trixROC          = new ROC({ period : 1, values : [], format : (v) => {return v}});

      this.result = [];

      this.generator = (function* (){
        let tick = yield;
        while (true) {
          let initialema           = ema.nextValue(tick);
          let smoothedResult       = initialema ? emaOfema.nextValue(initialema) : undefined;
          let doubleSmoothedResult = smoothedResult ? emaOfemaOfema.nextValue(smoothedResult) : undefined;
          let result               = doubleSmoothedResult ? trixROC.nextValue(doubleSmoothedResult) : undefined;
          tick = yield result ? format(result) : undefined;
        }
      })();

      this.generator.next();

      priceArray.forEach((tick) => {
        let result = this.generator.next(tick);
        if(result.value !== undefined){
          this.result.push(result.value);
        }
      });
    };

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

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

    TRIX.prototype.nextValue = function (price) {
      let nextResult = this.generator.next(price);
      if(nextResult.value !== undefined)
        return nextResult.value;
    };