# all files / technicalindicators/lib/momentum/ KST.js

100% Statements 67/67
100% Branches 26/26
100% Functions 5/5
100% Lines 62/62

    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
    94
    95
    96
    97

    1×
    1×
    1×

    1×

    1×

    4×
    4×
    4×
    4×
    4×

    4×
    4×
    4×
    4×

    4×

    4×
    4×
    4×
    4×

    124×
    104×
    84×
    24×
    16×

    4×

    4×

    4×
    4×
    4×
    4×
    4×
    4×
    4×
    200×
    200×
    200×
    200×
    200×
    200×
    200×
    200×
    200×

    24×

    200×
    200×

    200×

    4×

    4×
    150×
    150×
    18×

    1×
    2×
    2×
    2×
    2×

    1×
    1×

    1×
    50×
    50×
    6×

     /**
     * Created by AAravindan on 5/9/16.
     */
    "use strict"

    const ROC = require('./ROC.js');
    const SMA = require('../moving_averages/SMA.js');
    const nf  = require('../Utils/NumberFormatter');

    let KST;

    module.exports = KST = function(input) {

      let priceArray  = input.values;
      let rocPer1     = input.ROCPer1;
      let rocPer2     = input.ROCPer2;
      let rocPer3     = input.ROCPer3;
      let rocPer4     = input.ROCPer4;

      let smaPer1     = input.SMAROCPer1;
      let smaPer2     = input.SMAROCPer2;
      let smaPer3     = input.SMAROCPer3;
      let smaPer4     = input.SMAROCPer4;

      let signalPeriod= input.signalPeriod;

      let roc1        = new ROC({ period : rocPer1, values: []});
      let roc2        = new ROC({ period : rocPer2, values: []});
      let roc3        = new ROC({ period : rocPer3, values: []});
      let roc4        = new ROC({ period : rocPer4, values: []});

      let sma1        = new SMA({ period : smaPer1, values: [], format : (v) => {return v}});
      let sma2        = new SMA({ period : smaPer2, values: [], format : (v) => {return v}});
      let sma3        = new SMA({ period : smaPer3, values: [], format : (v) => {return v}});
      let sma4        = new SMA({ period : smaPer4, values: [], format : (v) => {return v}});
      let signalSMA   = new SMA({ period : signalPeriod, values: [], format : (v) => {return v}})

      let format = input.format || nf;

      this.result = [];

      let firstResult = Math.max(rocPer1 + smaPer1, rocPer2+smaPer2, rocPer3+smaPer3, rocPer4+smaPer4);
      this.generator = (function* (){
        let index = 1;
        let tick = yield;
        let kst;
        let RCMA1,RCMA2,RCMA3,RCMA4,signal,result;
        while (true) {
          let roc1Result = roc1.nextValue(tick);
          let roc2Result = roc2.nextValue(tick);
          let roc3Result = roc3.nextValue(tick);
          let roc4Result = roc4.nextValue(tick);
          RCMA1 = (roc1Result!==undefined) ? sma1.nextValue(roc1Result) : undefined;
          RCMA2 = (roc2Result!==undefined) ? sma2.nextValue(roc2Result) : undefined;
          RCMA3 = (roc3Result!==undefined) ? sma3.nextValue(roc3Result) : undefined;
          RCMA4 = (roc4Result!==undefined) ? sma4.nextValue(roc4Result) : undefined;
          if(index < firstResult){
            index++;
          }else {
            kst = (RCMA1 * 1) + (RCMA2 * 2) + (RCMA3 * 3) + (RCMA4 * 4)
          }
          signal = (kst!==undefined) ? signalSMA.nextValue(kst) : undefined;
          result = kst!==undefined ? {
            kst : format(kst),
            signal : signal ? format(signal) : undefined
          } : undefined;
          tick = yield result;
        }
      })();

      this.generator.next();

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

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

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

    KST.prototype.nextValue = function (price) {
      let nextResult = this.generator.next(price);
      if(nextResult.value !== undefined)
        return nextResult.value;
    };