You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在TradingView PineScript中用input.source捕获指标信号?

TradingView中用input.source捕获信号的正确写法

你的问题出在对input.source的用法理解有误:这个函数接收的是序列数据(比如K线收盘价、指标输出的数值信号),而不是直接传入"Bullish"这类文本标签。下面给你正确的实现思路和代码:

核心逻辑说明

目标指标的看涨/看跌信号通常是用数值标记的(比如1代表看涨,-1代表看跌,0代表无信号),你需要捕获的是这个数值序列,而非文本标签。

正确代码示例

情况1:自己编写指标输出信号

如果是你自己写的BSI指标,先定义信号输出,再用input.source选择:

// 先定义BSI的看涨/看跌信号(示例逻辑,替换成你实际的BSI计算)
bsiValue = ta.sma(close - open, 14)
bullishSignal = bsiValue > 0 ? 1 : 0
bearishSignal = bsiValue < 0 ? -1 : 0

// 用input.source选择要捕获的信号源
selectedSignal = input.source(bullishSignal, title="选择信号源", tooltip="切换看涨/看跌信号")

// 根据信号绘图
plotshape(selectedSignal == 1, title="看涨信号", location=location.belowbar, color=color.green, style=shape.labelup, text="BULLISH")
plotshape(selectedSignal == -1, title="看跌信号", location=location.abovebar, color=color.red, style=shape.labeldown, text="BEARISH")

情况2:调用已有的BSI指标信号

如果是调用市场上已有的BSI指标,先通过正确方式引用指标的输出序列,再传入input.source:

// 引用已有的BSI指标(假设该指标输出的看涨/看跌信号为对应序列)
[bsiBullish, bsiBearish] = request.security(syminfo.tickerid, timeframe.period, [ta.indicator("BSI", "bullish"), ta.indicator("BSI", "bearish")])

// 用input.source选择信号源
selectedSignal = input.source(bsiBullish, title="选择BSI信号", options=[bsiBullish, bsiBearish])

// 绘图逻辑
plotshape(selectedSignal == 1, title="看涨信号", location=location.belowbar, color=color.green, style=shape.labelup, text="BULLISH")
plotshape(selectedSignal == -1, title="看跌信号", location=location.abovebar, color=color.red, style=shape.labeldown, text="BEARISH")

常见报错原因

  • 传入input.source的不是序列数据:比如直接写"Bullish"字符串、非序列类型变量,都会触发报错。
  • 引用的外部指标未正确加载:调用外部指标时,要确保该指标已添加到当前图表,或通过request.security正确引用。
  • 变量命名冲突:检查是否有重复变量名导致脚本异常。

内容的提问来源于stack exchange,提问作者Page

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 00:01:05