forked from coin-unknown/Indicators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathewma.ts
32 lines (28 loc) · 1.28 KB
/
ewma.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* The Exponentially Weighted Moving Average (EWMA) is a quantitative or statistical measure used to model or describe
* a time series. The EWMA is widely used in finance, the main applications being technical analysis and volatility modeling.
* The moving average is designed as such that older observations are given lower weights.
* The weights fall exponentially as the data point gets older – hence the name exponentially weighted.
* The only decision a user of the EWMA must make is the parameter alpha.
* The parameter decides how important the current observation is in the calculation of the EWMA.
* The higher the value of alpha, the more closely the EWMA tracks the original time series.
* @param alpha must be from 0 to 1
*/
export class EWMA {
private prevValue: number;
private filled = false;
constructor(private alpha = 0.2) {}
nextValue(value: number) {
const ewma = this.alpha * value + (1 - this.alpha) * (this.prevValue || 1);
this.filled = this.filled || this.prevValue !== undefined;
this.prevValue = ewma;
if (this.filled) {
return ewma;
}
}
momentValue(value: number) {
if (this.filled) {
return this.alpha * value + (1 - this.alpha) * (this.prevValue || 1);
}
}
}