// TODO: synchronize persistable sclass TickerSequence is IntSize { settable S market; // e.g. "TRBUSDT" // are we live? settable bool live; // Is the data incomplete (still loading)? settable bool loading; new LongBuffer timestamps; new DoubleBuffer prices; // timestamp of last ticker event even though price may not // have changed settable long lastTickerEvent; event pricePointAdded(long timestamp); void addIfPriceChanged(double price, long timestamp) { lastTickerEvent = max(lastTickerEvent, timestamp); if (isEmpty() || last(prices) != price) add(price, timestamp); } void add aka addPrice(double price, long timestamp) { lastTickerEvent = max(lastTickerEvent, timestamp); timestamps.add(timestamp); prices.add(price); pricePointAdded(timestamp); } void add(double[] prices, long[] timestamps) { assertEquals(l(prices), l(timestamps)); if (empty(prices)) ret; // TODO: call pricePointAdded? this.prices.addAll(asList(prices)); this.timestamps.addAll(asList(timestamps)); lastTickerEvent = max(lastTickerEvent, last(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 firstPrice() { ret empty(prices) ? Double.NaN : first(prices); } double lastPrice() { ret empty(prices) ? Double.NaN : last(prices); } TickerPoint lastPoint() { ret new TickerPoint(this, endTime()); } // parse line from a .ticker file void addJSONLine(S line) { Map data = parseJSONMap(line); double price = toDouble(data.get("bestAsk")); long time = toLong(data.get("systemTime")); addIfPriceChanged(price, time); } }