Skip to content

Commit

Permalink
refactor(AC,AO): Expose internal indicators
Browse files Browse the repository at this point in the history
  • Loading branch information
bennycode committed Aug 5, 2021
1 parent 1515dde commit 7127370
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
13 changes: 7 additions & 6 deletions src/AC/AC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import {MOM} from '../MOM/MOM';
* change of momentum.
*/
export class AC implements SimpleIndicator {
public readonly ao: AO;
public readonly momentum: MOM;
private readonly ao: AO;
public readonly signal: SMA;

private result: Big | undefined;
private readonly signalSMA: SMA;

constructor(public readonly shortAO: number, public readonly longAO: number, public readonly signalInterval: number) {
this.ao = new AO(shortAO, longAO);
this.signalSMA = new SMA(signalInterval);
this.signal = new SMA(signalInterval);
this.momentum = new MOM(1);
}

Expand All @@ -40,9 +41,9 @@ export class AC implements SimpleIndicator {
this.ao.update(low, high);
if (this.ao.isStable) {
const ao = this.ao.getResult();
this.signalSMA.update(ao);
if (this.signalSMA.isStable) {
this.result = ao.sub(this.signalSMA.getResult());
this.signal.update(ao);
if (this.signal.isStable) {
this.result = ao.sub(this.signal.getResult());
this.momentum.update(this.result);
}
}
Expand Down
17 changes: 9 additions & 8 deletions src/AO/AO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import {NotEnoughDataError} from '../error';
* It has been developed by the technical analyst and charting enthusiast Bill Williams.
*/
export class AO implements SimpleIndicator {
public readonly long: SMA;
public readonly short: SMA;

private result: Big | undefined;
private readonly shortSMA: SMA;
private readonly longSMA: SMA;

constructor(public readonly shortInterval: number, public readonly longInterval: number) {
this.shortSMA = new SMA(shortInterval);
this.longSMA = new SMA(longInterval);
this.short = new SMA(shortInterval);
this.long = new SMA(longInterval);
}

get isStable(): boolean {
Expand All @@ -33,11 +34,11 @@ export class AO implements SimpleIndicator {
const candleSum = new Big(low).add(high);
const medianPrice = candleSum.div(2);

this.shortSMA.update(medianPrice);
this.longSMA.update(medianPrice);
this.short.update(medianPrice);
this.long.update(medianPrice);

if (this.shortSMA.isStable && this.longSMA.isStable) {
this.result = this.shortSMA.getResult().sub(this.longSMA.getResult());
if (this.short.isStable && this.long.isStable) {
this.result = this.short.getResult().sub(this.long.getResult());
}
}
}

0 comments on commit 7127370

Please sign in to comment.