// TODO: synchronize persistable sclass TickerSequence is IntSize { settable S market; // e.g. "TRBUSDT" // are we live? settable bool openEnded; // Is the data incomplete (still loading)? settable bool loading; new LongBuffer timestamps; new DoubleBuffer prices; event pricePointAdded(long timestamp); void addIfPriceChanged(double price, long timestamp) { if (isEmpty() || last(prices) != price) add(price, timestamp); } void add aka addPrice(double price, long timestamp) { timestamps.add(timestamp); prices.add(price); pricePointAdded(timestamp); } void add(double[] prices, long[] timestamps) { assertEquals(l(prices), l(timestamps)); // TODO: call pricePointAdded? this.prices.addAll(asList(prices)); this.timestamps.addAll(asList(timestamps)); } void add(TickerSequence ticker) { add(ticker.prices.toArray(), ticker.timestamps.toArray()); } toString { ret "TickerSequence from " + timeRange() + ", " + n2(l(prices), "price change"); } TickerSequence subSequence(int start, int end) { new TickerSequence s; s.add( subDoubleArray(prices.toArray(), start, end), subArray(timestamps.toArray(), start, end)); ret s; } TickerSequence cutOffAfterSeconds(double seconds) { int idx = indexOfTimestamp(startTime()+secondsToMS(seconds)); ret subSequence(0, idx); } TickerSequence subSequenceByTimestamps(long from, long to) { int idx1 = indexOfTimestamp(from); int idx2 = indexOfTimestamp(to); ret subSequence(idx1, idx2); } int indexOfTimestamp(double ts) { ret binarySearch_insertionPoint(timestamps.asVirtualList(), lround(ts)); } long nextTimestamp(double ts) { ret getTimestamp(indexOfTimestamp(ts)+1); } double priceAtTimestamp(double ts) { ret getPrice(indexOfTimestamp(ts)); } public int size() { ret l(prices); } public bool isEmpty() { ret size() == 0; } TimestampRange timeRange() { ret isEmpty() ? null : TimestampRange(first(timestamps), last(timestamps)); } long startTime() { ret first(timestamps); } long endTime() { ret last(timestamps); } double getPrice(int i) { ret prices.get(clampToLength(size(), i)); } long getTimestamp(int i) { ret timestamps.get(clampToLength(size(), i)); } TickerSequence removePlateaus() { new TickerSequence seq; seq.market(market); int n = size(); for i to n: { var value = getPrice(i); seq.prices.add(value); seq.timestamps.add(getTimestamp(i)); while (i+1 < n && getPrice(i+1) == value) ++i; } ret seq; } double minPrice() { ret doubleMin(prices.toArray()); } double maxPrice() { ret doubleMax(prices.toArray()); } double lastPrice() { ret empty(prices) ? Double.NaN : last(prices); } TickerPoint lastPoint() { ret new TickerPoint(this, endTime()); } }