all files / technicalindicators/lib/volume/ OBV.js

100% Statements 37/37
85.71% Branches 12/14
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 63 64 65 66 67                    60× 32×   28× 28×   60× 60×         48×       48× 48× 45×                   16×    
/**
 * Created by AAravindan on 5/17/16.
 */
"use strict"
let OBV;
 
module.exports = OBV = function(input) {
 
  var closes      = input.close;
  var volumes     = input.volume;
 
  this.result = [];
 
  this.generator = (function* (){
    var result = 0;
    var tick;
    var lastClose;
    tick = yield;
    if(Etick.close && (typeof tick.close === 'number')){
      lastClose = tick.close;
      tick = yield;
    }
    while (true)
    {
      if(lastClose < tick.close ){
        result = result + tick.volume;
      }
      else Eif(tick.close < lastClose){
        result = result - tick.volume;
      }
      lastClose = tick.close;
      tick = yield result;
    }
  })();
 
  this.generator.next();
 
  closes.forEach((close, index) => {
    let tickInput = {
      close   : closes[index],
      volume  : volumes[index]
    }
    let result = this.generator.next(tickInput);
    if(result.value){
      this.result.push(result.value);
    }
  });
};
 
OBV.calculate = function(input) {
  if(input.reversedInput) {
    input.close.reverse();
    input.volume.reverse();
  }
  let result = (new OBV(input)).result;
  input.reversedInput ? result.reverse():undefined;
  return result;
};
 
OBV.prototype.getResult = function () {
  return this.result;
};
 
OBV.prototype.nextValue = function (price) {
  return this.generator.next(price).value;
};