Skip to content

Commit

Permalink
feat: Expose read-only intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
bennycode committed Feb 8, 2021
1 parent 14a7d2f commit f849a8c
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 22 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@
"release:minor": "generate-changelog -m -x \"chore,docs,refactor,style,test\" && yarn changelog:commit && npm version minor",
"release:patch": "generate-changelog -p -x \"chore,docs,refactor,style,test\" && yarn changelog:commit && npm version patch",
"test": "nyc --nycrc-path=nyc.config.coverage.json jasmine --config=jasmine.json",
"test:dev": "nyc --nycrc-path=nyc.config.json jasmine --config=jasmine.json"
"test:dev": "nyc --nycrc-path=nyc.config.json jasmine --config=jasmine.json",
"test:types": "yarn lint:types"
},
"version": "1.4.0"
}
5 changes: 1 addition & 4 deletions src/ATR/ATR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ export type ATRCandle = {close: BigSource; high: BigSource; low: BigSource};
* waning interest.
*/
export class ATR {
private readonly interval: number;
private readonly smma: SMMA;
private readonly candles: ATRCandle[] = [];

private result: Big | undefined;
private prevCandle: ATRCandle | undefined;

constructor(interval: number) {
this.interval = interval;

constructor(public readonly interval: number) {
this.smma = new SMMA(interval);
}

Expand Down
6 changes: 2 additions & 4 deletions src/BANDS/AccelerationBands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export class AccelerationBands {
private readonly lowerBand: EMA | SMA;
private readonly middleBand: EMA | SMA;
private readonly upperBand: EMA | SMA;
private readonly width: Big;

/**
* Acceleration Bands
Expand All @@ -22,11 +21,10 @@ export class AccelerationBands {
* @see https://github.com/QuantConnect/Lean/blob/master/Indicators/AccelerationBands.cs
* @see https://github.com/twopirllc/pandas-ta/blob/master/pandas_ta/volatility/accbands.py
*/
constructor(period: number, width: number, Indicator: typeof EMA | typeof SMA = SMA) {
constructor(public readonly period: number, public readonly width: number, Indicator: typeof EMA | typeof SMA = SMA) {
this.lowerBand = new Indicator(period);
this.middleBand = new Indicator(period);
this.upperBand = new Indicator(period);
this.width = new Big(width);
}

get isStable(): boolean {
Expand All @@ -39,7 +37,7 @@ export class AccelerationBands {
}

update(high: BigSource, low: BigSource, close: BigSource): void {
const coefficient = this.width.mul(new Big(high).minus(low).div(new Big(high).plus(low)));
const coefficient = new Big(high).minus(low).div(new Big(high).plus(low)).mul(this.width);

// (Low * (1 - 4 * (High - Low)/ (High + Low)))
this.lowerBand.update(new Big(low).mul(new Big(1).minus(coefficient)));
Expand Down
6 changes: 1 addition & 5 deletions src/BANDS/BollingerBands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@ import {MathAnalysis} from '../util';
import {BandsResult} from './BandsResult';

export class BollingerBands {
public readonly interval: number;
public readonly deviationMultiplier: number;
private readonly middleSMA: SMA;
private readonly prices: Big[] = [];
private result: BandsResult | undefined;

constructor(interval: number = 0, deviationMultiplier: number = 2) {
this.interval = interval;
this.deviationMultiplier = deviationMultiplier;
constructor(public readonly interval: number = 0, public readonly deviationMultiplier: number = 2) {
this.middleSMA = new SMA(this.interval);
}

Expand Down
2 changes: 1 addition & 1 deletion src/DEMA/DEMA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class DEMA {
private readonly inner: EMA;
private readonly outer: EMA;

constructor(interval: number) {
constructor(public readonly interval: number) {
this.inner = new EMA(interval);
this.outer = new EMA(interval);
}
Expand Down
6 changes: 3 additions & 3 deletions src/MACD/MACD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export type MACDResult = {
};

export class MACD {
private readonly long: EMA | DEMA;
private readonly short: EMA | DEMA;
private readonly signal: EMA | DEMA;
public readonly long: EMA | DEMA;
public readonly short: EMA | DEMA;

private readonly signal: EMA | DEMA;
private age: number = 0;
private result: MACDResult | undefined;

Expand Down
3 changes: 1 addition & 2 deletions src/ROC/ROC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import Big, {BigSource} from 'big.js';
import {NotEnoughDataError} from '../error';

export class ROC {
private readonly interval: number;
private readonly priceHistory: Big[] = [];
private result: Big | undefined;

constructor(interval: number) {
constructor(public readonly interval: number) {
this.interval = interval;
}

Expand Down
2 changes: 1 addition & 1 deletion src/RSI/RSI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class RSI {
private readonly avgGain: MovingAverage;
private readonly avgLoss: MovingAverage;

constructor(private readonly interval: number, Indicator: typeof EMA | typeof SMMA = SMMA) {
constructor(public readonly interval: number, Indicator: typeof EMA | typeof SMMA = SMMA) {
this.avgGain = new Indicator(this.interval);
this.avgLoss = new Indicator(this.interval);
this.result = new Big(0);
Expand Down
2 changes: 1 addition & 1 deletion src/SMMA/SMMA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class SMMA extends MovingAverage {
private readonly prices: Big[] = [];
private readonly sma: SMA;

constructor(interval: number) {
constructor(public readonly interval: number) {
super(interval);
this.sma = new SMA(interval);
this.result = new Big(0);
Expand Down

0 comments on commit f849a8c

Please sign in to comment.