MCPcopy
hub / github.com/ChartGPU/ChartGPU / switchCandleCount

Function switchCandleCount

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

* Switch to a different candle count. * Regenerates historical data and recreates the chart view.

(count: number)

Source from the content-addressed store, hash-verified

351 * Regenerates historical data and recreates the chart view.
352 */
353function switchCandleCount(count: number) {
354 // Skip if already at this count
355 if (count === currentCandleCount) {
356 return;
357 }
358
359 // Remember if we were streaming
360 const wasStreaming = isStreaming;
361
362 // Stop streaming while we regenerate
363 if (isStreaming) {
364 tickSimulator.stop();
365 }
366
367 // Update candle count
368 currentCandleCount = count;
369
370 console.log(`Switching to ${count.toLocaleString()} candles`);
371
372 // Regenerate historical data with new count
373 const startGen = performance.now();
374 data = generateHistoricalData({
375 symbol: CONFIG.symbol,
376 startPrice: CONFIG.startPrice,
377 volatility: 0.03,
378 candleCount: currentCandleCount,
379 intervalMs: candleIntervalMs,
380 });
381 console.log(`Regenerated ${data.length.toLocaleString()} candles in ${(performance.now() - startGen).toFixed(0)}ms`);
382
383 // Get last price for tick simulator
384 const lastCandle = data[data.length - 1];
385 const lastPrice = getClose(lastCandle);
386
387 // Reset simulated time based on new data
388 simulatedTimeMs = getTimestamp(lastCandle) + candleIntervalMs;
389 lastSimPerfNow = performance.now();
390
391 // Recreate candle aggregator
392 candleAggregator = createCandleAggregator(candleIntervalMs);
393
394 // Recreate tick simulator with current price
395 tickSimulator = createTickSimulator({
396 initialPrice: lastPrice,
397 ticksPerSecond: CONFIG.ticksPerSecond,
398 volatility: CONFIG.tickVolatility,
399 onTick: handleTick,
400 });
401
402 // Update chart with new data and appropriate dataZoom config
403 const series0 = fullChartOptions.series?.[0];
404 if (series0 && series0.type === 'candlestick') {
405 fullChartOptions = {
406 ...fullChartOptions,
407 dataZoom: getDataZoomConfig(currentCandleCount),
408 series: [
409 {
410 ...series0,

Callers 1

setupControlsFunction · 0.85

Calls 10

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

Tested by

no test coverage detected