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

如何修复Pine Script v5自定义Cyclic Smoothed RSI中的‘iff’错误

修复Pine Script v5中"Could not find function or function reference 'iff'"错误

问题原因

Pine Script v5移除了全局的iff函数,该函数在v4及更早版本中可用,v5需使用ta.iff(技术分析命名空间下的同名函数),或直接用三元条件运算符condition ? 真值 : 假值替代。

修复步骤

将代码中所有iff(...)替换为ta.iff(...)即可解决报错。

修复后的完整代码

//@version=5
indicator(title="csRSI-EMA", shorttitle="csRSI-EMA")
src = close
domcycle = input.int(20, minval=10, title="Dominant Cycle Length")
crsi = 0.0
cyclelen = domcycle / 2
vibration = 10
leveling = 10.0
cyclicmemory = domcycle * 2
//set min/max ranges?

h1 = hline(30, color=color.silver, linestyle=hline.style_dashed)
h2 = hline(70, color=color.silver, linestyle=hline.style_dashed)

torque = 2.0 / (vibration + 1)
phasingLag = (vibration - 1) / 2.0

up = ta.rma(math.max(ta.change(src), 0), cyclelen)
down = ta.rma(-math.min(ta.change(src), 0), cyclelen)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - 100 / (1 + up / down)
crsi := torque * (2 * rsi - rsi[phasingLag]) + (1 - torque) * nz(crsi[1])

lmax = -999999.0
lmin = 999999.0
for i = 0 to cyclicmemory - 1 by 1
    if nz(crsi[i], -999999.0) > lmax
        lmax := nz(crsi[i])
        lmax
    else
        if nz(crsi[i], 999999.0) < lmin
            lmin := nz(crsi[i])
            lmin

mstep = (lmax - lmin) / 100
aperc = leveling / 100

db = 0.0
for steps = 0 to 100 by 1
    testvalue = lmin + mstep * steps
    above = 0
    below = 0
    for m = 0 to cyclicmemory - 1 by 1
        below := below + ta.iff(crsi[m] < testvalue, 1, 0)
        below

    ratio = below / cyclicmemory
    if ratio >= aperc
        db := testvalue
        break
    else
        continue

ub = 0.0
for steps = 0 to 100 by 1
    testvalue = lmax - mstep * steps
    above = 0
    for m = 0 to cyclicmemory - 1 by 1
        above := above + ta.iff(crsi[m] >= testvalue, 1, 0)
        above

    ratio = above / cyclicmemory
    if ratio >= aperc
        ub := testvalue
        break
    else
        continue

//EMA with Signal
//Inputs
len = input(9, minval=1, title="Length")
srcema = crsi
smoothingLength = input(title = "Length", defval = 5, minval = 1, maxval = 100, group="Smoothing")
offset = input(title="Offset", defval=0, minval=-500, maxval=500)
//Calculation
ma = ta.ema(srcema, len)
sigline = ta.sma(ma, smoothingLength)
hist = ma - sigline + 50

//Histogram Color
col_grow_above = input(#26A69A, "Above   Grow", group="Histogram", inline="Above")
col_fall_above = input(#B2DFDB, "Fall", group="Histogram", inline="Above")
col_grow_below = input(#FFCDD2, "Below Grow", group="Histogram", inline="Below")
col_fall_below = input(#FF5252, "Fall", group="Histogram", inline="Below")

//Plotting
plot(hist, histbase=50, title="Histogram", style=plot.style_columns, color=(hist>=50 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))
plot(ma, title="EMA", color=color.green, offset=offset)
hline(50, "Median", color=color.new(#787B86, 50))
plot(sigline, title="Smoothing Line", color=color.red, offset=offset, display=display.none)
lowband = plot(db, "LowBand", color.aqua)
highband = plot(ub, "HighBand", color.aqua)
fill(h1, h2, color=color.silver, transp=90)
fill(lowband, highband, color=color.gray, transp=90)
plot(crsi, "CRSI", color.fuchsia)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 15:45:16