Skip to content

Commit

Permalink
refactor!: Remove "getFixedArray" (#747)
Browse files Browse the repository at this point in the history
  • Loading branch information
bennycode authored Jan 13, 2025
1 parent 3f004b5 commit cd29068
Show file tree
Hide file tree
Showing 15 changed files with 33 additions and 74 deletions.
1 change: 0 additions & 1 deletion src/BBANDS/BollingerBands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {getFasterAverage, getFasterStandardDeviation, getStandardDeviation, push
* @see https://www.investopedia.com/terms/b/bollingerbands.asp
*/
export class BollingerBands extends TechnicalIndicator<BandsResult, BigSource> {
// TODO: Use "getFixedArray"
public readonly prices: Big[] = [];

/**
Expand Down
21 changes: 13 additions & 8 deletions src/CCI/CCI.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import Big from 'big.js';
import {BigIndicatorSeries, NumberIndicatorSeries} from '../Indicator.js';
import {getFixedArray, pushUpdate, type HighLowClose, type HighLowCloseNumber} from '../util/index.js';
import {FasterSMA, SMA} from '../SMA/SMA.js';
import {FasterMAD, MAD} from '../MAD/MAD.js';
import type {BigSource} from 'big.js';
import Big from 'big.js';
import {FasterSMA, SMA} from '../SMA/SMA.js';
import {pushUpdate, type HighLowClose, type HighLowCloseNumber} from '../util/index.js';

/**
* Commodity Channel Index (CCI)
Expand All @@ -30,20 +29,23 @@ import Big from 'big.js';
* @see https://en.wikipedia.org/wiki/Commodity_channel_index
*/
export class CCI extends BigIndicatorSeries<HighLowClose> {
public readonly prices: BigSource[] = [];
private readonly sma: SMA;
private readonly typicalPrices: Big[];

constructor(public readonly interval: number) {
super();
this.sma = new SMA(this.interval);
this.typicalPrices = getFixedArray(interval);
this.typicalPrices = [];
}

update(candle: HighLowClose, replace: boolean) {
const typicalPrice = this.cacheTypicalPrice(candle, replace);
this.sma.update(typicalPrice, replace);

if (this.typicalPrices.length > this.interval) {
this.typicalPrices.shift();
}

if (this.sma.isStable) {
const mean = this.sma.getResultOrThrow();
const meanDeviation = MAD.getResultFromBatch(this.typicalPrices, mean);
Expand All @@ -63,20 +65,23 @@ export class CCI extends BigIndicatorSeries<HighLowClose> {
}

export class FasterCCI extends NumberIndicatorSeries<HighLowCloseNumber> {
public readonly prices: number[] = [];
private readonly sma: FasterSMA;
private readonly typicalPrices: number[];

constructor(public readonly interval: number) {
super();
this.sma = new FasterSMA(this.interval);
this.typicalPrices = getFixedArray(interval);
this.typicalPrices = [];
}

update(candle: HighLowCloseNumber, replace: boolean) {
const typicalPrice = this.cacheTypicalPrice(candle, replace);
this.sma.update(typicalPrice, replace);

if (this.typicalPrices.length > this.interval) {
this.typicalPrices.shift();
}

if (this.sma.isStable) {
const mean = this.sma.getResultOrThrow();
const meanDeviation = FasterMAD.getResultFromBatch(this.typicalPrices, mean);
Expand Down
1 change: 0 additions & 1 deletion src/CG/CG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {pushUpdate} from '../util/pushUpdate.js';
*/
export class CG extends BigIndicatorSeries {
public signal: SMA;
// TODO: Use "getFixedArray"
public readonly prices: Big[] = [];

override get isStable(): boolean {
Expand Down
1 change: 0 additions & 1 deletion src/MACD/MACD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export type FasterMACDResult = {
* @see https://www.investopedia.com/terms/m/macd.asp
*/
export class MACD extends TechnicalIndicator<MACDResult, BigSource> {
// TODO: Use "getFixedArray"
public readonly prices: BigSource[] = [];
public readonly long: EMA | DEMA;
public readonly short: EMA | DEMA;
Expand Down
1 change: 0 additions & 1 deletion src/MAD/MAD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {getAverage, getFasterAverage, pushUpdate} from '../util/index.js';
* @see https://en.wikipedia.org/wiki/Average_absolute_deviation
*/
export class MAD extends BigIndicatorSeries {
// TODO: Use "getFixedArray"
public readonly prices: BigSource[] = [];

constructor(public readonly interval: number) {
Expand Down
13 changes: 10 additions & 3 deletions src/MOM/MOM.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type {BigSource} from 'big.js';
import Big from 'big.js';
import {BigIndicatorSeries, NumberIndicatorSeries} from '../Indicator.js';
import {getFixedArray} from '../util/getFixedArray.js';
import {pushUpdate} from '../util/pushUpdate.js';

/**
Expand All @@ -20,12 +19,16 @@ export class MOM extends BigIndicatorSeries {
constructor(public readonly interval: number) {
super();
this.historyLength = interval + 1;
this.history = getFixedArray<BigSource>(this.historyLength);
this.history = [];
}

update(value: BigSource, replace: boolean) {
pushUpdate(this.history, replace, value);

if (this.history.length > this.historyLength) {
this.history.shift();
}

if (this.history.length === this.historyLength) {
return this.setResult(new Big(value).minus(this.history[0]), replace);
}
Expand All @@ -41,12 +44,16 @@ export class FasterMOM extends NumberIndicatorSeries {
constructor(public readonly interval: number) {
super();
this.historyLength = interval + 1;
this.history = getFixedArray<number>(this.historyLength);
this.history = [];
}

update(value: number, replace: boolean) {
pushUpdate(this.history, replace, value);

if (this.history.length > this.historyLength) {
this.history.shift();
}

if (this.history.length === this.historyLength) {
return this.setResult(value - this.history[0], replace);
}
Expand Down
1 change: 0 additions & 1 deletion src/ROC/ROC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {pushUpdate} from '../util/pushUpdate.js';
* @see https://www.investopedia.com/terms/r/rateofchange.asp
*/
export class ROC extends BigIndicatorSeries {
// TODO: Use "getFixedArray"
public readonly prices: Big[] = [];

constructor(public readonly interval: number) {
Expand Down
1 change: 0 additions & 1 deletion src/RSI/RSI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {pushUpdate} from '../util/pushUpdate.js';
* @see https://www.investopedia.com/terms/r/rsi.asp
*/
export class RSI extends BigIndicatorSeries {
// TODO: Use "getFixedArray"
private readonly previousPrices: BigSource[] = [];
private readonly avgGain: MovingAverage;
private readonly avgLoss: MovingAverage;
Expand Down
1 change: 0 additions & 1 deletion src/SMA/SMA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import Big from 'big.js';
* @see https://www.investopedia.com/terms/s/sma.asp
*/
export class SMA extends MovingAverage {
// TODO: Use "getFixedArray"
public readonly prices: BigSource[] = [];

update(price: BigSource, replace: boolean) {
Expand Down
1 change: 0 additions & 1 deletion src/STOCH/StochasticOscillator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export interface FasterStochasticResult {
export class StochasticOscillator extends TechnicalIndicator<StochasticResult, HighLowClose> {
private readonly periodM: SMA;
private readonly periodP: SMA;
// TODO: Use "getFixedArray"
private readonly candles: HighLowClose[] = [];

/**
Expand Down
1 change: 0 additions & 1 deletion src/WMA/WMA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {pushUpdate} from '../util/pushUpdate.js';
* @see https://corporatefinanceinstitute.com/resources/career-map/sell-side/capital-markets/weighted-moving-average-wma/
*/
export class WMA extends MovingAverage {
// TODO: Use "getFixedArray"
public readonly prices: BigSource[] = [];

constructor(public override readonly interval: number) {
Expand Down
13 changes: 10 additions & 3 deletions src/util/Period.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type {BigSource} from 'big.js';
import Big from 'big.js';
import {TechnicalIndicator} from '../Indicator.js';
import {getFixedArray} from './getFixedArray.js';
import {getMaximum} from './getMaximum.js';
import {getMinimum} from './getMinimum.js';
import {pushUpdate} from './pushUpdate.js';
Expand Down Expand Up @@ -33,12 +32,16 @@ export class Period extends TechnicalIndicator<PeriodResult, BigSource> {

constructor(public readonly interval: number) {
super();
this.values = getFixedArray<Big>(interval);
this.values = [];
}

update(value: BigSource, replace: boolean) {
pushUpdate(this.values, replace, new Big(value));

if (this.values.length > this.interval) {
this.values.shift();
}

if (this.values.length === this.interval) {
this._lowest = getMinimum(this.values);
this._highest = getMaximum(this.values);
Expand Down Expand Up @@ -69,12 +72,16 @@ export class FasterPeriod extends TechnicalIndicator<FasterPeriodResult, number>

constructor(public readonly interval: number) {
super();
this.values = getFixedArray(interval);
this.values = [];
}

update(value: number, replace: boolean) {
pushUpdate(this.values, replace, value);

if (this.values.length > this.interval) {
this.values.shift();
}

if (this.values.length === this.interval) {
this._lowest = Math.min(...this.values);
this._highest = Math.max(...this.values);
Expand Down
33 changes: 0 additions & 33 deletions src/util/getFixedArray.test.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/util/getFixedArray.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from './BandsResult.js';
export * from './getAverage.js';
export * from './getFixedArray.js';
export * from './getLastFromForEach.js';
export * from './getMaximum.js';
export * from './getMinimum.js';
Expand Down

0 comments on commit cd29068

Please sign in to comment.