all files / chart/technical-indicators/ stochastic-indicator.js

100% Statements 108/108
93.33% Branches 28/30
100% Functions 15/15
100% Lines 105/105
11 statements, 6 functions, 9 branches Ignored     
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137          61×   62× 62× 62× 62× 60× 60× 60× 60×     60× 60× 60× 60× 60× 60× 60× 48× 46× 2107× 2107×     48× 48× 48×   60× 60× 60× 58× 58×     96× 96× 94× 94× 94× 94× 4236× 4236×   94× 94× 3154× 3154× 25478×   3154× 3154× 3154× 3154×   94× 94× 4236× 3154× 3154×       96×   48× 48× 48× 48× 48× 2131× 2131× 2131×   48× 48× 48× 48× 113× 113× 113×   48× 2018× 2018× 2018× 6207× 6207×   2018× 2018×   48× 2018× 2018× 2018× 2018× 2018×     48×     41750×        
/* istanbul ignore next */ 
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
define(["require", "exports", "../series/chart-series", "./indicator-base"], function (require, exports, chart_series_1, indicator_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var StochasticIndicator = (function (_super) {
        __extends(StochasticIndicator, _super);
        function StochasticIndicator() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        StochasticIndicator.prototype.initSeriesCollection = function (indicator, chart) {
            _super.prototype.initSeriesCollection.call(this, indicator, chart);
            var periodLine = new chart_series_1.Series(indicator, 'targetSeries', {}, true);
            this.setSeriesProperties(periodLine, indicator, 'PeriodLine', indicator.periodLine.color, indicator.periodLine.width, chart);
            if (indicator.showZones) {
                var upperSeries = new chart_series_1.Series(indicator, 'targetSeries', {}, true);
                this.setSeriesProperties(upperSeries, indicator, 'UpperLine', indicator.upperLine.color, indicator.upperLine.width, chart);
                var lowerSeries = new chart_series_1.Series(indicator, 'targetSeries', {}, true);
                this.setSeriesProperties(lowerSeries, indicator, 'LowerLine', indicator.lowerLine.color, indicator.lowerLine.width, chart);
            }
        };
        StochasticIndicator.prototype.initDataSource = function (indicator) {
            var signalCollection = [];
            var upperCollection = [];
            var lowerCollection = [];
            var periodCollection = [];
            var source = [];
            var validData = indicator.points;
            if (validData.length && validData.length >= indicator.period) {
                if (indicator.showZones) {
                    for (var i = 0; i < validData.length; i++) {
                        upperCollection.push(this.getDataPoint(validData[i].x, indicator.overBought, validData[i], indicator.targetSeries[2], upperCollection.length));
                        lowerCollection.push(this.getDataPoint(validData[i].x, indicator.overSold, validData[i], indicator.targetSeries[3], lowerCollection.length));
                    }
                }
                source = this.calculatePeriod(indicator.period, indicator.kPeriod, validData, indicator.targetSeries[1]);
                periodCollection = this.smaCalculation(indicator.period, indicator.kPeriod, source, indicator.targetSeries[1]);
                signalCollection = this.smaCalculation(indicator.period + indicator.kPeriod - 1, indicator.dPeriod, source, indicator.targetSeries[0]);
            }
            this.setSeriesRange(signalCollection, indicator, indicator.targetSeries[0]);
            this.setSeriesRange(periodCollection, indicator, indicator.targetSeries[1]);
            if (indicator.showZones) {
                this.setSeriesRange(upperCollection, indicator, indicator.targetSeries[2]);
                this.setSeriesRange(lowerCollection, indicator, indicator.targetSeries[3]);
            }
        };
        StochasticIndicator.prototype.smaCalculation = function (period, kPeriod, data, sourceSeries) {
            var pointCollection = [];
            if (data.length >= period + kPeriod) {
                var count = period + (kPeriod - 1);
                var temp = [];
                var values = [];
                for (var i = 0; i < data.length; i++) {
                    var value = Number(data[i].y);
                    temp.push(value);
                }
                var length_1 = temp.length;
                while (length_1 >= count) {
                    var sum = 0;
                    for (var i = period - 1; i < (period + kPeriod - 1); i++) {
                        sum = sum + temp[i];
                    }
                    sum = sum / kPeriod;
                    values.push(sum.toFixed(2));
                    temp.splice(0, 1);
                    length_1 = temp.length;
                }
                var len = count - 1;
                for (var i = 0; i < data.length; i++) {
                    if (!(i < len)) {
                        pointCollection.push(this.getDataPoint(data[i].x, Number(values[i - len]), data[i], sourceSeries, pointCollection.length));
                        data[i].y = Number((values[i - len]));
                    }
                }
            }
            return pointCollection;
        };
        StochasticIndicator.prototype.calculatePeriod = function (period, kPeriod, data, series) {
            var lowValues = [];
            var highValues = [];
            var closeValues = [];
            var modifiedSource = [];
            for (var j = 0; j < data.length; j++) {
                lowValues[j] = data[j].low;
                highValues[j] = data[j].high;
                closeValues[j] = data[j].close;
            }
            Eif (data.length > period) {
                var mins = [];
                var maxs = [];
                for (var i = 0; i < period - 1; ++i) {
                    maxs.push(0);
                    mins.push(0);
                    modifiedSource.push(this.getDataPoint(data[i].x, data[i].close, data[i], series, modifiedSource.length));
                }
                for (var i = period - 1; i < data.length; ++i) {
                    var min = Number.MAX_VALUE;
                    var max = Number.MIN_VALUE;
                    for (var j = 0; j < period; ++j) {
                        min = Math.min(min, lowValues[i - j]);
                        max = Math.max(max, highValues[i - j]);
                    }
                    maxs.push(max);
                    mins.push(min);
                }
                for (var i = period - 1; i < data.length; ++i) {
                    var top_1 = 0;
                    var bottom = 0;
                    top_1 += closeValues[i] - mins[i];
                    bottom += maxs[i] - mins[i];
                    modifiedSource.push(this.getDataPoint(data[i].x, (top_1 / bottom) * 100, data[i], series, modifiedSource.length));
                }
            }
            return modifiedSource;
        };
        StochasticIndicator.prototype.destroy = function () {
        };
        StochasticIndicator.prototype.getModuleName = function () {
            return 'StochasticIndicator';
        };
        return StochasticIndicator;
    }(indicator_base_1.TechnicalAnalysis));
    exports.StochasticIndicator = StochasticIndicator;
});