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

57
LINES

< > BotCompany Repo | #1036559 // MAIndicator [simple moving average, trading indicator]

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

Transpiled version (19023L) is out of date.

persistable sclass MAIndicator extends CandleBasedIndicator<Double> is IntegrityCheckable {
  // The MA calculator
  settable SimpleMovingAverage ma;
  
  // MA values collected as ticker
  settableWithVar TickerSequence history = new TickerSequence("MA");
  
  // MA value
  double value = Double.NaN;
  
  // How many historic items to keep
  settable int maxHistoryLength = 1000;
  
  Double value() { ret value; }

  *(int *length) {}

  {
    length = 7;
    
    onCandleAdded((IVF1<TradingCandle>) candle -> {
      if (ma == null) makeMA();
      
      ma.add(candle.close());
      value = ma.complete() ? ma! : Double.NaN;
      if (!isNaN(value)) {
        long time = candle.endTime().toLong();
        history?.addIfPriceChanged(value, time);
        shortenHistory();
      }
    });
  }
  
  TickerSequence asTicker(L<TradingCandle> candles) {
    feed(candles);
    ret history;
  }
  
  void makeMA {
    ma = new SimpleMovingAverage(length);
  }
  
  void reset :: after {
    resetFields(this, "value history");
    makeMA();
  }
  
  public void integrityCheck {
    history?.integrityCheck();
  }
  
  void shortenHistory {
    int increment = 10;
    if (history != null && history.size() > maxHistoryLength+increment)
      history(history.subSequence(history.size()-maxHistoryLength));
  }
}

Author comment

Began life as a copy of #1036532

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1036559
Snippet name: MAIndicator [simple moving average, trading indicator]
Eternal ID of this version: #1036559/7
Text MD5: 3e11eeec306052ffb833566511b4d7e8
Author: stefan
Category: javax / trading
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-03-28 19:37:56
Source code size: 1372 bytes / 57 lines
Pitched / IR pitched: No / No
Views / Downloads: 167 / 250
Version history: 6 change(s)
Referenced in: #1003674 - Standard Classes + Interfaces (LIVE continued in #1034167)