From c95a09fccbd6e26db4aa5eca6d6bcd86bc770ec7 Mon Sep 17 00:00:00 2001 From: Yurov Dmitry Date: Tue, 2 Feb 2021 20:18:59 +0300 Subject: [PATCH] filled bbands --- src/bands.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/bands.ts b/src/bands.ts index 9f6778c..f9726ae 100644 --- a/src/bands.ts +++ b/src/bands.ts @@ -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); @@ -12,6 +13,13 @@ 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; @@ -19,6 +27,10 @@ export class BollingerBands { } 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;