import { assert } from 'chai' ; import { ISeries , Series , DataFrame , IDataFrame } from 'data-forge' ; /**
 * A record in the output bollinger bands dataframe.
 */ export interface IBollingerBand { /**
     * The value that the bollinger bands are derived from.
     */ value : number ; /**
     * Middle value in the bollinger band, the average value for the particular period.
     */ middle : number ; /***
     * The upper value. middle + (standard deviations x X).
     */ upper : number ; /***
     * The upper value. middle - (standard deviations x X).
     */ lower : number ; } declare module "data-forge/build/lib/series" { interface ISeries < IndexT , ValueT > { bollinger ( period , stdDevMultUpper , stdDevMultLower ) : IDataFrame < any , IBollingerBand > ; } interface Series < IndexT , ValueT > { bollinger ( period , stdDevMultUpper , stdDevMultLower ) : IDataFrame < any , IBollingerBand > ; } } param period param stdDevMultUpper param stdDevMultLower returns function bollinger < IndexT = any > ( this , period , stdDevMultUpper , stdDevMultLower ) { assert . isNumber ( period , "Expected 'period' parameter to 'Series.bollinger' to be a number that specifies the time period of the moving average." ) ; assert . isNumber ( stdDevMultUpper , "Expected 'stdDevMultUpper' parameter to 'Series.bollinger' to be a number that specifies multipler to compute the upper band from the standard deviation." ) ; assert . isNumber ( stdDevMultLower , "Expected 'stdDevMultLower' parameter to 'Series.bollinger' to be a number that specifies multipler to compute the upper band from the standard deviation." ) ; return this . rollingWindow ( period ) . select < [ IndexT , IBollingerBand ] > ( window => { const avg = window . average ( ) ; const stddev = window . std ( ) ; var bollingerRecord = { value : window . last ( ) , middle : avg , upper : avg + ( stddev * stdDevMultUpper ) , lower : avg - ( stddev * stdDevMultLower ) , } ; return [ window . getIndex ( ) . last ( ) , bollingerRecord ] ; } ) . withIndex ( pair => pair [ 0 ] ) . inflate ( pair => pair [ 1 ] ) ; } ; Series . prototype . bollinger = bollinger ;