Pine Script延长交易时段下表格正确显示当前ticker名称方法
Pine Script 延长交易时段下ticker名称显示异常修复
问题背景
尝试在同一表格中同时展示当前图表ticker与指定ticker的名称,初始实现代码如下:
//@version=5 indicator('My Indicator') ticker1 = input.symbol(defval='AMEX:DIA', title='Ticker') ticker1_data = request.security('={"session":"extended","symbol":"' + ticker1 + '"}', "", close, barmerge.gaps_off, barmerge.lookahead_off) plot(close, title='Source', color=color.rgb(255, 0, 0, 0), linewidth=1, style=plot.style_line) plot(ticker1_data, title='Source', color=color.rgb(255, 0, 0, 0), linewidth=1, style=plot.style_line) getName(_str) => string[] _pair = str.split(_str, ":") string[] _chars = str.split(array.get(_pair, 1), "") string _return = array.join(_chars, "") table1 = table.new(position = position.top_right, columns = 1, rows = 2) table.cell(table_id = table1, column = 0, row = 0, text = getName(syminfo.tickerid), text_color = color.new(color.blue, 0), text_size = size.normal, bgcolor = color.new(color.white, 0)) table.cell(table_id = table1, column = 0, row = 1, text = getName(ticker1), text_color = color.new(color.blue, 0), text_size = size.normal, bgcolor = color.new(color.white, 0))
上述代码在常规交易时段可正常运行,但开启延长交易时段(extended session)后,表格中当前ticker的显示内容变为"extended", "symbol",无法正确展示TSLA这类实际标的代码,需要调整字符串处理逻辑,兼容两种交易时段的切换。
问题根源
原getName函数逻辑存在明显缺陷:
- 仅简单按
:分割字符串取第二段,完全没有适配开启延长交易时段后syminfo.tickerid返回的JSON结构字符串(格式为={"session":"extended","symbol":"交易所:标的代码"}),分割后拿到的是JSON片段而非真实标的代码 - 函数内将字符串拆分为单字符数组再重新拼接的操作属于冗余逻辑,无实际作用
修复方案
优先使用Pine Script内置API处理标的信息,避免手写不可靠的字符串解析逻辑,同时兼容常规/延长交易时段场景:
- 构造扩展时段标的请求时,用官方
ticker.new()函数替代手写JSON拼接,减少格式错误 - 重写名称解析逻辑,自动识别普通格式、JSON格式的ticker字符串,提取真实标的代码
- 当前图表标的直接调用内置变量
syminfo.root获取代码,无需手动解析,从根源避免时段切换导致的解析错误
修复后完整代码:
//@version=5 indicator('My Indicator') ticker1 = input.symbol(defval='AMEX:DIA', title='Ticker') // 用官方API构造带延长时段支持的标的ID,替代手写JSON extendedTickerId = ticker.new(ticker1, session=session.extended) ticker1_data = request.security(extendedTickerId, timeframe.period, close, barmerge.gaps_off, barmerge.lookahead_off) plot(close, title='Current Ticker Close', color=color.rgb(255, 0, 0, 0), linewidth=1, style=plot.style_line) plot(ticker1_data, title='Selected Ticker Close', color=color.rgb(255, 0, 0, 0), linewidth=1, style=plot.style_line) // 兼容所有ticker格式的名称解析函数 getName(_tickerStr) => string pureTicker = _tickerStr // 识别扩展时段的JSON格式ticker,提取真实标的字段 if str.startswith(_tickerStr, '={"') var string extractPattern = '"symbol":"([^"]+)"' pureTicker := str.extract(_tickerStr, extractPattern, 1) // 分割交易所前缀,返回纯标的代码 string[] tickerParts = str.split(pureTicker, ":") array.get(tickerParts, array.size(tickerParts) - 1) table1 = table.new(position = position.top_right, columns = 1, rows = 2) // 当前图表标的直接用内置变量获取,无需解析 table.cell(table_id = table1, column = 0, row = 0, text = syminfo.root, text_color = color.new(color.blue, 0), text_size = size.normal, bgcolor = color.new(color.white, 0)) table.cell(table_id = table1, column = 0, row = 1, text = getName(ticker1), text_color = color.new(color.blue, 0), text_size = size.normal, bgcolor = color.new(color.white, 0))
效果说明
修复后无论在常规交易时段还是延长交易时段下,表格内两个标的名称均可正常显示,同时兼容带交易所前缀、不带交易所前缀的各类标的代码,不会再出现JSON片段乱码的问题。
内容的提问来源于stack exchange,提问作者Richard D.
相关产品推荐
相关产品推荐

