如何在Tradingview上的pinescript中在某个时间绘制垂直线?

not*_*bit 8 pine-script

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

自从我上一篇关于垂直线的文章以来, pine-script 已更新为包含vline(),但是,这里的问题是获取正确的时间。大多数服务器(针对外汇)似乎都位于美国,并且交易视图本地时间设置(显示在左下角)似乎完全独立于 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
Run Code Online (Sandbox Code Playgroud)

我无法使上述工作正常,但我至少可以显示以下几行:

//@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)
Run Code Online (Sandbox Code Playgroud)

然而,这些台词是完全错误的:

在此输入图像描述

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


可能有用的链接:


更新

感谢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)
Run Code Online (Sandbox Code Playgroud)

但是,这段代码有两个问题。

  1. 它没有使用timeframe.period允许您在一个地方指定某些日期和时间间隔的好功能。
  2. 它将线条放在蜡烛的“中间”,因此如果您在 1H 图表上并希望在 08.00 处绘制线条,它会将其放在中间。因此不适合作为警报/信号,除非您使用 <= 1 分钟的时间范围。

Pin*_*ucF 8

版本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")
Run Code Online (Sandbox Code Playgroud)

图表以 UTC+1 时间显示,指标设置为“无比例”,以免扰乱价格比例:

在此输入图像描述

版本2

在此版本中,您可以选择:

  • 从/到小时范围
  • 仅在工作日显示线路
  • 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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述