persistable sclass TradingCandleMaker { settable double candleLength = 1; // in minutes settable int maxCandles = Int.MAX_VALUE; *(double *candleLength) {} L candles = syncL(); TradingCandle currentCandle() { ret last(candles); } Timestamp endTime() { var candle = last(candles); ret candle?.endTime(); } void add(double price, long timestamp) { if (currentCandle() != null) currentCandle().addValue(price, new Timestamp(timestamp)); if (needNewCandle(timestamp)) { candles.add(new TradingCandle); dropOldCandles(); currentCandle().addValue(price, new Timestamp(timestamp)); } } void addTickerSequence aka feed(TickerSequence ts) { Timestamp endTime = endTime(); if (endTime != null) ts = ts.subSequenceFromTimestamp(endTime().unixDate()+1); int n = l(ts); for i to n: add(ts.prices.get(i), ts.timestamps.get(i)); if (ts.lastTickerEvent != 0) add(ts.lastPrice(), ts.lastTickerEvent); } bool needNewCandle(long timestamp) { ret empty(candles) || timestamp >= currentCandle().startTime.plusSeconds(candleLength*60).unixDate(); } void dropOldCandles { if (l(candles) > maxCandles) removeSubList(candles, 0, l(candles)-maxCandles); } L candles() { ret cloneList(candles); } }