persistable sclass MAIndicator extends CandleBasedIndicator 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) 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 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)); } }