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).

1  
srecord noeq SimpleMovingAverage(int length) {
2  
  selfType length(int length) { this.length = length; this; }
3  
  
4  
  simplyCached SimpleCircularBuffer<Double> values() {
5  
    ret new SimpleCircularBuffer(length);
6  
  }
7  
  
8  
  new Average avg;
9  
  
10  
  double get() { ret avg!; }
11  
  
12  
  double add(double x) {
13  
    while (values().size() > max(length-1, 0))
14  
      avg.remove(values().remove());
15  
    values().add(x);
16  
    avg.add(x);
17  
    ret get();
18  
  }
19  
  
20  
  void reset {
21  
    resetFields(this, "avg");
22  
  }
23  
  
24  
  bool complete() {
25  
    ret avg.n() >= length;
26  
  }
27  
}

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: 199 / 272
Version history: 4 change(s)
Referenced in: [show references]