()
| 88 | } |
| 89 | |
| 90 | async function init() { |
| 91 | const container = document.getElementById('chart')!; |
| 92 | |
| 93 | // Generate impressive historical data |
| 94 | console.log(`Generating ${currentCandleCount.toLocaleString()} historical candles...`); |
| 95 | const startGen = performance.now(); |
| 96 | |
| 97 | data = generateHistoricalData({ |
| 98 | symbol: CONFIG.symbol, |
| 99 | startPrice: CONFIG.startPrice, |
| 100 | volatility: 0.03, // 3% volatility like static example for nice candle sizes |
| 101 | candleCount: currentCandleCount, |
| 102 | intervalMs: candleIntervalMs, |
| 103 | }); |
| 104 | |
| 105 | console.log(`Generated in ${(performance.now() - startGen).toFixed(0)}ms`); |
| 106 | |
| 107 | // Get last price for tick simulator |
| 108 | const lastCandle = data[data.length - 1]; |
| 109 | const lastPrice = getClose(lastCandle); // close price |
| 110 | |
| 111 | // Seed simulated time from the most recent historical candle so the first live candle |
| 112 | // continues smoothly after history (instead of starting far in the past/future). |
| 113 | simulatedTimeMs = getTimestamp(lastCandle) + candleIntervalMs; |
| 114 | lastSimPerfNow = performance.now(); |
| 115 | |
| 116 | // Create chart |
| 117 | fullChartOptions = { |
| 118 | xAxis: { type: 'time', name: 'Time' }, |
| 119 | yAxis: { type: 'value', name: `${CONFIG.symbol}` }, |
| 120 | series: [ |
| 121 | { |
| 122 | type: 'candlestick', |
| 123 | name: CONFIG.symbol, |
| 124 | data, |
| 125 | style: 'classic', |
| 126 | itemStyle: { |
| 127 | upColor: '#22c55e', |
| 128 | downColor: '#ef4444', |
| 129 | upBorderColor: '#16a34a', |
| 130 | downBorderColor: '#dc2626', |
| 131 | }, |
| 132 | sampling: 'ohlc', |
| 133 | samplingThreshold: 2000, |
| 134 | }, |
| 135 | ], |
| 136 | dataZoom: getDataZoomConfig(currentCandleCount), |
| 137 | tooltip: { trigger: 'item' }, |
| 138 | animation: false, // Critical for streaming performance |
| 139 | autoScroll: true, |
| 140 | }; |
| 141 | chart = await createChart(container, fullChartOptions); |
| 142 | |
| 143 | // Setup tick simulator |
| 144 | candleAggregator = createCandleAggregator(candleIntervalMs); |
| 145 | |
| 146 | tickSimulator = createTickSimulator({ |
| 147 | initialPrice: lastPrice, |
no test coverage detected