You need to enable JavaScript to run this app. Pine Script如何过滤交易所休市K线 仅统计双方开市时段价差
优惠活动
大模型
产品
解决方案
定价
更多

Pine Script如何过滤交易所休市K线 仅统计双方开市时段价差

Pine Script跨标的价差休市时段过滤解决方案

Pine Script中调用security()函数获取其他标的行情时,若对应时段标的处于休市状态,返回值会是na(空值),你只要增加一层有效性判断,仅在两个标的的返回值都不为空时计算价差即可。

修改后的完整代码如下:

//@version=4
study("Spread")

security_1 = input(title="Security 1", type=symbol, defval="BINANCE:BTCPERP")
security_2 = input(title="Security 2", type=symbol, defval="BINANCE:ETHPERP")

security_1_close = security(security_1, period, close)
security_2_close = security(security_2, period, close)

// 仅两个标的都有有效行情时才计算价差,否则返回空值
spread = not na(security_1_close) and not na(security_2_close) ? security_1_close - security_2_close : na

// 可选:绘制价差线
plot(spread, title="Spread", color=color.blue)

改动说明:

  • 核心新增了三元判断逻辑:not na(security_1_close) and not na(security_2_close)会验证当前K线时段两个标的都有有效交易数据,只要有一个休市返回空,就会给价差赋值na,图表上对应时段会自动留空,指标计算时也会自动跳过这些空值
  • 完全兼容原有参数配置,不需要调整输入项和标的取值逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.21 10:09:03