# all files / technicalindicators/lib/Utils/ AverageGain.jic-1.js

0% Statements 0/33
0% Branches 0/6
0% Functions 0/5
0% Lines 0/32

    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

     /**
     * Created by AAravindan on 5/5/16.
     */
    "use strict"
    const SMA = require('../moving_averages/SMA');
    let AvgGain;

    module.exports = AvgGain = function(input) {

      let values = input.values;
      let period = input.period;
      var gainMA = new SMA({ period : period, values : [], format : (v) => {return v}})
      this.generator = (function* (period){
        var currentValue = yield;
        var lastValue;
        var gain;
        while(true){
          if(!lastValue){
            lastValue = currentValue;
            currentValue = yield;
          }
          gain = (currentValue - lastValue) >= 0 ? (currentValue - lastValue) : 0;
          console.log('input of maGain', gain);
          var maGain = gainMA.nextValue(gain);
          console.log('Output of maGain', maGain);
          lastValue = currentValue;
          currentValue = yield maGain
        }
      })(period);

      this.generator.next();

      this.result = [];

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

    AvgGain.calculate = function(period, price, options) {
      return (new AvgGain(period, price, options)).result;
    };

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

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