Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

29
LINES

< > BotCompany Repo | #1036444 // SmoothedMovingAverage - TODO: rename to ExponentialMovingAverage

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (10967L/61K).

srecord noeq SmoothedMovingAverage(double period) {
  selfType period(double period) { this.period = period; this; }
  
  gettable double value = Double.NaN;
  gettable long steps;
  
  double get() { ret value; }
  
  double add(double x) {
    ++steps;
    if (isNaN(value))
      value = x;
    else
      value = blend(value, x, alpha());
    ret value;
  }
  
  double alpha() {
    ret reciprocal(period);
  }
  
  void reset {
    resetFields(this, "steps value");
  }
  
  bool complete() {
    ret steps >= period;
  }
}

Author comment

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: 179 / 256
Version history: 6 change(s)
Referenced in: #1003674 - Standard Classes + Interfaces (LIVE continued in #1034167)
#1036459 - SimpleMovingAverage