Skip to content

Commit 4c218a9

Browse files
authored
fix: Check long interval when using moving average crossover with EMA (#142)
1 parent b66368a commit 4c218a9

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/DMA/DMA.test.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('DMA', () => {
3030
});
3131

3232
describe('isStable', () => {
33-
it('is dependant on the long interval', () => {
33+
it('is dependant on the long interval (SMA)', () => {
3434
const dma = new DMA(3, 5);
3535
dma.update(40);
3636
dma.update(30);
@@ -40,6 +40,17 @@ describe('DMA', () => {
4040
dma.update(30);
4141
expect(dma.isStable).toBeTrue();
4242
});
43+
44+
it('is dependant on the long interval (EMA)', () => {
45+
const dma = new DMA(3, 5, EMA);
46+
dma.update(40);
47+
dma.update(30);
48+
dma.update(20);
49+
expect(dma.isStable).toBeFalse();
50+
dma.update(10);
51+
dma.update(30);
52+
expect(dma.isStable).toBeTrue();
53+
});
4354
});
4455

4556
describe('getResult', () => {

src/DMA/DMA.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@ export type DMAResult = {long: Big; short: Big};
77
export class DMA {
88
public readonly long: MovingAverage;
99
public readonly short: MovingAverage;
10+
private received: number = 0;
1011

1112
constructor(short: number, long: number, Indicator: typeof EMA | typeof SMA = SMA) {
1213
this.short = new Indicator(short);
1314
this.long = new Indicator(long);
1415
}
1516

1617
get isStable(): boolean {
17-
return this.long.isStable;
18+
return this.received >= this.long.interval;
1819
}
1920

2021
update(_price: BigSource): void {
2122
const price = new Big(_price);
22-
2323
this.short.update(price);
2424
this.long.update(price);
25+
this.received += 1;
2526
}
2627

2728
getResult(): DMAResult {

0 commit comments

Comments
 (0)