persistable sclass TradingCandleMaker { settable double candleLength = 1; // in minutes settable int maxCandles = Int.MAX_VALUE; settable double alignment; // shift in seconds *(double *candleLength) {} L candles = syncL(); TradingCandle currentCandle() { ret last(candles); } Timestamp endTime() { var candle = last(candles); ret candle?.endTime(); } double lastPrice() { ret currentCandle().end(); } void add(double price, long timestamp) { var ts = new Timestamp(timestamp); int safety = 0; while (currentCandle() != null) { if (safety++ >= 1000) { warn("TradingCandleMaker safety warning"); break; } Timestamp maxEnd = maxEndOfCurrentCandle(); if (!greaterOrEq(ts, maxEnd)) break; currentCandle().endTime(maxEnd); double lastPrice = lastPrice(); newCandle(); currentCandle().addValue(lastPrice, maxEnd); } if (empty(candles)) newCandle(); currentCandle().addValue(price, ts); } void newCandle { candles.add(new TradingCandle); dropOldCandles(); } 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); } Timestamp maxEndOfCurrentCandle() { long time = currentCandle().startTime.toLong(); long align = secondsToMS(alignment); time += roundUpTo(time+align, secondsToMS(candleLength*60))-align; ret new Timestamp(time); } /*bool needNewCandle(long timestamp) { ret empty(candles) || timestamp >= maxEndOfCurrentCandle().unixDate(); }*/ void dropOldCandles { if (l(candles) > maxCandles) removeSubList(candles, 0, l(candles)-maxCandles); } L candles() { ret cloneList(candles); } }