Minute Candle Aggregation
EasyArrays · Robinhood
A charting service turns raw trades into one-minute OHLCV candles. Each trade is [timestamp, price, quantity], where timestamp is in seconds. Trades can arrive out of order, so the list is not sorted by timestamp.
A trade belongs to minute timestamp // 60 (so seconds 0-59 are minute 0, 60-119 are minute 1, and so on). For every minute that has at least one trade, build the row [minute, open, high, low, close, volume]:
open: price of the trade with the smallest timestamp in that minute. If several trades share that timestamp, use the one that appears first in the input.close: price of the trade with the largest timestamp in that minute. If several trades share that timestamp, use the one that appears last in the input.high/low: the maximum / minimum price in that minute.volume: the sum of the quantities in that minute.
Return the rows sorted by minute ascending. Minutes with no trades produce no row; return an empty list when there are no trades.
Example 1
- Input:
- trades = [[65, 101, 3], [10, 100, 5], [59, 98, 2], [70, 104, 1], [61, 103, 4]]
- Output:
- [[0, 100, 100, 98, 98, 7], [1, 103, 104, 101, 104, 8]]
- Explanation
- Minute 0 holds the trades at 10 and 59: it opens at 100 (earliest), closes at 98 (latest), and has volume 5 + 2 = 7. Minute 1 holds the trades at 61, 65 and 70: open 103, high 104, low 101, close 104 and volume 8. The input order does not matter; timestamps decide open and close.
Example 2
- Input:
- trades = [[125, 50, 1], [125, 52, 2], [130, 49, 1]]
- Output:
- [[2, 50, 52, 49, 49, 4]]
- Explanation
- All three trades fall in minute 2 (seconds 120 to 179). Two trades share the earliest timestamp 125, so the one listed first (price 50) is the open. The close is the trade at 130.
Example 3
- Input:
- trades = [[0, 7, 1], [200, 9, 2]]
- Output:
- [[0, 7, 7, 7, 7, 1], [3, 9, 9, 9, 9, 2]]
- Explanation
- Minutes 1 and 2 have no trades, so no candles are produced for them.
Constraints
0 <= trades.length <= 10^50 <= timestamp <= 2 * 10^91 <= price <= 10^61 <= quantity <= 10^6