(intervalMs: number)
| 74 | } |
| 75 | |
| 76 | export function createCandleAggregator(intervalMs: number): CandleAggregator { |
| 77 | let currentCandle: OHLCDataPoint | null = null; |
| 78 | let candleStartTime = 0; |
| 79 | |
| 80 | return { |
| 81 | processTick(tick: Tick): OHLCDataPoint | null { |
| 82 | const candleTime = Math.floor(tick.timestamp / intervalMs) * intervalMs; |
| 83 | |
| 84 | if (currentCandle === null || candleTime > candleStartTime) { |
| 85 | // New candle period |
| 86 | const completedCandle = currentCandle; |
| 87 | candleStartTime = candleTime; |
| 88 | currentCandle = [ |
| 89 | candleTime, |
| 90 | tick.price, // open |
| 91 | tick.price, // close |
| 92 | tick.price, // low |
| 93 | tick.price, // high |
| 94 | ]; |
| 95 | return completedCandle; |
| 96 | } |
| 97 | |
| 98 | // Update current candle |
| 99 | const [time, open, , low, high] = currentCandle; |
| 100 | currentCandle = [ |
| 101 | time, |
| 102 | open, |
| 103 | tick.price, // close = latest price |
| 104 | Math.min(low, tick.price), // low |
| 105 | Math.max(high, tick.price), // high |
| 106 | ]; |
| 107 | |
| 108 | return null; |
| 109 | }, |
| 110 | |
| 111 | getCurrentCandle() { |
| 112 | return currentCandle; |
| 113 | }, |
| 114 | }; |
| 115 | } |
no outgoing calls
no test coverage detected