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

feat(DMA): Add faster implementation #375

Merged
merged 2 commits into from
Dec 6, 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
8 changes: 7 additions & 1 deletion src/DMA/DMA.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import twoDays from '../test/fixtures/DMA/LTC-USDT-1h-2d.json';
import {DMA} from './DMA';
import {DMA, FasterDMA} from './DMA';
import {EMA, SMA} from '..';

describe('DMA', () => {
Expand Down Expand Up @@ -56,15 +56,21 @@ describe('DMA', () => {
describe('getResult', () => {
it('detects uptrends', () => {
const dma = new DMA(3, 8);
const fasterDMA = new FasterDMA(3, 8);
const nineHours = twoDays.slice(0, 9);

for (const oneHour of nineHours) {
const price = oneHour.close;
dma.update(price);
fasterDMA.update(parseFloat(price));
}

const {short, long} = dma.getResult();
expect(short.gt(long)).toBeTrue();

const fasterResult = fasterDMA.getResult();
expect(fasterDMA.isStable).toBeTrue();
expect(fasterResult.short > fasterResult.long).toBeTrue();
});
});
});
41 changes: 36 additions & 5 deletions src/DMA/DMA.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import Big, {BigSource} from 'big.js';
import {MovingAverage, MovingAverageTypes, SMA} from '..';
import {Indicator} from '../Indicator';
import {FasterMovingAverage, MovingAverage} from '../MA/MovingAverage';
import {FasterMovingAverageTypes, MovingAverageTypes} from '../MA/MovingAverageTypes';
import {FasterSMA, SMA} from '../SMA/SMA';

export type DMAResult = {long: Big; short: Big};

export interface FasterDMAResult {
long: number;
short: number;
}

/**
* Dual Moving Average (DMA)
* Type: Trend
Expand All @@ -17,23 +24,21 @@ export type DMAResult = {long: Big; short: Big};
* @see https://faculty.fuqua.duke.edu/~charvey/Teaching/BA453_2002/CCAM/CCAM.htm#_Toc2634228
*/
export class DMA implements Indicator<DMAResult> {
public readonly long: MovingAverage;
public readonly short: MovingAverage;
private received: number = 0;
public readonly long: MovingAverage;

constructor(short: number, long: number, Indicator: MovingAverageTypes = SMA) {
this.short = new Indicator(short);
this.long = new Indicator(long);
}

get isStable(): boolean {
return this.received >= this.long.interval;
return this.long.isStable;
}

update(price: BigSource): void {
this.short.update(price);
this.long.update(price);
this.received += 1;
}

getResult(): DMAResult {
Expand All @@ -43,3 +48,29 @@ export class DMA implements Indicator<DMAResult> {
};
}
}

export class FasterDMA implements Indicator<FasterDMAResult, number> {
public readonly short: FasterMovingAverage;
public readonly long: FasterMovingAverage;

constructor(short: number, long: number, SmoothingIndicator: FasterMovingAverageTypes = FasterSMA) {
this.short = new SmoothingIndicator(short);
this.long = new SmoothingIndicator(long);
}

get isStable(): boolean {
return this.long.isStable;
}

update(price: number): void {
this.short.update(price);
this.long.update(price);
}

getResult(): FasterDMAResult {
return {
long: this.long.getResult(),
short: this.short.getResult(),
};
}
}
13 changes: 13 additions & 0 deletions src/start/startBenchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {AccelerationBands, FasterAccelerationBands} from '../ABANDS/Acceleration
import {FasterMOM, MOM} from '../MOM/MOM';
import {AO, FasterAO} from '../AO/AO';
import {AC, FasterAC} from '../AC/AC';
import {DMA, FasterDMA} from '../DMA/DMA';

const interval = 20;
const prices: number[] = candles.map(candle => parseInt(candle.close, 10));
Expand Down Expand Up @@ -119,6 +120,18 @@ new Benchmark.Suite('Technical Indicators')
fasterCCI.update(candle);
}
})
.add('DMA', () => {
const dma = new DMA(3, 6);
for (const price of prices) {
dma.update(price);
}
})
.add('FasterDMA', () => {
const fasterDMA = new FasterDMA(3, 6);
for (const price of prices) {
fasterDMA.update(price);
}
})
.add('DX', () => {
const dx = new DX(interval);
for (const candle of highLowCloses) {
Expand Down