# all files / technicalindicators/lib/directionalmovement/ ATR.js

84.78% Statements 39/46
68.75% Branches 11/16
83.33% Functions 5/6
84.44% Lines 38/45

    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
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93

    1×
    1×
    1×

    1×

    1×

    1×

    3×

    3×
    3×
    3×
    3×
    3×

    3×

    3×

    51×

    3×

    3×
    3×
    3×
    3×
    90×

    90×

    90×

    90×

    3×

    3×
    60×

    60×
    34×

    1×
    1×

    1×
    1×
    1×

    1×
    1×

    1×
    30×

     /**
     * Created by AAravindan on 5/8/16.
     */
    "use strict"
    const WEMA        = require('../moving_averages/WEMA');
    const TrueRange  = require('./TrueRange');
    const nf = require('./../Utils/NumberFormatter');

    let ATR;

    let validateInput = function(input) {
      return input;
    };

    module.exports = ATR = function(input) {

      if(I!validateInput) {
        throw 'Invalid Input'
      };

      var lows = input.low;
      var highs = input.high;
      var closes = input.close;
      var period = input.period;
      var format = input.format || nf;

      if(I!((lows.length === highs.length) && (highs.length === closes.length) )){
        throw ('Inputs(low,high, close) not of equal size');
      }

      var trueRange = new TrueRange({
        low : [],
        high: [],
        close: []
      });

      var wema = new WEMA({period : period, values : [], format : (v) => {return v}});

      this.result = [];

      this.generator = (function* (){
        var tick = yield;
        var avgTrueRange,trange;;
        while (true) {
          trange = trueRange.nextValue({
            low : tick.low,
            high : tick.high,
            close : tick.close
          });
          if(Itrange === undefined){
            avgTrueRange = undefined;
          }else {
            avgTrueRange = wema.nextValue(trange);
          }
          tick = yield avgTrueRange
        }
      })();

      this.generator.next();

      lows.forEach((tick,index) => {
        var result = this.generator.next({
          high : highs[index],
          low  : lows[index],
          close : closes[index]
        });
        if(result.value !== undefined){
          this.result.push(format(result.value));
        }
      });
    };

    ATR.calculate = function(input) {
      if(Iinput.reversedInput) {
        input.high.reverse();
        input.low.reverse();
        input.close.reverse();
      }
      let result = (new ATR(input)).result;
      input.reversedInput ? result.reverse():undefined;
      return result;
    };

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

    ATR.prototype.nextValue = function (price) {
      return this.generator.next(price).value;
    };