MCPcopy
hub / github.com/ChartGPU/ChartGPU / switchTimeframe

Function switchTimeframe

examples/candlestick-streaming/main.ts:266–347  ·  view source on GitHub ↗

* Switch to a different candlestick timeframe. * Regenerates historical data and recreates the candle aggregator.

(tf: string)

Source from the content-addressed store, hash-verified

264 * Regenerates historical data and recreates the candle aggregator.
265 */
266function switchTimeframe(tf: string) {
267 if (!TIMEFRAME_INTERVALS[tf]) {
268 console.warn(`Unknown timeframe: ${tf}`);
269 return;
270 }
271
272 // Skip if already on this timeframe
273 if (tf === currentTimeframe) {
274 return;
275 }
276
277 // Remember if we were streaming
278 const wasStreaming = isStreaming;
279
280 // Stop streaming while we regenerate
281 if (isStreaming) {
282 tickSimulator.stop();
283 }
284
285 // Update timeframe state
286 currentTimeframe = tf;
287 candleIntervalMs = TIMEFRAME_INTERVALS[tf];
288 timeScale = TIMEFRAME_TIMESCALES[tf];
289
290 console.log(`Switching to ${tf} timeframe (${candleIntervalMs}ms interval, ${timeScale}x speed)`);
291
292 // Regenerate historical data with new interval
293 const startGen = performance.now();
294 data = generateHistoricalData({
295 symbol: CONFIG.symbol,
296 startPrice: CONFIG.startPrice,
297 volatility: 0.03, // 3% volatility like static example for nice candle sizes
298 candleCount: currentCandleCount,
299 intervalMs: candleIntervalMs,
300 });
301 console.log(`Regenerated ${data.length.toLocaleString()} candles in ${(performance.now() - startGen).toFixed(0)}ms`);
302
303 // Get last price for tick simulator
304 const lastCandle = data[data.length - 1];
305 const lastPrice = getClose(lastCandle);
306
307 // Reset simulated time based on new data
308 simulatedTimeMs = getTimestamp(lastCandle) + candleIntervalMs;
309 lastSimPerfNow = performance.now();
310
311 // Recreate candle aggregator with new interval
312 candleAggregator = createCandleAggregator(candleIntervalMs);
313
314 // Recreate tick simulator with current price
315 tickSimulator = createTickSimulator({
316 initialPrice: lastPrice,
317 ticksPerSecond: CONFIG.ticksPerSecond,
318 volatility: CONFIG.tickVolatility,
319 onTick: handleTick,
320 });
321
322 // Update chart with new data (full replacement)
323 const series0 = fullChartOptions.series?.[0];

Callers 1

setupControlsFunction · 0.85

Calls 9

generateHistoricalDataFunction · 0.90
createCandleAggregatorFunction · 0.90
createTickSimulatorFunction · 0.90
updateStatsFunction · 0.85
getCloseFunction · 0.70
getTimestampFunction · 0.70
stopMethod · 0.65
setOptionMethod · 0.65
startMethod · 0.65

Tested by

no test coverage detected