Libraryless. Click here for Pure Java version (10967L/61K).
1 | srecord noeq SmoothedMovingAverage(double period) { |
2 | selfType period(double period) { this.period = period; this; } |
3 | |
4 | gettable double value = Double.NaN; |
5 | gettable long steps; |
6 | |
7 | double get() { ret value; } |
8 | |
9 | double add(double x) { |
10 | ++steps; |
11 | if (isNaN(value)) |
12 | value = x; |
13 | else |
14 | value = blend(value, x, alpha()); |
15 | ret value; |
16 | } |
17 | |
18 | double alpha() { |
19 | ret reciprocal(period); |
20 | } |
21 | |
22 | void reset { |
23 | resetFields(this, "steps value"); |
24 | } |
25 | |
26 | bool complete() { |
27 | ret steps >= period; |
28 | } |
29 | } |
Began life as a copy of #1036438
download show line numbers debug dex old transpilations
Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1036444 |
Snippet name: | SmoothedMovingAverage - TODO: rename to ExponentialMovingAverage |
Eternal ID of this version: | #1036444/7 |
Text MD5: | 6edb6cce0f07f0e59fdd24e29e632edc |
Transpilation MD5: | d649167dbda1cd24e6b9f24f87021810 |
Author: | stefan |
Category: | javax / trading |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2023-04-12 10:17:11 |
Source code size: | 557 bytes / 29 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 178 / 256 |
Version history: | 6 change(s) |
Referenced in: | [show references] |