* Switch to a different candle count. * Regenerates historical data and recreates the chart view.
(count: number)
| 351 | * Regenerates historical data and recreates the chart view. |
| 352 | */ |
| 353 | function 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, |
no test coverage detected