Pine 脚本中的计时器

Timer in Pine Script

是否可以在 Pine 中编写计时器?

我想检查某个条件是否在较长时间内得到满足,但要查看实时价格。例如,如果“收盘价”在 MA 上方徘徊 35 秒,则发出警报。

这似乎无法完成,或者至少无法正确完成,因为即使是“倒计时”脚本也非常不稳定且几乎没有响应。

编辑: 我已经有了代码,但它甚至不能远程工作

//@version=4
study("My Script")

int counter = 0
for t=timenow to timenow+29000 by 1000
    bool condition = close > close[1]
    if condition
        counter:=counter+1

isCounter = counter==30
plot(counter)
alertcondition(isCounter, title="alert", message="TEST ALERT")

我觉得这很接近你想要的。
不过,在这个概念示例中,我没有包括 收盘价高于前一个 条件。
如果自上一柱的收盘时间起超过 30 秒,标签将变为绿色。
缺点是它不是很准确,因为 Pine 脚本会在价格变化时执行。
所以恐怕不能创建一个精确的(连续的)计时器。

您可以在 BTCUSD 1 分钟图表上观看。

//@version=4
study("My Script", "My", true)

var     float   last_close      = na
var     int     last_close_time = na
var     color   myColor         = na
var     label   myLabel         = label.new(na, na, "", style=label.style_label_left)

last_close          := close[1]
last_close_time     := time_close[1]

over_threshold = barstate.isrealtime and (timenow - last_close_time >= 30000)
myColor := over_threshold ? color.lime : color.blue

label.set_xy(myLabel, bar_index, close)
label.set_text(myLabel, tostring((timenow - last_close_time)/1000) + " seconds passed" )
label.set_color(myLabel, myColor)