Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Simplify indicator interfaces #351

Merged
merged 1 commit into from
Nov 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/AC/AC.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/AO/AO.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/ATR/ATR.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/BBANDS/BollingerBands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -60,7 +60,7 @@ export class BollingerBands implements Indicator<BandsResult> {
}
}

export class FasterBollingerBands implements FasterIndicator<FasterBandsResult> {
export class FasterBollingerBands implements Indicator<FasterBandsResult> {
public readonly prices: number[] = [];
private result: FasterBandsResult | undefined;

Expand Down
4 changes: 2 additions & 2 deletions src/CG/CG.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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[] = [];
Expand Down
4 changes: 2 additions & 2 deletions src/DEMA/DEMA.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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;

Expand Down
29 changes: 18 additions & 11 deletions src/Indicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,25 @@ export interface Indicator<T = Big> {

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<T = Big> extends Indicator<T> {
update(value: T): void | T;
}

export type SimpleNumberIndicator = SimpleIndicator<number>;

/**
* Tracks results of an indicator over time and memorizes the highest & lowest result.
*/
export interface IndicatorSeries<T = Big> extends Indicator<T> {
highest?: T;
lowest?: T;
}

export abstract class BigIndicatorSeries implements IndicatorSeries {
highest?: Big;
lowest?: Big;
protected result?: Big;
Expand All @@ -34,15 +49,7 @@ export abstract class SimpleIndicator implements Indicator {
abstract update(...args: any): void;
}

export interface FasterIndicator<T = number> {
getResult(): T;

isStable: boolean;

update(price: number): void | T;
}

export abstract class FasterSimpleIndicator implements FasterIndicator {
export abstract class NumberIndicatorSeries implements IndicatorSeries<number> {
highest?: number;
lowest?: number;
protected result?: number;
Expand Down
6 changes: 3 additions & 3 deletions src/MA/MovingAverage.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions src/MOM/MOM.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/ROC/ROC.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/RSI/RSI.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/SMA/SMA.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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[] = [];

Expand Down