Skip to content

Commit

Permalink
adx & wema moment values and new specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy Yurov committed May 1, 2023
1 parent 2904937 commit eff7147
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 22 deletions.
4 changes: 0 additions & 4 deletions src/adx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ export class ADX {
const avgPDI = this.wma2.momentValue(pDM);
const avgNDI = this.wma3.momentValue(nDM);

this.prevHigh = h;
this.prevLow = l;
this.prevClose = c;

if (avgPDI === undefined || avgNDI === undefined) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/wema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class WEMA {
*/
momentValue(value: number) {
if (!this.wema) {
return;
return this.sma.momentValue(value);
}

return (value - this.wema) * this.smooth + this.wema;
Expand Down
21 changes: 9 additions & 12 deletions src/wma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,21 @@ export class WMA {
* does not affect any next calculations
*/
momentValue(value: number) {
const buffer = new CircularBuffer(this.buffer.length);
const removed = this.buffer.push(value);

this.buffer.forEach((v) => {
if (typeof v === 'number') {
buffer.push(v);
}
});
buffer.push(value);

if (!buffer.filled) {
if (!this.buffer.filled) {
this.buffer.pushback(removed);
return
}


let result = 0

buffer.forEach((v, idx) => {
result += (v * (Number(idx) + 1)) / this.denominator
})
this.buffer.forEach((v, idx) => {
result += (v * (idx + 1)) / this.denominator;
});

this.buffer.pushback(removed);

return result
}
Expand Down
6 changes: 5 additions & 1 deletion src/wws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export class WWS {
* does not affect any next calculations
*/
momentValue(value: number) {
return this.prevValue + (value - this.prevValue) / this.period;
if (this.sumCount < this.period) {
return;
}

return this.prevValue - this.prevValue / this.period + value;
}
}
8 changes: 4 additions & 4 deletions tests/adx/adx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ describe('ADX', () => {
// @ts-ignore typing error?
const cross = adx2.nextValue({ high: tick.h, low: tick.l, close: tick.c });

if (local && cross) {
if (local && cross && localMoment) {
expect(local.adx).toEqual(cross.adx);
expect(local.mdi).toEqual(cross.mdi);
expect(local.pdi).toEqual(cross.pdi);
expect(localMoment?.adx).toEqual(local.adx);
expect(localMoment?.mdi).toEqual(local.mdi);
expect(localMoment?.pdi).toEqual(local.pdi);
expect(localMoment.adx).toEqual(local.adx);
expect(localMoment.mdi).toEqual(local.mdi);
expect(localMoment.pdi).toEqual(local.pdi);
}
});
});
Expand Down

0 comments on commit eff7147

Please sign in to comment.