Skip to content

Commit

Permalink
filled bbands
Browse files Browse the repository at this point in the history
  • Loading branch information
BusinessDuck committed Feb 2, 2021
1 parent 2bd55c0 commit c95a09f
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/bands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { StandardDeviation } from './standard-deviation';
export class BollingerBands {
private sd: StandardDeviation;
private sma: SMA;
private filled = false;

constructor(period = 20, private stdDev: number = 2) {
this.sma = new SMA(period);
Expand All @@ -12,13 +13,24 @@ export class BollingerBands {
nextValue(close: number) {
const middle = this.sma.nextValue(close);
const sd = this.sd.nextValue(close, middle);

this.filled = this.filled || !!(sd && middle);

if (!this.filled) {
return;
}

const lower = middle - this.stdDev * sd;
const upper = middle + this.stdDev * sd;

return { lower, middle, upper };
}

momentValue(close: number) {
if (!this.filled) {
return;
}

const middle = this.sma.momentValue(close);
const sd = this.sd.momentValue(close, middle);
const lower = middle - this.stdDev * sd;
Expand Down

0 comments on commit c95a09f

Please sign in to comment.