Skip to content

Commit

Permalink
prepare tests for StochasticRSI
Browse files Browse the repository at this point in the history
  • Loading branch information
bennycode committed Dec 13, 2024
1 parent b8aec8e commit 702090e
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/ADX/ADX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class ADX extends BigIndicatorSeries<HighLowClose> {
return this.dx.pdi;
}

// TODO: Implement "replace" parameter
update(candle: HighLowClose): Big | void {
const result = this.dx.update(candle);
if (result) {
Expand Down
1 change: 1 addition & 0 deletions src/CCI/CCI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class CCI extends BigIndicatorSeries<HighLowClose> {
this.sma = new SMA(this.interval);
}

// TODO: Implement "replace" parameter
update(candle: HighLowClose): void | Big {
const typicalPrice = this.cacheTypicalPrice(candle);
this.sma.update(typicalPrice);
Expand Down
1 change: 1 addition & 0 deletions src/DX/DX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class DX extends BigIndicatorSeries<HighLowClose> {
this.previousCandle = candle;
}

// TODO: Implement "replace" parameter
update(candle: HighLowClose): Big | void {
if (!this.previousCandle) {
this.updateState(candle);
Expand Down
1 change: 1 addition & 0 deletions src/Indicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Indicator<Result = Big, Input = BigSource> {

isStable: boolean;

// TODO: Add "replace" parameter
update(input: Input): void | Result;

// TODO: Implement this function for all indicators, ensuring each returns "this.result"
Expand Down
34 changes: 22 additions & 12 deletions src/STOCH/StochasticRSI.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import {FasterStochasticRSI, StochasticRSI} from './StochasticRSI.js';

describe('StochasticRSI', () => {
describe('replace', () => {
it('guarantees that a replacement is done correctly', () => {
const interval = 2;
const stochRSI = new StochasticRSI(interval);
const stochRSIWithReplace = new StochasticRSI(interval);

stochRSI.updates([2, 2, 2, 2]);
stochRSIWithReplace.updates([2, 2, 2, 1]);
stochRSIWithReplace.replace(2);

expect(stochRSI.getResult().valueOf()).toBe(stochRSIWithReplace.getResult().valueOf());

Check failure on line 14 in src/STOCH/StochasticRSI.test.ts

View workflow job for this annotation

GitHub Actions / test-coverage / test-coverage

src/STOCH/StochasticRSI.test.ts > StochasticRSI > replace > guarantees that a replacement is done correctly

AssertionError: expected '100' to be '1' // Object.is equality - Expected + Received - 1 + 100 ❯ src/STOCH/StochasticRSI.test.ts:14:46
});
});

describe('getResult', () => {
it('calculates the Stochastic RSI', () => {
// Test data verified with:
Expand All @@ -9,8 +23,9 @@ describe('StochasticRSI', () => {
81.59, 81.06, 82.87, 83.0, 83.61, 83.15, 82.84, 83.99, 84.55, 84.36, 85.53, 86.54, 86.89, 87.77, 87.29,
];
const expectations = ['0.658', '1.000', '1.000', '1.000', '1.000', '0.000'];
const stochRSI = new StochasticRSI(5);
const fasterStochRSI = new FasterStochasticRSI(5);
const interval = 5;
const stochRSI = new StochasticRSI(interval);
const fasterStochRSI = new FasterStochasticRSI(interval);
for (const price of prices) {
const result = stochRSI.update(price);
const fasterResult = fasterStochRSI.update(price);
Expand All @@ -34,18 +49,13 @@ describe('StochasticRSI', () => {
});

it('catches division by zero errors', () => {
const stochRSI = new StochasticRSI(2);
stochRSI.update(2);
stochRSI.update(2);
stochRSI.update(2);
stochRSI.update(2);
const interval = 2;
const stochRSI = new StochasticRSI(interval);
stochRSI.updates([2, 2, 2, 2]);
expect(stochRSI.getResult().valueOf()).toBe('100');

const fasterStochRSI = new FasterStochasticRSI(2);
fasterStochRSI.update(2);
fasterStochRSI.update(2);
fasterStochRSI.update(2);
fasterStochRSI.update(2);
const fasterStochRSI = new FasterStochasticRSI(interval);
fasterStochRSI.updates([2, 2, 2, 2]);
expect(fasterStochRSI.getResult().valueOf()).toBe(100);
});
});
Expand Down
7 changes: 4 additions & 3 deletions src/STOCH/StochasticRSI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export class StochasticRSI extends BigIndicatorSeries {
this.rsi = new RSI(interval, SmoothingIndicator);
}

override update(price: BigSource, replace: boolean = false): void | Big {
// TODO: Implement "replace"
override update(price: BigSource): void | Big {
const rsiResult = this.rsi.update(price);
if (rsiResult) {
const periodResult = this.period.update(rsiResult);
Expand All @@ -43,10 +44,10 @@ export class StochasticRSI extends BigIndicatorSeries {
const denominator = max.minus(min);
// Prevent division by zero: https://github.com/bennycode/trading-signals/issues/378
if (denominator.eq(0)) {
return this.setResult(new Big(100), replace);
return this.setResult(new Big(100), false);
}
const numerator = rsiResult.minus(min);
return this.setResult(numerator.div(denominator), replace);
return this.setResult(numerator.div(denominator), false);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/WSMA/WSMA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class FasterWSMA extends NumberIndicatorSeries {
}

update(price: number, replace: boolean = false): number | void {
const sma = this.indicator.update(price);
const sma = this.indicator.update(price, replace);
if (replace && this.previousResult !== undefined) {
const smoothed = (price - this.previousResult) * this.smoothingFactor;
return this.setResult(smoothed + this.previousResult, replace);
Expand Down
2 changes: 2 additions & 0 deletions src/util/Period.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export class Period implements Indicator<PeriodResult> {
values.forEach(value => this.update(value));
}

// TODO: Implement "replace"
// Info: This may not work with "getFixedArray" as it shifts values out of our control
update(value: BigSource): PeriodResult | void {
this.values.push(new Big(value));
if (this.isStable) {
Expand Down

0 comments on commit 702090e

Please sign in to comment.