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

如何在Pine Script v4中处理布尔运算及指标转换报错解决

问题解决:Binary Options Arrows指标Pine Script v4转换及报错修复

报错原因分析

  • 布尔值减法错误:Pine Script v4对类型检查更严格,series[bool]类型不能直接使用-运算符;而v3会自动将布尔值转为1(true)或0(false),v4需要显式完成类型转换。
  • ARROW未定义:此错误是前一个类型错误引发的连锁问题,修复类型转换后会自动解决。

转换后的完整v4代码

//@version=4
indicator(shorttitle="BOA", title="Binary Options Arrows (example) [TheMightyChicken]", overlay=true)
// Inputs
E = input(1, minval=1, title="Expiry")
// Calculations
Bearish_harami = (close[1] > open[1] and open > close and open <= close[1] and open[1] <= close and open - close < close[1] - open[1] and open[5] < open)
Bullish_harami = (open[1] > close[1] and close > open and close <= open[1] and close[1] <= open and close - open < open[1] - close[1] and open[5] > open)
// Setups
CALL = Bullish_harami
PUT = Bearish_harami
// 显式转换布尔值为整数,解决减法运算类型不匹配问题
ARROW = int(CALL) - int(PUT)
plotarrow(ARROW, colorup=color.lime, colordown=color.red, transp=0, minheight=10, maxheight=10)
// Results
WIN = (CALL[E] and close[E] < close) or (PUT[E] and close[E] > close)
LOSE = (CALL[E] and close[E] >= close) or (PUT[E] and close[E] <= close)
bgcolor(WIN ? color.lime : LOSE ? color.red : na, transp=70)

关键修改点

  • 替换v3的study()为v4标准入口函数indicator()。
  • 用int()显式将布尔类型的CALL和PUT转为整数:int(CALL)会把true转为1,false转为0,满足减法运算的类型要求。
  • 移除冗余的==1判断:Pine Script中布尔值可直接用于逻辑判断,无需与1比较。
  • v4中颜色常量需添加color.前缀,例如lime改为color.lime。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 20:49:58