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

100% Statements 111/111
97.56% Branches 40/41
100% Functions 18/18
100% Lines 108/108
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 138 139 140 141 142 143 144          28×   29× 29× 28× 28×   29× 28× 28× 28×     27× 27× 27× 27× 27× 27× 27× 27× 27× 27× 27× 27×     26× 26× 25×     27×   25× 25× 25× 25× 25× 25× 25× 24×     27× 27× 26×   27× 26×     75× 75× 75× 75× 75× 400×   75× 75× 75× 75× 640× 640×   75×   25× 25× 25× 25× 230× 230× 230×   25×   25× 25× 25× 25× 180× 180× 180×   25×   25× 25× 25× 230×   25×   24× 24× 24× 24× 177×   177× 177×   24×     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 MacdIndicator = (function (_super) {
        __extends(MacdIndicator, _super);
        function MacdIndicator() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        MacdIndicator.prototype.initSeriesCollection = function (indicator, chart) {
            _super.prototype.initSeriesCollection.call(this, indicator, chart);
            if (indicator.macdType === 'Line' || indicator.macdType === 'Both') {
                var macdSeries = new chart_series_1.Series(indicator, 'targetSeries', {}, true);
                this.setSeriesProperties(macdSeries, indicator, 'MacdLine', indicator.macdLine.color, indicator.macdLine.width, chart);
            }
            if (indicator.macdType === 'Histogram' || indicator.macdType === 'Both') {
                var histogramSeries = new chart_series_1.Series(indicator, 'targetSeries', {}, true);
                histogramSeries.type = 'Column';
                this.setSeriesProperties(histogramSeries, indicator, 'Histogram', indicator.macdPositiveColor, indicator.width, chart);
            }
        };
        MacdIndicator.prototype.initDataSource = function (indicator) {
            var signalCollection = [];
            var fastPeriod = indicator.fastPeriod;
            var slowPeriod = indicator.slowPeriod;
            var trigger = indicator.period;
            var length = fastPeriod + trigger;
            var macdCollection = [];
            var histogramCollection = [];
            var validData = indicator.points;
            var signalSeries = indicator.targetSeries[0];
            var histogramSeries;
            var macdLineSeries;
            if (indicator.macdType === 'Histogram') {
                histogramSeries = indicator.targetSeries[1];
            }
            else {
                macdLineSeries = indicator.targetSeries[1];
                if (indicator.macdType === 'Both') {
                    histogramSeries = indicator.targetSeries[2];
                }
            }
            if (validData && length < validData.length && slowPeriod <= fastPeriod &&
                slowPeriod > 0 && (length - 2) >= 0) {
                var shortEMA = this.calculateEMAValues(slowPeriod, validData, 'close');
                var longEMA = this.calculateEMAValues(fastPeriod, validData, 'close');
                var macdValues = this.getMACDVales(indicator, shortEMA, longEMA);
                macdCollection = this.getMACDPoints(indicator, macdValues, validData, macdLineSeries || signalSeries);
                var signalEMA = this.calculateEMAValues(trigger, macdCollection, 'y');
                signalCollection = this.getSignalPoints(indicator, signalEMA, validData, signalSeries);
                if (histogramSeries) {
                    histogramCollection = this.getHistogramPoints(indicator, macdValues, signalEMA, validData, histogramSeries);
                }
            }
            this.setSeriesRange(signalCollection, indicator, indicator.targetSeries[0]);
            if (histogramSeries) {
                this.setSeriesRange(histogramCollection, indicator, histogramSeries);
            }
            if (macdLineSeries) {
                this.setSeriesRange(macdCollection, indicator, macdLineSeries);
            }
        };
        MacdIndicator.prototype.calculateEMAValues = function (period, validData, field) {
            var sum = 0;
            var initialEMA = 0;
            var emaValues = [];
            var emaPercent = (2 / (period + 1));
            for (var i = 0; i < period; i++) {
                sum += Number(validData[i][field]);
            }
            initialEMA = (sum / period);
            emaValues.push(initialEMA);
            var emaAvg = initialEMA;
            for (var j = period; j < validData.length; j++) {
                emaAvg = (Number(validData[j][field]) - emaAvg) * emaPercent + emaAvg;
                emaValues.push(emaAvg);
            }
            return emaValues;
        };
        MacdIndicator.prototype.getMACDPoints = function (indicator, macdPoints, validData, series) {
            var macdCollection = [];
            var dataMACDIndex = indicator.fastPeriod - 1;
            var macdIndex = 0;
            while (dataMACDIndex < validData.length) {
                macdCollection.push(this.getDataPoint(validData[dataMACDIndex].x, macdPoints[macdIndex], validData[dataMACDIndex], series, macdCollection.length));
                dataMACDIndex++;
                macdIndex++;
            }
            return macdCollection;
        };
        MacdIndicator.prototype.getSignalPoints = function (indicator, signalEma, validData, series) {
            var dataSignalIndex = indicator.fastPeriod + indicator.period - 2;
            var signalIndex = 0;
            var signalCollection = [];
            while (dataSignalIndex < validData.length) {
                signalCollection.push(this.getDataPoint(validData[dataSignalIndex].x, signalEma[signalIndex], validData[dataSignalIndex], series, signalCollection.length));
                dataSignalIndex++;
                signalIndex++;
            }
            return signalCollection;
        };
        MacdIndicator.prototype.getMACDVales = function (indicator, shortEma, longEma) {
            var macdPoints = [];
            var diff = indicator.fastPeriod - indicator.slowPeriod;
            for (var i = 0; i < longEma.length; i++) {
                macdPoints.push((shortEma[i + diff] - longEma[i]));
            }
            return macdPoints;
        };
        MacdIndicator.prototype.getHistogramPoints = function (indicator, macdPoints, signalEma, validData, series) {
            var dataHistogramIndex = indicator.fastPeriod + indicator.period - 2;
            var histogramIndex = 0;
            var histogramCollection = [];
            while (dataHistogramIndex < validData.length) {
                histogramCollection.push(this.getDataPoint(validData[dataHistogramIndex].x, macdPoints[histogramIndex + (indicator.period - 1)] -
                    signalEma[histogramIndex], validData[dataHistogramIndex], series, histogramCollection.length, indicator));
                dataHistogramIndex++;
                histogramIndex++;
            }
            return histogramCollection;
        };
        MacdIndicator.prototype.destroy = function () {
        };
        MacdIndicator.prototype.getModuleName = function () {
            return 'MacdIndicator';
        };
        return MacdIndicator;
    }(indicator_base_1.TechnicalAnalysis));
    exports.MacdIndicator = MacdIndicator;
});