如何在Tradingview的pinescript中的某个时间画一条垂直线?

How to draw a vertical line at a certain time in pinescript on Tradingview?

我想画一条垂直线,每天在某个当地时间(例如 08:00 GMT+1)。

然而,自从我 about vertical lines, pine-script has been updated to include vline() 以来,这里的问题是把握好时间。大多数服务器(用于 FX)似乎是基于美国的,交易视图本地时间设置(显示在左下角)似乎完全独立于 pine-script 中的设置。

//@version=4
study("Time Adjusted Vertical Line", overlay=true)

vline(BarIndex, Color, LineStyle, LineWidth) => // Verticle Line, 54 lines maximum allowable per indicator
    return = line.new(BarIndex, -1000, BarIndex, 1000, xloc.bar_index, extend.both, Color, LineStyle, LineWidth)

if(bar_index%10==0.0)
    vline(bar_index, #FF8000ff, line.style_solid, 1) // Variable assignment not required

我无法让上面的工作正常工作,但我得到这个至少可以显示这些行:

//@version=4
study(title="Time Based Session Bars", shorttitle="NowOpen", overlay=true)
line_height = 2    // We must define a height that reaches far above the highest price level in main chart!

gmt_offs = 2 // GMT + X
nys_offs = 6 // EST (in GMT)

t1 = time(timeframe.period, "0930-0935:23456")
//t1 = time(timeframe.isintraday, "0930-0935:23456")
//t2 = t1 + gmt_offs*60*60*60
t2 = t1 + 2
plot(na(t2) ? 0 : line_height, title='Hello!', color=#101010, linewidth=1, style=plot.style_histogram, transp=50, offset=0, trackprice=false)

但是,这些行完全错误:

如何扩展上面的代码,在每天的特定时间绘制一条垂直线,并获得正确的时间位置?


可能有用的链接:


更新

感谢 PineCoders-LucF,我能够使用以下代码大致得到我想要的东西:

//@version=4
study("Line at time",overlay=true)

t1 = timestamp("GMT+2", year, month, dayofmonth, 07, 00, 00)
//t2 = timestamp("GMT+2", year, month, dayofmonth, 10, 00, 00)  // Uncomment this to make a range
t2=t1                                                           // Comment out this to use a range
bgcolor( (time >= t1) and (time <= t2) ? color.silver : na, transp = 0)

但是,此代码存在两个问题。

  1. 它没有使用允许您在一个地方指定特定日期和时间间隔的 timeframe.period 功能。
  2. 它将线放在蜡烛的 "middle" 中,因此如果您在 1H 图表上并且想要一条位于 08.00 的线,它只会将它放在中间。因此不适合作为 alarm/signal,除非您使用 <= 1 分钟的时间范围。

版本 1

timestamp() 函数的一个版本可以使用时区参数:

//@version=4
study("Line at time", overlay=true)
targetTime = timestamp("GMT+1", year, month, dayofmonth, 08, 00, 00)
bgcolor(targetTime == time ? color.silver : na, transp = 0)

// Debugging: these plots lines in separate window
plot(targetTime, "targetTime", color.orange)
plot(time, "time")

图表以 UTC+1 时间显示,指标设置为 "No Scale" 不扰乱价格比例:

版本 2

使用此版本您可以选择:

  • 一个from/to小时范围
  • 仅在工作日显示线路
  • 介于 bgcolor 或 vline 模式之间
//@version=4
study("Line at time",overlay=true)
fromHour = input(7)
toHour = input(10)
weekdaysOnly = input(true)
useVline = input(false)
dayIsOk = not weekdaysOnly or (dayofweek != dayofweek.saturday and dayofweek != dayofweek.sunday)
t1 = timestamp("GMT+2", year, month, dayofmonth, fromHour, 00, 00)
t2 = timestamp("GMT+2", year, month, dayofmonth, toHour, 00, 00)
timeIsOk = (time >= t1) and (time <= t2)
bgcolor( not useVline and timeIsOk and dayIsOk ? color.orange : na, transp = 80)
if useVline and timeIsOk and dayIsOk
    line.new(bar_index, low * .9999, bar_index, high * 1.0001, xloc.bar_index, extend.both, #FF8000ff, line.style_solid, 1)

这是我用过的一些代码:

enableTimeLines = input(defval=true, type=input.bool, title="Enable Time Lines")
// Chartlines 1h and 4h
if enableTimeLines
    timeA = 21 // starts at 21:00 hs
    timeB = 21
    for i = 0 to 60
        timeA := timeA - 4 // and from 21hs goes backwards
        targetTimeA = timestamp("GMT-3", year, month, dayofmonth, timeA, 00, 00)
        line.new(x1=targetTimeA, y1=open, x2=targetTimeA, y2=close, xloc=xloc.bar_time, extend=extend.both, color=color.new(color.yellow, 20), style=line.style_dotted, width=1)
        timeB := timeB - 1
        if timeB != (21 or 01 or 05 or 09 or 13 or 17)
            targetTimeB = timestamp("GMT-3", year, month, dayofmonth, timeB, 00, 00)
            line.new(x1=targetTimeB, y1=open, x2=targetTimeB, y2=close, xloc=xloc.bar_time, extend=extend.both, color=color.new(color.silver, 50), style=line.style_dotted, width=1)

它每 1 小时绘制一次银色线条,除了每 4 小时绘制一次黄色线条。它倒退了。

请记住,FOR 循环的时间响应是有限制的。