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

persistable sclass SMAIndicator extends CandleBasedIndicator<Double> {
  // The SMA calculator
  settable SmoothedMovingAverage sma;
  
  // SMA values collected as ticker
  gettable TickerSequence history = new TickerSequence("SMA");
  
  // SMA value
  double value = Double.NaN;
  
  Double value() { ret value; }

  *(int *length) {}

  {
    length = 7;
    
    onCandleAdded((IVF1<TradingCandle>) candle -> {
      if (sma == null) sma = new SmoothedMovingAverage(length);
      
      sma.add(candle.close());
      value = sma.complete() ? sma! : Double.NaN;
      if (!isNaN(value)) {
        long time = candle.endTime().toLong();
        history?.addIfPriceChanged(value, time);
      }
    });
  }
  
  TickerSequence asTicker(L<TradingCandle> candles) {
    feed(candles);
    ret history;
  }
  
  void reset :: after {
    resetFields(this, "value history");
    sma?.reset();
  }
}

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: 186 / 1096
Version history: 4 change(s)
Referenced in: #1003674 - Standard Classes + Interfaces (LIVE continued in #1034167)
#1036559 - MAIndicator [simple moving average, trading indicator]