UNPKG

@amcharts/amcharts5

Version:
184 lines 7.68 kB
import { __awaiter } from "tslib"; import { Indicator } from "./Indicator"; import { LineSeries } from "../../xy/series/LineSeries"; // factor is stored in thousandths, but shown as 1 = single band width const FACTOR_SCALE = 1000; /** * An implementation of a [[StockChart]] indicator. * * @see {@link https://www.amcharts.com/docs/v5/charts/stock/indicators/} for more info */ export class AccelerationBands extends Indicator { constructor() { super(...arguments); this._editableSettings = [{ key: "period", name: this.root.language.translateAny("Period"), type: "number", minValue: 10, maxValue: 100, step: 1 }, { key: "factor", name: this.root.language.translateAny("Factor"), type: "number", minValue: 0.0005, maxValue: 0.003, step: 0.0001, // Stored in thousandths (0.001 = standard bands); shown in the UI as the // conventional 1 = single band width (so the field reads 0.5..3.0). scale: FACTOR_SCALE }, { key: "upperColor", name: this.root.language.translateAny("Upper"), type: "color" }, { key: "seriesColor", name: this.root.language.translateAny("Average"), type: "color" }, { key: "lowerColor", name: this.root.language.translateAny("Lower"), type: "color" }]; } _afterNew() { const stockSeries = this.get("stockSeries"); const chart = stockSeries.chart; if (chart) { const series = chart.series.push(LineSeries.new(this._root, { valueXField: "valueX", valueYField: "average", groupDataDisabled: true, calculateAggregates: true, xAxis: stockSeries.get("xAxis"), yAxis: stockSeries.get("yAxis"), themeTags: ["indicator", "accelerationbands", "average"] })); series.setPrivate("baseValueSeries", stockSeries); this.series = series; const upperBandSeries = chart.series.push(LineSeries.new(this._root, { valueXField: "valueX", valueYField: "upper", openValueYField: "lower", calculateAggregates: true, xAxis: stockSeries.get("xAxis"), yAxis: stockSeries.get("yAxis"), groupDataDisabled: true, themeTags: ["indicator", "accelerationbands", "upper"] })); upperBandSeries.setPrivate("baseValueSeries", stockSeries); this.upperBandSeries = upperBandSeries; const lowerBandSeries = chart.series.push(LineSeries.new(this._root, { valueXField: "valueX", valueYField: "lower", calculateAggregates: true, xAxis: stockSeries.get("xAxis"), yAxis: stockSeries.get("yAxis"), groupDataDisabled: true, themeTags: ["indicator", "accelerationbands", "lower"] })); lowerBandSeries.setPrivate("baseValueSeries", stockSeries); this.lowerBandSeries = lowerBandSeries; } this._handleLegend(this.series); super._afterNew(); this.series.addTag("accelerationbands"); this.series._applyThemes(); } _prepareChildren() { super._prepareChildren(); if (this.isDirty("factor")) { this.markDataDirty(); // legend shows the same scaled value as the settings modal this.setCustomData("factor", this.get("factor", 0.001) * FACTOR_SCALE); } } _updateChildren() { super._updateChildren(); const upperColor = "upperColor"; if (this.isDirty(upperColor)) { const color = this.get(upperColor); const upperBandSeries = this.upperBandSeries; upperBandSeries.set("stroke", color); upperBandSeries.set("fill", color); upperBandSeries.strokes.template.set("stroke", color); this._updateSeriesColor(upperBandSeries, color, upperColor); } const lowerColor = "lowerColor"; if (this.isDirty(lowerColor)) { const color = this.get(lowerColor); const lowerBandSeries = this.lowerBandSeries; lowerBandSeries.set("stroke", color); lowerBandSeries.strokes.template.set("stroke", color); this._updateSeriesColor(lowerBandSeries, color, lowerColor); } } prepareData() { super.prepareData(); if (this.series) { let period = this.get("period", 20); const stockSeries = this.get("stockSeries"); const dataItems = stockSeries.dataItems; let data = this._getDataArray(dataItems); let factor = this.get("factor", 0.001); for (let i = 0; i < data.length; i++) { const dataItem = data[i]; const stockDataItem = dataItems[i]; if (stockDataItem) { const low = stockDataItem.get("lowValueY"); const high = stockDataItem.get("highValueY"); if (low == null || high == null) { continue; } const sum = high + low; if (sum === 0) { continue; } const ff = (((high - low) / (sum / 2)) * 1000) * factor; dataItem._upper = (high * (1 + 2 * ff)); dataItem._lower = (low * (1 - 2 * ff)); dataItem._average = dataItem._lower + (dataItem._upper - dataItem._lower) / 2; } } this._sma(data, period, "_lower", "lower"); this._sma(data, period, "_upper", "upper"); this._sma(data, period, "_average", "average"); this.upperBandSeries.updateData(data); this.lowerBandSeries.updateData(data); this.series.updateData(data); } } _dispose() { this.upperBandSeries.dispose(); this.lowerBandSeries.dispose(); super._dispose(); } hide(duration) { const _super = Object.create(null, { hide: { get: () => super.hide } }); return __awaiter(this, void 0, void 0, function* () { return Promise.all([ _super.hide.call(this, duration), this.upperBandSeries.hide(duration), this.lowerBandSeries.hide(duration) ]); }); } show(duration) { const _super = Object.create(null, { show: { get: () => super.show } }); return __awaiter(this, void 0, void 0, function* () { return Promise.all([ _super.show.call(this, duration), this.upperBandSeries.show(duration), this.lowerBandSeries.show(duration) ]); }); } } AccelerationBands.className = "AccelerationBands"; AccelerationBands.classNames = Indicator.classNames.concat([AccelerationBands.className]); //# sourceMappingURL=AccelerationBands.js.map