如何在Pine Script中用request.symbol限定指标仅加载指定标的数据?
问题:如何让Pine Script指标仅针对特定标的生成信号并在其他品种图表显示?
原始指标代码
indicator("India VIX Modified",overlay=true) A=ta.lowest(low,15) B=low C=close>open D=B==A Sell=C and D E=ta.highest(high,15) F=high G=close<open H=F==E Buy=G and H plotshape(Sell,"sell",style=shape.arrowdown,color=color.red) plotshape(Buy,"Buy",style = shape.arrowup,color=color.green)
用户需求
希望上述指标仅针对某一特定标的计算信号,当指标叠加在任意图表/品种上时,显示的始终是该特定标的的信号结果,询问如何使用request.symbol实现。
解决方案
要实现这个需求,你需要通过request.security()函数结合request.symbol拉取目标标的的行情数据,所有信号计算都基于该目标数据,而非当前图表的品种数据。以下是修改后的完整代码:
indicator("India VIX Modified (Target Symbol)", overlay=true) // 1. 定义你要跟踪的特定标的,格式为「交易所代码:标的代码」 targetSymbol = request.symbol("NSE:INDIAVIX") // 替换为你实际需要的标的 // 2. 拉取目标标的的OHLC数据,使用当前图表的时间周期 [targetOpen, targetHigh, targetLow, targetClose] = request.security(targetSymbol, timeframe.period, [open, high, low, close]) // 3. 基于目标标的的数据计算买卖信号 A = ta.lowest(targetLow, 15) B = targetLow C = targetClose > targetOpen D = B == A Sell = C and D E = ta.highest(targetHigh, 15) F = targetHigh G = targetClose < targetOpen H = F == E Buy = G and H // 4. 在当前图表绘制信号 plotshape(Sell, "sell", style=shape.arrowdown, color=color.red) plotshape(Buy, "Buy", style=shape.arrowup, color=color.green)
关键说明
request.symbol("NSE:INDIAVIX"):明确指定目标标的,可根据需求替换为其他标的(如美股NYSE:SPX),需遵循「交易所代码:标的代码」的格式规范。request.security():从目标标的拉取与当前图表一致时间周期的行情数据,保证信号时间对齐。- 所有信号计算逻辑均基于
targetOpen、targetHigh等目标标的数据,而非当前图表的本地数据,因此无论叠加在哪个品种上,显示的始终是指定标的的买卖信号。
内容的提问来源于stack exchange,提问作者Avi Singh
相关产品推荐
相关产品推荐

