@@ -3,7 +3,7 @@ import {PointValue} from "../../../models/run"
3
3
4
4
export class TwoSidedExponentialAverage extends SeriesSmoothing {
5
5
protected smooth ( ) : void {
6
- let smoothingFactor = 1 - this . smoothValue / 100
6
+ let smoothingFactor = this . smoothValue / 100
7
7
8
8
for ( let i = 0 ; i < this . indicators . length ; i ++ ) {
9
9
let ind = this . indicators [ i ]
@@ -13,27 +13,45 @@ export class TwoSidedExponentialAverage extends SeriesSmoothing {
13
13
}
14
14
15
15
let result : PointValue [ ] = [ ]
16
- let forward_pass : number [ ] = [ ]
17
- let lastSmoothed = ind . series [ 0 ] . value
16
+
17
+ let f_sum : number [ ] = [ ]
18
+ let b_sum : number [ ] = [ ]
19
+ let f_weights : number [ ] = [ ]
20
+ let b_weights : number [ ] = [ ]
21
+
22
+ let last_sum = 0
23
+ let last_weight = 0
24
+ let last_step = ind . series [ 0 ] . step
18
25
for ( let j = 0 ; j < ind . series . length ; j ++ ) {
19
- let smoothed = lastSmoothed * ( 1 - smoothingFactor ) + ind . series [ j ] . value * smoothingFactor
20
- forward_pass . push ( smoothed )
21
- lastSmoothed = smoothed
26
+ let smooth_gap = Math . pow ( smoothingFactor , Math . abs ( ind . series [ j ] . step - last_step ) )
27
+ f_sum . push ( last_sum * smooth_gap + ind . series [ j ] . value )
28
+ f_weights . push ( last_weight * smooth_gap + 1 )
29
+
30
+ last_sum = f_sum [ j ]
31
+ last_weight = f_weights [ j ]
32
+ last_step = ind . series [ j ] . step
22
33
}
23
34
24
- let backward_pass : number [ ] = [ ]
25
- lastSmoothed = ind . series [ ind . series . length - 1 ] . value
35
+ last_sum = 0
36
+ last_weight = 0
37
+ last_step = ind . series [ ind . series . length - 1 ] . step
26
38
for ( let j = ind . series . length - 1 ; j >= 0 ; j -- ) {
27
- let smoothed = lastSmoothed * ( 1 - smoothingFactor ) + ind . series [ j ] . value * smoothingFactor
28
- backward_pass . push ( smoothed )
29
- lastSmoothed = smoothed
39
+ let smooth_gap = Math . pow ( smoothingFactor , Math . abs ( ind . series [ j ] . step - last_step ) )
40
+ b_sum . push ( last_sum * smooth_gap + ind . series [ j ] . value )
41
+ b_weights . push ( last_weight * smooth_gap + 1 )
42
+
43
+ last_sum = b_sum [ b_sum . length - 1 ]
44
+ last_weight = b_weights [ b_weights . length - 1 ]
45
+ last_step = ind . series [ j ] . step
30
46
}
31
- backward_pass = backward_pass . reverse ( )
47
+ b_weights . reverse ( )
48
+ b_sum . reverse ( )
32
49
33
50
for ( let j = 0 ; j < ind . series . length ; j ++ ) {
34
- let smoothed = ( forward_pass [ j ] + backward_pass [ j ] ) / 2
35
- result . push ( { step : ind . series [ j ] . step , value : ind . series [ j ] . value , smoothed : smoothed ,
36
- lastStep : ind . series [ j ] . lastStep } )
51
+ let smoothed = ( f_sum [ j ] + b_sum [ j ] - ind . series [ j ] . value ) /
52
+ ( f_weights [ j ] + b_weights [ j ] - 1 )
53
+ result . push ( { step : ind . series [ j ] . step , value : ind . series [ j ] . value ,
54
+ smoothed : smoothed , lastStep : ind . series [ j ] . lastStep } )
37
55
}
38
56
39
57
ind . series = result
0 commit comments