import { sum } from './utils' ; /**
 * A linearly weighted moving average (LWMA) is a moving average calculation that more heavily weights recent price data.
 * The most recent price has the highest weighting, and each prior price has progressively less weight.
 * The weights drop in a linear fashion.
 * LWMAs are quicker to react to price changes than simple moving averages (SMA) and exponential moving averages (EMA).
 */ export class LWMA { private arr : number [ ] = [ ] ; private filled = false ; private devider = 0 ; constructor ( private period ) { this . devider = sum ( Array . from ( Array ( this . period ) . keys ( ) ) . map ( ( i ) => i + 1 ) ) ; } nextValue ( value ) { this . filled = this . filled || this . arr . length === this . period ; this . arr . push ( value ) ; if ( this . filled ) { this . arr . shift ( ) ; return this . arr . reduce ( ( sum , value , idx ) => sum + value * ( idx + 1 ) , 0 ) / this . devider ; } } momentValue ( value ) { if ( this . filled ) { return this . arr . reduce ( ( sum , value , idx ) => sum + value * ( idx + 1 ) , 0 ) / this . devider ; } } }