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

39
LINES

< > BotCompany Repo | #1036532 // SMAIndicator [smoothed moving average, trading indicator]

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

Libraryless. Click here for Pure Java version (17813L/102K).

1  
persistable sclass SMAIndicator extends CandleBasedIndicator<Double> {
2  
  // The SMA calculator
3  
  settable SmoothedMovingAverage sma;
4  
  
5  
  // SMA values collected as ticker
6  
  gettable TickerSequence history = new TickerSequence("SMA");
7  
  
8  
  // SMA value
9  
  double value = Double.NaN;
10  
  
11  
  Double value() { ret value; }
12  
13  
  *(int *length) {}
14  
15  
  {
16  
    length = 7;
17  
    
18  
    onCandleAdded((IVF1<TradingCandle>) candle -> {
19  
      if (sma == null) sma = new SmoothedMovingAverage(length);
20  
      
21  
      sma.add(candle.close());
22  
      value = sma.complete() ? sma! : Double.NaN;
23  
      if (!isNaN(value)) {
24  
        long time = candle.endTime().toLong();
25  
        history?.addIfPriceChanged(value, time);
26  
      }
27  
    });
28  
  }
29  
  
30  
  TickerSequence asTicker(L<TradingCandle> candles) {
31  
    feed(candles);
32  
    ret history;
33  
  }
34  
  
35  
  void reset :: after {
36  
    resetFields(this, "value history");
37  
    sma?.reset();
38  
  }
39  
}

Author comment

Began life as a copy of #1036488

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1036532
Snippet name: SMAIndicator [smoothed moving average, trading indicator]
Eternal ID of this version: #1036532/5
Text MD5: 3ac2553d6c3be02a690c7621350cb469
Transpilation MD5: 6629d1c2f1bf81f80e091c11c086a163
Author: stefan
Category: javax / trading
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-01-24 15:02:39
Source code size: 936 bytes / 39 lines
Pitched / IR pitched: No / No
Views / Downloads: 187 / 1098
Version history: 4 change(s)
Referenced in: [show references]