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

27
LINES

< > BotCompany Repo | #1036459 // SimpleMovingAverage

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

Libraryless. Click here for Pure Java version (10692L/60K).

srecord noeq SimpleMovingAverage(int length) {
  selfType length(int length) { this.length = length; this; }
  
  simplyCached SimpleCircularBuffer<Double> values() {
    ret new SimpleCircularBuffer(length);
  }
  
  new Average avg;
  
  double get() { ret avg!; }
  
  double add(double x) {
    while (values().size() > max(length-1, 0))
      avg.remove(values().remove());
    values().add(x);
    avg.add(x);
    ret get();
  }
  
  void reset {
    resetFields(this, "avg");
  }
  
  bool complete() {
    ret avg.n() >= length;
  }
}

Author comment

Began life as a copy of #1036444

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036459
Snippet name: SimpleMovingAverage
Eternal ID of this version: #1036459/5
Text MD5: cc2838752a9473ebfc30bef867d7969f
Transpilation MD5: 94405453994bc7c8d96c94ed5cfe2242
Author: stefan
Category: javax / trading
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-02-21 13:26:12
Source code size: 568 bytes / 27 lines
Pitched / IR pitched: No / No
Views / Downloads: 200 / 272
Version history: 4 change(s)
Referenced in: #1003674 - Standard Classes + Interfaces (LIVE continued in #1034167)