# all files / technicalindicators/lib/moving_averages/ MACD.js

93.88% Statements 46/49
75% Branches 15/20
83.33% Functions 5/6
93.48% Lines 43/46

    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

    1×
    1×
    1×

    1×

    1×

    1×

    3×

    3×

    3×

    42×

    33×

    27×

    3×

    3×

    3×
    3×
    3×
    3×
    3×
    57×
    24×
    24×
    24×

    33×
    33×
    33×
    33×

    30×
    30×

    3×

    3×
    36×
    36×
    22×

    1×
    1×

    1×
    1×
    1×

    1×
    1×

    1×
    18×

     /**
     * Created by AAravindan on 5/4/16.
     */
    "use strict"
    const SMA        = require('./SMA');
    const EMA        = require('./EMA');
    const nf = require('./../Utils/NumberFormatter');

    let MACD;

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

    module.exports = MACD = function(input) {

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

      var oscillatorMAtype = input.SimpleMAOscillator ? SMA : EMA;

      var signalMAtype   = input.SimpleMASignal ? SMA : EMA;

      var fastMAProducer = new oscillatorMAtype({period : input.fastPeriod, values : [], format : (v) => {return v}});

      var slowMAProducer = new oscillatorMAtype({period : input.slowPeriod, values : [], format : (v) => {return v}});

      var signalMAProducer = new signalMAtype({period : input.signalPeriod, values : [], format : (v) => {return v}});

      var format = input.format || nf;

      this.result = [];

      this.generator = (function* (){
        var index = 0;
        var tick;
        var MACD, signal, histogram, fast, slow;
        while (true) {
          if(index < input.slowPeriod){
            tick = yield;
            fast = fastMAProducer.nextValue(tick);
            slow = slowMAProducer.nextValue(tick);
            index++;
            continue;
          }
          MACD = fast - slow;
          signal = signalMAProducer.nextValue(MACD);
          histogram = MACD - signal;
          tick = yield({
            //fast : fast,
            //slow : slow,
            MACD : format(MACD),
            signal : signal ? format(signal) : undefined,
            histogram : isNaN(histogram) ? undefined : format(histogram)
          })
          fast = fastMAProducer.nextValue(tick);
          slow = slowMAProducer.nextValue(tick);
        }
      })();

      this.generator.next();

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

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

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

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