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

fix(macd): Ensure MACD histogram compatibility with Tulip Indicators #219

Merged
merged 1 commit into from
Apr 17, 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
50 changes: 47 additions & 3 deletions src/MACD/MACD.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('MACD', () => {
87.77,
87.29,
];

const expectedMacds = [
undefined,
undefined,
Expand All @@ -40,6 +41,43 @@ describe('MACD', () => {
'0.98',
'0.62',
];

const expectedMacdSignals = [
undefined,
undefined,
undefined,
undefined,
'0.62',
'0.56',
'0.47',
'0.46',
'0.49',
'0.47',
'0.51', // Note: Tulip indicators example (https://tulipindicators.org/macd) shows "0.52" because they are rounding "0.51492" up.
'0.60',
'0.66',
'0.72',
'0.70',
];

const expectedMacdHistograms = [
undefined,
undefined,
undefined,
undefined,
'0.00',
'-0.21',
'-0.36',
'-0.05',
'0.09',
'-0.05',
'0.17',
'0.33',
'0.24',
'0.26',
'-0.08',
];

const indicator = new MACD({
indicator: EMA,
longInterval: 5,
Expand All @@ -50,10 +88,16 @@ describe('MACD', () => {
for (const [index, input] of Object.entries(inputs)) {
indicator.update(input);

const expectedMacd = expectedMacds[parseInt(index, 10)];
const key = parseInt(index, 10);
const expectedMacd = expectedMacds[key]!;
const expectedMacdSignal = expectedMacdSignals[key]!;
const expectedMacdHistogram = expectedMacdHistograms[key]!;

if (expectedMacd !== undefined) {
const {macd} = indicator.getResult();
expect(macd.toFixed(2)).toBe(expectedMacd);
const result = indicator.getResult();
expect(result.macd.toFixed(2)).withContext('MACD').toBe(expectedMacd);
expect(result.signal.toFixed(2)).withContext('MACD Signal').toBe(expectedMacdSignal);
expect(result.histogram.toFixed(2)).withContext('MACD Histogram').toBe(expectedMacdHistogram);
}
}
});
Expand Down
31 changes: 16 additions & 15 deletions src/MACD/MACD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ export class MACD implements Indicator {
}

get isStable(): boolean {
return this.age >= Math.max(this.config.longInterval, this.config.shortInterval, this.config.signalInterval);
return this.age >= this.config.longInterval;
}

update(_price: BigSource): void {
const price = new Big(_price);

this.short.update(price);
this.long.update(price);
this.age++;

const shortEMA = this.short.getResult();
const longEMA = this.long.getResult();
Expand All @@ -47,30 +48,30 @@ export class MACD implements Indicator {
* Standard MACD is the short (usually 12 periods) EMA less the long (usually 26 periods) EMA. Closing prices are
* used to form the moving averages.
*/
const diff = shortEMA.sub(longEMA);

/**
* A short (usually 9 periods) EMA of MACD is plotted along side to act as a signal line to identify turns in the
* indicator.
*/
this.signal.update(diff);
const macd = shortEMA.sub(longEMA);

if (this.isStable) {
/**
* A short (usually 9 periods) EMA of MACD is plotted along side to act as a signal line to identify turns in the
* indicator. It gets updated once the long EMA has enough input data.
*/
this.signal.update(macd);
}

const signal = this.signal.getResult();
const signal = this.isStable ? this.signal.getResult() : new Big(0);

/**
* The MACD-Histogram represents the difference between MACD and its 9-day EMA, the signal line.
* The MACD histogram is calculated as the MACD indicator minus the signal line (usually 9 periods) EMA.
*/
this.result = {
histogram: diff.sub(signal),
macd: diff,
histogram: macd.sub(signal),
macd: macd,
signal,
};

this.age++;
}

getResult(): MACDResult {
if (!this.result) {
if (!this.isStable || !this.result) {
throw new NotEnoughDataError();
}

Expand Down