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

Pine Script复制代码报错:if条件需布尔值问题排查与解决

Pine Script 报错分析与解决方法

问题原因

  1. 报错根源:time(timeframe.period, Londont)返回的是时间戳(series int类型),而Pine Script的if条件要求必须是布尔值,直接写if London会因类型不匹配触发报错。
  2. 无效修改的问题:改成if London == londontime后,londontime初始值为脚本启动时的时间,只有第一次时间戳完全匹配时才会触发,后续时段无法满足条件,导致指标逻辑失效,图表无显示。

解决方法

核心修正点

  • 用not na(London)判断当前是否处于目标时段:当time()函数不在指定时段内时会返回na,非空判断可得到合法的布尔值,符合if条件要求。
  • 补全时段变量定义:原代码中Londont、USt、Asiat未定义,需添加对应时区时段字符串(如伦敦时段"GMT+1")。

修正后的完整代码

//@version=5
indicator("Session High/Low", overlay=true)

// 定义各时段时区字符串(根据实际需求调整)
Londont = "GMT+1"
USt = "GMT-4"
Asiat = "GMT+8"

London = time(timeframe.period, Londont)
US = time(timeframe.period, USt)
Asia = time(timeframe.period, Asiat)

var clondonhigh = 0.0
var clondonlow = close
var londontime = time
var flondonhigh = 0.0
var flondonlow = 0.0
var flondonopen = 0.0

var onelondonfalse = false
if not na(London)
    if high > clondonhigh
        clondonhigh := high
    if low < clondonlow
        clondonlow := low
    if onelondonfalse
        londontime := time
        flondonopen := open
    flondonhigh := clondonhigh
    flondonlow := clondonlow
    onelondonfalse := false
else
    if not onelondonfalse
        flondonhigh := clondonhigh
        flondonlow := clondonlow
    onelondonfalse := true
    clondonhigh := 0.0
    clondonlow := close

//////////////////////////////////
var cushigh = 0.0
var cuslow = close
var ustime = time
var fushigh = 0.0
var fuslow = 0.0
var fusopen = 0.0

var oneusfalse = false
if not na(US)
    if high > cushigh
        cushigh := high
    if low < cuslow
        cuslow := low
    if oneusfalse
        ustime := time
        fusopen := open
    fushigh := cushigh
    fuslow := cuslow
    oneusfalse := false
else
    if not oneusfalse
        fushigh := cushigh
        fuslow := cuslow
    oneusfalse := true
    cushigh := 0.0
    cuslow := close

//////////////////////////////////
var casiahigh = 0.0
var casialow = close
var asiatime = time
var fasiahigh = 0.0
var fasialow = 0.0
var fasiaopen = 0.0

var oneasiafalse = false
if not na(Asia)
    if high > casiahigh
        casiahigh := high
    if low < casialow
        casialow := low
    if oneasiafalse
        asiatime := time
        fasiaopen := open
    fasiahigh := casiahigh
    fasialow := casialow
    oneasiafalse := false
else
    if not oneasiafalse
        fasiahigh := casiahigh
        fasialow := casialow
    oneasiafalse := true
    casiahigh := 0.0
    casialow := close

// 添加绘图逻辑(原代码未包含,需手动添加才能在图表显示)
plot(flondonhigh, color=color.blue, title="London High")
plot(flondonlow, color=color.red, title="London Low")
plot(fushigh, color=color.orange, title="US High")
plot(fuslow, color=color.green, title="US Low")
plot(fasiahigh, color=color.purple, title="Asia High")
plot(fasialow, color=color.teal, title="Asia Low")

额外说明

原代码未包含绘图语句,即使逻辑修复后也不会在图表显示,因此修正代码中添加了plot()函数,将各时段高低价绘制到图表上。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 15:48:25