Skip to content

Commit

Permalink
extend technical indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
bennycode committed Dec 29, 2024
1 parent cd3fd5b commit 0862e77
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/DMA/DMA.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<DMAResult> {
export class DMA extends TechnicalIndicator<DMAResult, BigSource> {
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);
}
Expand Down
18 changes: 17 additions & 1 deletion src/Indicator.ts
Original file line number Diff line number Diff line change
@@ -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<Result = Big, Input = BigSource> {
getResult(): Result;
Expand All @@ -13,6 +13,22 @@ export interface Indicator<Result = Big, Input = BigSource> {
updates(input: Input[], replace: boolean): void | Result;
}

export abstract class TechnicalIndicator<Result, Input> implements Indicator<Result, Input> {
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.
*/
Expand Down

0 comments on commit 0862e77

Please sign in to comment.