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

100% Statements 45/45
94.74% Branches 18/19
100% Functions 12/12
100% Lines 42/42
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          38×   37× 37× 37× 37× 37× 37× 34× 34× 34× 34× 109×   34× 34× 34× 34× 560× 560× 560× 560×     37×     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", "../../common/utils/helper", "./indicator-base"], function (require, exports, helper_1, indicator_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var EmaIndicator = (function (_super) {
        __extends(EmaIndicator, _super);
        function EmaIndicator() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        EmaIndicator.prototype.initDataSource = function (indicator) {
            var field = helper_1.firstToLowerCase(indicator.field);
            var xField = 'x';
            var emaPoints = [];
            var signalSeries = indicator.targetSeries[0];
            var validData = indicator.points;
            if (validData && validData.length && validData.length >= indicator.period) {
                var sum = 0;
                var average = 0;
                var k = (2 / (indicator.period + 1));
                for (var i = 0; i < indicator.period; i++) {
                    sum += validData[i][field];
                }
                average = sum / indicator.period;
                emaPoints.push(this.getDataPoint(validData[indicator.period - 1][xField], average, validData[indicator.period - 1], signalSeries, emaPoints.length));
                var index = indicator.period;
                while (index < validData.length) {
                    var prevAverage = emaPoints[index - indicator.period][signalSeries.yName];
                    var yValue = (validData[index][field] - prevAverage) * k + prevAverage;
                    emaPoints.push(this.getDataPoint(validData[index][xField], yValue, validData[index], signalSeries, emaPoints.length));
                    index++;
                }
            }
            this.setSeriesRange(emaPoints, indicator);
        };
        EmaIndicator.prototype.destroy = function () {
        };
        EmaIndicator.prototype.getModuleName = function () {
            return 'EmaIndicator';
        };
        return EmaIndicator;
    }(indicator_base_1.TechnicalAnalysis));
    exports.EmaIndicator = EmaIndicator;
});