如何从 Pine Script 中函数中断的循环中获取值?

How to get values from a loop with a break in a function in Pine Script?

真的很难解决这个问题,希望得到任何帮助。 如何在提前中断循环时从循环中获取值?

这是一个更简单的例子,说明了我们真正想要完成的事情,但我希望它能传达我们的想法。

目前,它会引发运行时错误。

//@version=4
study("Test barssince", overlay=true)

// NOTE: run on the daily chart

n = input(1)

sundayHigh = dayofweek == dayofweek.sunday ? high : na 
bgcolor(sundayHigh ? color.green : na)

someHighPrice = valuewhen(sundayHigh, sundayHigh, n) // value of the nth previous sundayHigh

mybarssince(ser, val) =>
    int bars = na
    for i=0 to 99
        bars := i
        if ser[i] == val
            break
    bars
int sbars = mybarssince(high, someHighPrice) // Runtime error.

if dayofweek == dayofweek.wednesday
    // connect this bar (wednesday) with the nth previous sunday
    line.new(bar_index-sbars, high[sbars], bar_index, high, color=color.orange, width=3)

非常感谢任何帮助:)

我将您的测试日期更改为星期一以及 bars 变量重新分配的位置:

//@version=4
study("Test barssince", overlay=true)

// NOTE: run on the daily chart

n = input(1)

sundayHigh = dayofweek == dayofweek.monday ? high : na 
bgcolor(sundayHigh ? color.green : na)

someHighPrice = valuewhen(sundayHigh, sundayHigh, n) // value of the nth previous sundayHigh

mybarssince(ser, val) =>
    int bars = na
    for i=0 to 99
        if ser[i] == val
            bars := i
            break
    bars
int sbars = mybarssince(high, someHighPrice) // Runtime error.

if dayofweek == dayofweek.wednesday
    // connect this bar (wednesday) with the nth previous sunday
    line.new(bar_index-sbars, high[sbars], bar_index, high, color=color.orange, width=3)

正如@LucF 评论的那样,这原来是 Pine 中的一个错误。