Skip to content

Commit

Permalink
refactor(ADX,ATR,STOCH): Share high low close type
Browse files Browse the repository at this point in the history
  • Loading branch information
bennycode committed Sep 5, 2021
1 parent 300b441 commit e3d677f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/ADX/ADX.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 {ATR, ATRCandle, MovingAverageTypeContext, SMMA} from '..';
import {ATR, HighLowClose, MovingAverageTypeContext, SMMA} from '..';
import {Indicator} from '../Indicator';
import {getAverage} from '../util/getAverage';
import {MovingAverage} from '../MA/MovingAverage';
Expand Down Expand Up @@ -32,12 +32,12 @@ export type ADXResult = {
* @see https://learn.tradimo.com/technical-analysis-how-to-work-with-indicators/adx-determing-the-strength-of-price-movement
*/
export class ADX implements Indicator<ADXResult> {
private readonly candles: ATRCandle[] = [];
private readonly candles: HighLowClose[] = [];
private readonly atr: ATR;
private readonly smoothedPDM: MovingAverage;
private readonly smoothedMDM: MovingAverage;
private readonly dxValues: Big[] = [];
private prevCandle: ATRCandle | undefined;
private prevCandle: HighLowClose | undefined;
private adx: Big | undefined;
private pdi: Big = new Big(0);
private mdi: Big = new Big(0);
Expand All @@ -52,7 +52,7 @@ export class ADX implements Indicator<ADXResult> {
return this.dxValues.length >= this.interval;
}

update(candle: ATRCandle): void {
update(candle: HighLowClose): void {
this.candles.push(candle);
const atrResult = this.atr.update(candle);

Expand Down Expand Up @@ -153,7 +153,7 @@ export class ADX implements Indicator<ADXResult> {
};
}

private directionalMovement(prevCandle: ATRCandle, currentCandle: ATRCandle): {mdm: Big; pdm: Big} {
private directionalMovement(prevCandle: HighLowClose, currentCandle: HighLowClose): {mdm: Big; pdm: Big} {
const currentHigh = new Big(currentCandle.high);
const lastHigh = new Big(prevCandle.high);

Expand Down
16 changes: 8 additions & 8 deletions src/ATR/ATR.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Big, {BigSource} from 'big.js';
import {NotEnoughDataError, SMMA} from '..';
import Big from 'big.js';
import {NotEnoughDataError} from '../error';
import {SimpleIndicator} from '../Indicator';
import {MovingAverage} from '../MA/MovingAverage';
import {MovingAverageTypeContext} from '../MA/MovingAverageTypeContext';

export type ATRCandle = {close: BigSource; high: BigSource; low: BigSource};
import {SMMA} from '../SMMA/SMMA';
import {HighLowClose} from '../util';

/**
* Average True Range (ATR)
Expand All @@ -17,9 +17,9 @@ export type ATRCandle = {close: BigSource; high: BigSource; low: BigSource};
* @see https://www.investopedia.com/terms/a/atr.asp
*/
export class ATR extends SimpleIndicator {
private readonly candles: ATRCandle[] = [];
private readonly candles: HighLowClose[] = [];
private readonly smoothing: MovingAverage;
private prevCandle: ATRCandle | undefined;
private prevCandle: HighLowClose | undefined;

constructor(public readonly interval: number, SmoothingIndicator: MovingAverageTypeContext = SMMA) {
super();
Expand All @@ -30,7 +30,7 @@ export class ATR extends SimpleIndicator {
return this.candles.length > this.interval;
}

override update(candle: ATRCandle): Big | void {
override update(candle: HighLowClose): Big | void {
this.candles.push(candle);

if (!this.prevCandle) {
Expand Down Expand Up @@ -63,7 +63,7 @@ export class ATR extends SimpleIndicator {
return this.result;
}

private trueRange(prevCandle: ATRCandle, currentCandle: ATRCandle): Big {
private trueRange(prevCandle: HighLowClose, currentCandle: HighLowClose): Big {
const prevClose = new Big(prevCandle.close);
const low = new Big(currentCandle.low);
const high = new Big(currentCandle.high);
Expand Down
6 changes: 3 additions & 3 deletions src/STOCH/StochasticOscillator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {Indicator} from '../Indicator';
import Big from 'big.js';
import {SMA} from '../SMA/SMA';
import {ATRCandle} from '../ATR/ATR';
import {MovingAverageTypeContext} from '../MA/MovingAverageTypeContext';
import {MovingAverage} from '../MA/MovingAverage';
import {getMaximum} from '../util/getMaximum';
import {getMinimum} from '../util/getMinimum';
import {NotEnoughDataError} from '../error';
import {HighLowClose} from '../util';

export interface StochasticResult {
d: Big;
Expand All @@ -33,7 +33,7 @@ export interface StochasticResult {
export class StochasticOscillator implements Indicator<StochasticResult> {
public readonly d: MovingAverage;

private readonly candles: ATRCandle[] = [];
private readonly candles: HighLowClose[] = [];
private result?: StochasticResult;

/**
Expand All @@ -59,7 +59,7 @@ export class StochasticOscillator implements Indicator<StochasticResult> {
return this.result;
}

update(candle: ATRCandle): StochasticResult | void {
update(candle: HighLowClose): StochasticResult | void {
this.candles.push(candle);

if (this.candles.length > this.periodK) {
Expand Down
3 changes: 3 additions & 0 deletions src/util/HighLowClose.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {BigSource} from 'big.js';

export type HighLowClose = {close: BigSource; high: BigSource; low: BigSource};
4 changes: 4 additions & 0 deletions src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
export * from './BandsResult';
export * from './getAverage';
export * from './getFixedArray';
export * from './getMaximum';
export * from './getMinimum';
export * from './HighLowClose';

0 comments on commit e3d677f

Please sign in to comment.