TradingView Advanced Chart v26升级后getBars无限循环无法渲染图表
TradingView v26升级后getBars无限循环问题分析
核心问题与版本变更影响
TradingView v26对数据馈(Data Feed)的getBars实现有明确行为变更,主要集中在返回值规范和参数处理逻辑上,你的代码存在几个关键问题导致无限循环:
Promise与回调混用
代码同时调用了resolve()和onHistoryCallback(),v25可能兼容这种写法,但v26要求数据馈必须严格通过Promise的resolve返回数据,不能混用旧回调方式。这种混用会让图表库接收到两次空数据信号,误以为数据未加载完成,从而反复发起请求。返回空的bars数组
当response.s === 'ok'时,你创建了空的bars数组并返回,图表库会判定未获取到有效K线数据,进而不断触发getBars请求加载更多数据,形成无限循环。必须从response中提取K线数据填充到bars数组,示例逻辑如下:// 从response提取数据填充bars(需根据实际接口返回结构调整) response.data.forEach(item => { bars.push({ time: item.timestamp, open: item.open, high: item.high, low: item.low, close: item.close, volume: item.volume }); });参数传递错误
- 同时设置了
count: periodParams.countBack和requestParams.countback = periodParams.countBack,参数名拼写不一致(count vs countback),v26对参数校验更严格,多余或错误参数可能导致后端返回异常,触发重试。 periodParams.from和periodParams.to是秒级时间戳,直接赋值给StartDate和EndDate需确认后端接口是否兼容该格式,v26对时间参数精度要求更严格。
- 同时设置了
noData场景未正确resolve
当response.s === 'no_data'时,仅设置了meta但未执行resolve,导致Promise一直处于pending状态,图表库会重复发起请求,需补充resolve({ bars: [], meta: meta })。
修复后的代码示例
getBars(symbolInfo, resolution, periodParams) { const isAdjusted = document.getElementById('isAdjusted').value; const requestParams = { symbol: symbolInfo.ticker || '', resolution: resolution, StartDate: periodParams.from, EndDate: periodParams.to, isAdjust: isAdjusted, countBack: periodParams.countBack }; if (symbolInfo.currency_code !== undefined) { requestParams.currencyCode = symbolInfo.currency_code; } if (symbolInfo.unit_id !== undefined) { requestParams.unitId = symbolInfo.unit_id; } return new Promise((resolve, reject) => { this._requester.sendRequest(URL, requestParams) .then((response) => { if (response.s !== 'ok' && response.s !== 'no_data') { reject(response.errmsg); return; } const bars = []; const meta = { noData: false }; if (response.s === 'no_data') { meta.noData = true; meta.nextTime = response.nextTime; resolve({ bars, meta }); return; } // 替换为实际从response提取K线数据的逻辑 if (response.data && Array.isArray(response.data)) { response.data.forEach(item => { bars.push({ time: item.timestamp, open: item.open, high: item.high, low: item.low, close: item.close, volume: item.volume || 0 }); }); } resolve({ bars, meta }); }) .catch((reason) => { reject(getErrorMessage(reason)); }); }); }
版本变更关键要点
TradingView v26对数据馈的核心变更:
- 强制要求
getBars返回Promise,不再支持旧回调模式混用 - 对返回的K线数据格式校验更严格,空数据会触发重复请求
- 参数传递规范性要求更高,错误或冗余参数可能导致请求失败
内容的提问来源于stack exchange,提问作者Rupak
相关产品推荐
相关产品推荐

