diff --git a/src/DMA/DMA.ts b/src/DMA/DMA.ts index d6eb023d..1f69656f 100644 --- a/src/DMA/DMA.ts +++ b/src/DMA/DMA.ts @@ -1,4 +1,5 @@ import type {Big, BigSource} from '../index.js'; +import {TechnicalIndicator} from '../index.js'; import type {Indicator} from '../Indicator.js'; import type {FasterMovingAverage, MovingAverage} from '../MA/MovingAverage.js'; import type {FasterMovingAverageTypes, MovingAverageTypes} from '../MA/MovingAverageTypes.js'; @@ -23,11 +24,12 @@ export interface FasterDMAResult { * * @see https://faculty.fuqua.duke.edu/~charvey/Teaching/BA453_2002/CCAM/CCAM.htm#_Toc2634228 */ -export class DMA implements Indicator { +export class DMA extends TechnicalIndicator { public readonly short: MovingAverage; public readonly long: MovingAverage; constructor(short: number, long: number, Indicator: MovingAverageTypes = SMA) { + super(); this.short = new Indicator(short); this.long = new Indicator(long); } diff --git a/src/Indicator.ts b/src/Indicator.ts index 2941150f..43a35393 100644 --- a/src/Indicator.ts +++ b/src/Indicator.ts @@ -1,5 +1,5 @@ import {NotEnoughDataError} from './error/index.js'; -import type {Big, BigSource} from './index.js'; +import {getLastFromForEach, type Big, type BigSource} from './index.js'; export interface Indicator { getResult(): Result; @@ -13,6 +13,22 @@ export interface Indicator { updates(input: Input[], replace: boolean): void | Result; } +export abstract class TechnicalIndicator implements Indicator { + accessor isStable: boolean = false; + + abstract getResult(): Result; + + replace(input: Input) { + return this.update(input, true); + } + + abstract update(input: Input, replace: boolean): void | Result; + + updates(inputs: Input[], replace: boolean) { + return getLastFromForEach(inputs, input => this.update(input, replace)); + } +} + /** * Tracks results of an indicator over time and memorizes the highest & lowest result. */