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

时间:2019-10-30 23:49:44

标签: 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)

非常感谢您的帮助:)

2 个答案:

答案 0 :(得分:1)

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

//@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)

enter image description here

答案 1 :(得分:0)

正如@LucF所说,这实际上是Pine中的错误。