@@ -11,6 +11,11 @@ export default class ScriptStd {
11
11
this . SWMA = [ 1 / 6 , 2 / 6 , 2 / 6 , 1 / 6 ]
12
12
}
13
13
14
+ // Generate the next timeseries id
15
+ _tsid ( prev , next ) {
16
+ return `${ prev } <-${ next } `
17
+ }
18
+
14
19
// Wait for a value !== undefined
15
20
nw ( x ) {
16
21
if ( x === undefined || x !== x ) {
@@ -45,23 +50,17 @@ export default class ScriptStd {
45
50
46
51
// Creates a new time-series & records each x.
47
52
// Return the an array. Id is auto-genrated
48
- ts ( x , id ) {
49
- let ts = this . env . tss [ id ]
53
+ ts ( x , _id ) {
54
+ let ts = this . env . tss [ _id ]
50
55
if ( ! ts ) {
51
- ts = this . env . tss [ id ] = [ x ]
52
- ts . __id__ = id
56
+ ts = this . env . tss [ _id ] = [ x ]
57
+ ts . __id__ = _id
53
58
} else {
54
59
ts [ 0 ] = x
55
60
}
56
61
return ts
57
62
}
58
63
59
- // Generate the next timeseries id
60
- _tsid ( args , next ) {
61
- let prev = args [ args . length - 1 ]
62
- return `${ prev } <-${ next } `
63
- }
64
-
65
64
abs ( x ) {
66
65
return Math . abs ( x )
67
66
}
@@ -195,8 +194,14 @@ export default class ScriptStd {
195
194
// TODO: this
196
195
}
197
196
198
- ema ( ) {
199
- // TODO: this
197
+ ema ( src , len , _id ) {
198
+ let id = this . _tsid ( _id , `ema(${ len } )` )
199
+ let a = 2 / ( len + 1 )
200
+ let ema = this . ts ( id , 0 )
201
+ ema [ 0 ] = this . na ( ema [ 1 ] ) ?
202
+ this . sma ( src , len , id ) [ 0 ] :
203
+ a * src [ 0 ] + ( 1 - a ) * this . nz ( ema [ 1 ] )
204
+ return ema
200
205
}
201
206
202
207
exp ( x ) {
@@ -215,8 +220,8 @@ export default class ScriptStd {
215
220
Math . floor ( x )
216
221
}
217
222
218
- highest ( src , len ) {
219
- let id = this . _tsid ( arguments , `highest(${ len } )` )
223
+ highest ( src , len , _id ) {
224
+ let id = this . _tsid ( _id , `highest(${ len } )` )
220
225
let high = - Infinity
221
226
for ( var i = 0 ; i < len ; i ++ ) {
222
227
if ( src [ i ] > high ) high = src [ i ]
@@ -260,8 +265,8 @@ export default class ScriptStd {
260
265
Math . log10 ( x )
261
266
}
262
267
263
- lowest ( src , len ) {
264
- let id = this . _tsid ( arguments , `lowest(${ len } )` )
268
+ lowest ( src , len , _id ) {
269
+ let id = this . _tsid ( _id , `lowest(${ len } )` )
265
270
let low = Infinity
266
271
for ( var i = 0 ; i < len ; i ++ ) {
267
272
if ( src [ i ] < low ) low = src [ i ]
@@ -372,8 +377,8 @@ export default class ScriptStd {
372
377
return Math . sin ( x )
373
378
}
374
379
375
- sma ( src , len ) {
376
- let id = this . _tsid ( arguments , `sma(${ len } )` )
380
+ sma ( src , len , _id ) {
381
+ let id = this . _tsid ( _id , `sma(${ len } )` )
377
382
let sum = 0
378
383
for ( var i = 0 ; i < len ; i ++ ) {
379
384
sum = sum + src [ i ]
@@ -393,8 +398,8 @@ export default class ScriptStd {
393
398
// TODO: this
394
399
}
395
400
396
- sum ( src , len ) {
397
- let id = this . _tsid ( arguments , `sum(${ len } )` )
401
+ sum ( src , len , _id ) {
402
+ let id = this . _tsid ( _id , `sum(${ len } )` )
398
403
let sum = 0
399
404
for ( var i = 0 ; i < len ; i ++ ) {
400
405
sum = sum + src [ i ]
@@ -406,8 +411,8 @@ export default class ScriptStd {
406
411
// TODO: this
407
412
}
408
413
409
- swma ( src ) {
410
- let id = this . _tsid ( arguments , `swma` )
414
+ swma ( src , _id ) {
415
+ let id = this . _tsid ( _id , `swma` )
411
416
let sum = src [ 3 ] * this . SWMA [ 0 ] + src [ 2 ] * this . SWMA [ 1 ] +
412
417
src [ 1 ] * this . SWMA [ 2 ] + src [ 0 ] * this . SWMA [ 3 ]
413
418
return this . ts ( sum , id )
@@ -454,8 +459,17 @@ export default class ScriptStd {
454
459
// TODO: this
455
460
}
456
461
457
- wma ( src , len ) {
458
- // TODO: this
462
+ wma ( src , len , _id ) {
463
+ // TODO: not precise
464
+ let id = this . _tsid ( _id , `wma(${ len } )` )
465
+ let norm = 0
466
+ let sum = 0
467
+ for ( var i = 0 ; i < len - 1 ; i ++ ) {
468
+ let w = ( len - i ) * len
469
+ norm += w
470
+ sum += src [ i ] * w
471
+ }
472
+ return this . ts ( sum / norm , id )
459
473
}
460
474
461
475
wpr ( len ) {
0 commit comments