From d31f44f4a77cfcc5533b81aee87a1c414418165d Mon Sep 17 00:00:00 2001 From: Benny Neugebauer Date: Sun, 7 Nov 2021 17:55:01 +0100 Subject: [PATCH] refactor: Simplify indicator interfaces --- src/AC/AC.ts | 4 ++-- src/AO/AO.ts | 4 ++-- src/ATR/ATR.ts | 4 ++-- src/BBANDS/BollingerBands.ts | 4 ++-- src/CG/CG.ts | 4 ++-- src/DEMA/DEMA.ts | 4 ++-- src/Indicator.ts | 29 ++++++++++++++++++----------- src/MA/MovingAverage.ts | 6 +++--- src/MOM/MOM.ts | 4 ++-- src/ROC/ROC.ts | 4 ++-- src/RSI/RSI.ts | 4 ++-- src/SMA/SMA.ts | 4 ++-- 12 files changed, 41 insertions(+), 34 deletions(-) diff --git a/src/AC/AC.ts b/src/AC/AC.ts index 905f884d5..9af5e22df 100644 --- a/src/AC/AC.ts +++ b/src/AC/AC.ts @@ -1,4 +1,4 @@ -import {SimpleIndicator} from '../Indicator'; +import {BigIndicatorSeries} from '../Indicator'; import Big, {BigSource} from 'big.js'; import {NotEnoughDataError} from '../error'; import {AO} from '../AO/AO'; @@ -17,7 +17,7 @@ import {MOM} from '../MOM/MOM'; * * @see https://www.thinkmarkets.com/en/indicators/bill-williams-accelerator/ */ -export class AC extends SimpleIndicator { +export class AC extends BigIndicatorSeries { public readonly ao: AO; public readonly momentum: MOM; public readonly signal: SMA; diff --git a/src/AO/AO.ts b/src/AO/AO.ts index dfa86e2e0..fc111e345 100644 --- a/src/AO/AO.ts +++ b/src/AO/AO.ts @@ -1,4 +1,4 @@ -import {SimpleIndicator} from '../Indicator'; +import {BigIndicatorSeries} from '../Indicator'; import Big, {BigSource} from 'big.js'; import {SMA} from '../SMA/SMA'; import {NotEnoughDataError} from '../error'; @@ -16,7 +16,7 @@ import {NotEnoughDataError} from '../error'; * @see https://www.tradingview.com/support/solutions/43000501826-awesome-oscillator-ao/ * @see https://tradingstrategyguides.com/bill-williams-awesome-oscillator-strategy/ */ -export class AO extends SimpleIndicator { +export class AO extends BigIndicatorSeries { public readonly long: SMA; public readonly short: SMA; diff --git a/src/ATR/ATR.ts b/src/ATR/ATR.ts index 55421bdb5..58213e2ca 100644 --- a/src/ATR/ATR.ts +++ b/src/ATR/ATR.ts @@ -1,6 +1,6 @@ import Big from 'big.js'; import {NotEnoughDataError} from '../error'; -import {SimpleIndicator} from '../Indicator'; +import {BigIndicatorSeries} from '../Indicator'; import {MovingAverage} from '../MA/MovingAverage'; import {MovingAverageTypeContext} from '../MA/MovingAverageTypeContext'; import {SMMA} from '../SMMA/SMMA'; @@ -16,7 +16,7 @@ import {HighLowClose} from '../util'; * * @see https://www.investopedia.com/terms/a/atr.asp */ -export class ATR extends SimpleIndicator { +export class ATR extends BigIndicatorSeries { private readonly candles: HighLowClose[] = []; private readonly smoothing: MovingAverage; private prevCandle: HighLowClose | undefined; diff --git a/src/BBANDS/BollingerBands.ts b/src/BBANDS/BollingerBands.ts index 4135f5866..ebd189fe9 100644 --- a/src/BBANDS/BollingerBands.ts +++ b/src/BBANDS/BollingerBands.ts @@ -2,7 +2,7 @@ import Big, {BigSource} from 'big.js'; import {SMA} from '../SMA/SMA'; import {NotEnoughDataError} from '../error'; import {BandsResult, FasterBandsResult} from '../util/BandsResult'; -import {FasterIndicator, Indicator} from '../Indicator'; +import {Indicator} from '../Indicator'; import {getFasterAverage, getFasterStandardDeviation, getStandardDeviation} from '../util'; /** @@ -60,7 +60,7 @@ export class BollingerBands implements Indicator { } } -export class FasterBollingerBands implements FasterIndicator { +export class FasterBollingerBands implements Indicator { public readonly prices: number[] = []; private result: FasterBandsResult | undefined; diff --git a/src/CG/CG.ts b/src/CG/CG.ts index 885c14ec0..901c2fa16 100644 --- a/src/CG/CG.ts +++ b/src/CG/CG.ts @@ -1,4 +1,4 @@ -import {SimpleIndicator} from '../Indicator'; +import {BigIndicatorSeries} from '../Indicator'; import Big, {BigSource} from 'big.js'; import {SMA} from '../SMA/SMA'; import {NotEnoughDataError} from '../error'; @@ -16,7 +16,7 @@ import {NotEnoughDataError} from '../error'; * profitable trading * @see http://www.mesasoftware.com/papers/TheCGOscillator.pdf */ -export class CG extends SimpleIndicator { +export class CG extends BigIndicatorSeries { public signal: SMA; public readonly prices: Big[] = []; diff --git a/src/DEMA/DEMA.ts b/src/DEMA/DEMA.ts index 54005a053..3dda3d782 100644 --- a/src/DEMA/DEMA.ts +++ b/src/DEMA/DEMA.ts @@ -1,6 +1,6 @@ import Big, {BigSource} from 'big.js'; import {EMA, NotEnoughDataError} from '..'; -import {SimpleIndicator} from '../Indicator'; +import {BigIndicatorSeries} from '../Indicator'; /** * Double Exponential Moving Average (DEMA) @@ -12,7 +12,7 @@ import {SimpleIndicator} from '../Indicator'; * * @see https://www.investopedia.com/terms/d/double-exponential-moving-average.asp */ -export class DEMA extends SimpleIndicator { +export class DEMA extends BigIndicatorSeries { private readonly inner: EMA; private readonly outer: EMA; diff --git a/src/Indicator.ts b/src/Indicator.ts index 63958f48b..c849fb85f 100644 --- a/src/Indicator.ts +++ b/src/Indicator.ts @@ -5,10 +5,25 @@ export interface Indicator { isStable: boolean; - update(...args: any): void; + update(...args: any): void | T; } -export abstract class SimpleIndicator implements Indicator { +/** Defines indicators which only require a single value (usually the price) to get updated. */ +export interface SimpleIndicator extends Indicator { + update(value: T): void | T; +} + +export type SimpleNumberIndicator = SimpleIndicator; + +/** + * Tracks results of an indicator over time and memorizes the highest & lowest result. + */ +export interface IndicatorSeries extends Indicator { + highest?: T; + lowest?: T; +} + +export abstract class BigIndicatorSeries implements IndicatorSeries { highest?: Big; lowest?: Big; protected result?: Big; @@ -34,15 +49,7 @@ export abstract class SimpleIndicator implements Indicator { abstract update(...args: any): void; } -export interface FasterIndicator { - getResult(): T; - - isStable: boolean; - - update(price: number): void | T; -} - -export abstract class FasterSimpleIndicator implements FasterIndicator { +export abstract class NumberIndicatorSeries implements IndicatorSeries { highest?: number; lowest?: number; protected result?: number; diff --git a/src/MA/MovingAverage.ts b/src/MA/MovingAverage.ts index ad4573975..22ecdd7eb 100644 --- a/src/MA/MovingAverage.ts +++ b/src/MA/MovingAverage.ts @@ -1,6 +1,6 @@ import Big, {BigSource} from 'big.js'; import {NotEnoughDataError} from '../error'; -import {FasterSimpleIndicator, SimpleIndicator} from '../Indicator'; +import {NumberIndicatorSeries, BigIndicatorSeries} from '../Indicator'; /** * Moving Average (MA) @@ -10,7 +10,7 @@ import {FasterSimpleIndicator, SimpleIndicator} from '../Indicator'; * * @see https://www.investopedia.com/terms/m/movingaverage.asp */ -export abstract class MovingAverage extends SimpleIndicator { +export abstract class MovingAverage extends BigIndicatorSeries { constructor(public readonly interval: number) { super(); } @@ -29,7 +29,7 @@ export abstract class MovingAverage extends SimpleIndicator { abstract update(price: BigSource): Big | void; } -export abstract class FasterMovingAverage extends FasterSimpleIndicator { +export abstract class FasterMovingAverage extends NumberIndicatorSeries { constructor(public readonly interval: number) { super(); } diff --git a/src/MOM/MOM.ts b/src/MOM/MOM.ts index 60a71f231..1ccf82219 100644 --- a/src/MOM/MOM.ts +++ b/src/MOM/MOM.ts @@ -1,4 +1,4 @@ -import {SimpleIndicator} from '../Indicator'; +import {BigIndicatorSeries} from '../Indicator'; import Big, {BigSource} from 'big.js'; import {getFixedArray} from '../util/getFixedArray'; import {NotEnoughDataError} from '../error'; @@ -11,7 +11,7 @@ import {NotEnoughDataError} from '../error'; * * @see https://www.warriortrading.com/momentum-indicator/ */ -export class MOM extends SimpleIndicator { +export class MOM extends BigIndicatorSeries { private readonly history: BigSource[]; private readonly historyLength: number; diff --git a/src/ROC/ROC.ts b/src/ROC/ROC.ts index c605f4494..1a807e08b 100644 --- a/src/ROC/ROC.ts +++ b/src/ROC/ROC.ts @@ -1,6 +1,6 @@ import Big, {BigSource} from 'big.js'; import {NotEnoughDataError} from '../error'; -import {SimpleIndicator} from '../Indicator'; +import {BigIndicatorSeries} from '../Indicator'; /** * Rate Of Change Indicator (ROC) @@ -10,7 +10,7 @@ import {SimpleIndicator} from '../Indicator'; * * @see https://www.investopedia.com/terms/r/rateofchange.asp */ -export class ROC extends SimpleIndicator { +export class ROC extends BigIndicatorSeries { private readonly priceHistory: BigSource[] = []; constructor(public readonly interval: number) { diff --git a/src/RSI/RSI.ts b/src/RSI/RSI.ts index 6ce6154d7..d5b17f02d 100644 --- a/src/RSI/RSI.ts +++ b/src/RSI/RSI.ts @@ -1,7 +1,7 @@ import Big, {BigSource} from 'big.js'; import {MovingAverageTypeContext, NotEnoughDataError, SMMA} from '..'; import {MovingAverage} from '../MA/MovingAverage'; -import {SimpleIndicator} from '../Indicator'; +import {BigIndicatorSeries} from '../Indicator'; /** * Relative Strength Index (RSI) @@ -18,7 +18,7 @@ import {SimpleIndicator} from '../Indicator'; * * @see https://www.investopedia.com/terms/r/rsi.asp */ -export class RSI extends SimpleIndicator { +export class RSI extends BigIndicatorSeries { public readonly prices: Big[] = []; private readonly avgGain: MovingAverage; private readonly avgLoss: MovingAverage; diff --git a/src/SMA/SMA.ts b/src/SMA/SMA.ts index 60dc8009b..297447c98 100644 --- a/src/SMA/SMA.ts +++ b/src/SMA/SMA.ts @@ -1,7 +1,7 @@ import Big, {BigSource} from 'big.js'; import {MovingAverage} from '../MA/MovingAverage'; import {NotEnoughDataError} from '../error'; -import {FasterIndicator} from '../Indicator'; +import {SimpleNumberIndicator} from '../Indicator'; /** * Simple Moving Average (SMA) @@ -33,7 +33,7 @@ export class SMA extends MovingAverage { } } -export class FasterSMA implements FasterIndicator { +export class FasterSMA implements SimpleNumberIndicator { protected result?: number; public readonly prices: number[] = [];