MCPcopy Index your code
hub / github.com/ChartGPU/ChartGPU / createCandleAggregator

Function createCandleAggregator

examples/candlestick-streaming/tickSimulator.ts:76–115  ·  view source on GitHub ↗
(intervalMs: number)

Source from the content-addressed store, hash-verified

74}
75
76export 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}

Callers 3

initFunction · 0.90
switchTimeframeFunction · 0.90
switchCandleCountFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected