发散函数结果问题

Divergence Funtion result issue

我尝试实现一个函数来查找短期 RSI 背离。我希望它像这样工作:

但是不行。

study("RSI short term divergence", overlay=true)

//Inputs
lookback = input(defval=34, title='Lookback for RSI divergence, bars', minval=1, type=input.integer)
len = input(defval=3, title='Pivot confirmation, bars', minval=1, type=input.integer)

//RSI
rsi = rsi(close, 14)

//Variables
pivotLow = pivotlow(rsi, len, 1)

//Function

func_short(pivsback) =>
    count_s = 0
    for i = 1 to pivsback
        rsiLast_s = valuewhen(pivotLow, rsi[1], 0)
        priceLast_s = valuewhen(pivotLow, close[1], 0)
        barLast_s = valuewhen(pivotLow, bar_index[1], 0)
        rsiWas_s = valuewhen(pivotLow, rsi[1], i)
        priceWas_s = valuewhen(pivotLow, close[1], i)
        barWas_s = valuewhen(pivotLow, bar_index[1], i)
        count_s = if (rsiLast_s > rsiWas_s) and (priceWas_s > priceLast_s) and (barLast_s-barWas_s)<lookback
            count_s := count_s + 1
    count_s

divShort = func_short(3)>0

plotchar(divShort, location=location.abovebar, color=color.white, size=size.small)

valuewhen() 与许多其他内置函数一样,不能在带有可变参数的 for 循环中使用。有关解释,请参阅 this usrman section

您的选择是:

  1. 使用这样的结构来预评估预设数量的枢轴,然后运行你对它们的逻辑:
//@version=4
study("Max Pivots", "", true)
legs = input(4)
pH = pivothigh(legs, legs)
newPH = not na(pH)
p00 = valuewhen(newPH, high[legs], 00)
p01 = valuewhen(newPH, high[legs], 01)
p02 = valuewhen(newPH, high[legs], 02)
maxPH = max(p00, p01, p02)
plot(maxPH)
plotchar(newPH, "newPH", "•", location.abovebar, offset = - legs)
plotchar(newPH, "newPH", "▲", location.top)
  1. 运行 一个嵌入式 for 循环来寻找过去的柱形以获取过去的枢轴值,我不推荐这样做,因为它会非常慢。