安全功能返回 "cannot use a mutable variable as an argument"

Security functions returing "cannot use a mutable variable as an argument"

我正在尝试创建一个筛选器,但 atm 收到上面代码行之一的错误消息 Cannot use a mutable variable as an argument of the security function

有错误的行是:c01 = security(s01, timeframe.period, condition) 所以我猜我的 condition 是有缺陷的,但我已经看到类似的代码工作并且无法解决问题。

顺便说一句 ,但不明白这在此处如何应用。

下面是(希望如此)代码的相关部分,您可以找到 the entire code here 以备不时之需 and/or 重新使用它。

感谢您的帮助!

lookBack = input(title="Lookback", type=input.integer, defval=24, minval=2)
range = input(title="Upper & Lower Range %", type=input.integer, defval=25, minval=10)

s01 = input('BINANCE:BTCUSDT', type=input.symbol)

highestHigh = highest(high, lookBack)
lowestLow = lowest(low, lookBack)
upperRange = highestHigh - ((highestHigh - lowestLow)/range) 
lowerRange = ((highestHigh - lowestLow)/range) + lowestLow
HighAboveUpperRange = high > upperRange
LowBelowLowerRange = low < lowerRange

occurrencesAbove = sum(HighAboveUpperRange ? 1 : 0, lookBack)

triggerA = occurrencesAboveFirstHalf >= 1 ? true : false
triggerB = crossunder(low, lowerRange)
condition = triggerA and triggerB

c01 = security(s01, timeframe.period, condition)
scr_label := c01 ? scr_label + s01 + '\n' : scr_label

lab_l = label.new(
          bar_index, 0, scr_label, 
          color=color.gray, 
          textcolor=color.black, 
          style =  label.style_labeldown,
          yloc = yloc.price)

label.delete(lab_l[1])
plot(0, transp = 100)

更新:

我已经将我的条件更新为一个函数,并相应地更新了我的安全函数,如下所示:

screenerFunc() => triggerA and triggerB and triggerC and triggerD
security(s01, res, screenerFunc())

尽管安全功能仍然 returns Cannot use a mutable variable as an argument of the security function

I have also updated the entire code here.

您需要将整个触发算法(及其所有因变量)移动到一个函数中。像这样:

trigger(range) =>

    lRange = 100/range

    highestHigh = highest(high, lookBack)
    lowestLow = lowest(low, lookBack)

    xAxisStartsAt = bar_index[lookBack]
    xAxisFinishesAt = bar_index
    upperLimit = highestHigh
    lowerLimit = lowestLow

    upperRange = highestHigh - ((highestHigh - lowestLow)/lRange) 
    lowerRange = ((highestHigh - lowestLow)/lRange) + lowestLow

    HighAboveUpperRange = high > upperRange
    LowBelowLowerRange = low < lowerRange

    occurrencesAboveTotal   = sum(HighAboveUpperRange ? 1 : 0, lookBack)
    occurrencesAboveSecondHalf = sum(HighAboveUpperRange ? 1 : 0, lookBack/2)
    occurrencesAboveFirstHalf  = occurrencesAboveTotal - occurrencesAboveSecondHalf

    occurrencesBelowTotal   = sum(LowBelowLowerRange ? 1 : 0, lookBack)
    occurrencesBelowSecondHalf = sum(LowBelowLowerRange ? 1 : 0, lookBack/2)
    occurrencesBelowFirstHalf  = occurrencesBelowTotal - occurrencesBelowSecondHalf

    triggerA = occurrencesAboveFirstHalf >= 1 ? true : false
    triggerB = occurrencesAboveSecondHalf >= 1 ? true : false
    triggerC = occurrencesBelowFirstHalf >= 1 ? true : false
    triggerD = crossunder(low, lowerRange)

    condition = triggerA and triggerB and triggerC and triggerD

c01 = security(s01, timeframe.period,  trigger(range))

因为安全函数会读取您的整个函数,并且它会根据您请求的时间范围和代码用适当的值替换上下文变量(如收盘价、低价等...)。您的布尔变量只是算法的结果,安全函数需要知道整个算法,以便它可以计算布尔值本身。

我测试了这个函数,它确实修复了可变错误,但由于我在函数内部移动了一些变量,它会在代码的其他部分产生一些错误,您可以自己轻松修复它。