all files / technicalindicators/lib/directionalmovement/ TrueRange.js

66.67% Statements 24/36
50% Branches 8/16
60% Functions 3/5
66.67% Lines 24/36
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                              90×         90× 90× 90×   90×                                                         90×    
/**
 * Created by AAravindan on 5/8/16.
 */
/**
 * Created by AAravindan on 5/8/16.
 */
"use strict"
const nf = require('../Utils/NumberFormatter');
 
let TrueRange;
 
module.exports = TrueRange = function(input) {
 
  var lows = input.low;
  var highs = input.high;
  var closes = input.close;
  var format = input.format || nf;
 
  if(Ilows.length != highs.length) {
    throw ('Inputs(low,high) not of equal size');
  }
 
  this.result = [];
 
  this.generator = (function* (){
    var current = yield;
    var previousClose,result;
    while (true) {
      result = Math.max(
          current.high - current.low,
          isNaN(Math.abs(current.high - previousClose)) ? 0 : Math.abs(current.high - previousClose),
          isNaN(Math.abs(current.low - previousClose)) ? 0 : Math.abs(current.low - previousClose)
      );
      previousClose = current.close;
      if(Eresult){
        result = format(result)
      }
      current = yield result;
    }
  })();
 
  this.generator.next();
 
  lows.forEach((tick, index) => {
    var result = this.generator.next({
      high : highs[index],
      low  : lows[index],
      close: closes[index]
    });
    if(result.value){
      this.result.push(result.value);
    }
  });
};
 
TrueRange.calculate = function(input) {
  if(input.reversedInput) {
    input.high.reverse();
    input.low.reverse();
    input.close.reverse();
  }
  let result = (new TrueRange(input)).result;
  input.reversedInput ? result.reverse():undefined;
  return result;
};
 
TrueRange.prototype.getResult = function () {
  return this.result;
};
 
TrueRange.prototype.nextValue = function (price) {
  return this.generator.next(price).value;
};