首页 > 解决方案 > 我们可以强制一个函数重绘吗?

问题描述

我创建了这个函数来获取给定柱偏移的时间戳。

// on the 1mn timeframe
getBarTime(lookbehind) => 
    timestamp(year(timenow), month(timenow), dayofmonth(timenow), hour(timenow), minute(timenow), 0) - lookbehind*60*1000

它似乎按预期工作,但是当我绘制时它似乎没有重新绘制:

plot(time == getBarTime(2) ? 1 : 0)

它冻结输出时间戳并且从不重绘。

标签: pine-script

解决方案


您可以使用 获取任何给定偏移量的开放时间time[offset]

这允许查看实时栏中发生的情况:

//@version=4
study("")
// on the 1mn timeframe
getBarTime(lookbehind) => 
    timestamp(year(timenow), month(timenow), dayofmonth(timenow), hour(timenow), minute(timenow), 0) - lookbehind*60*1000
// Condition is only true once, when the script executes on the second to last bar of the dataset.
bgcolor(time == getBarTime(2) ? color.green : na)
plot(getBarTime(2))
// Get time 2 bars back.
plotchar(time[2], "time[2]", "", location.top, size = size.tiny)
// See `timenow` advance on each realtime bar iteration (has a 1sec precision).
plotchar(timenow, "timenow", "", location.top, size = size.tiny)

在历史柱上,timenow将每秒更新一次,因此只有当您的脚本在它们上执行超过 1 秒时才会看到更改。


推荐阅读