如何在Tradingview的pinescript中的某个时间画一条垂直线? [英] How to draw a vertical line at a certain time in pinescript on Tradingview?

查看:221
本文介绍了如何在Tradingview的pinescript中的某个时间画一条垂直线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

自从我

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

<小时>

可能有用的链接:

  • 版本 2

    使用此版本,您可以选择:

    • 从/到小时范围
    • 仅在工作日显示线路
    • 在 bgcolor 或 vline 模式之间

    //@version=4研究(时间线",叠加=真)fromHour = 输入(7)toHour = 输入(10)仅工作日 = 输入(真)useVline = 输入(假)dayIsOk = not weekdaysOnly 或 (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(不使用Vline和timeIsOk和dayIsOk?color.orange:na,transp = 80)如果 useVline 和 timeIsOk 和 dayIsOkline.new(bar_index, low * .9999, bar_index, high * 1.0001, xloc.bar_index, extend.both, #FF8000ff, line.style_solid, 1)

    I would like to draw a vertical line, every day at a certain local time (e.g. 08:00 GMT+1).

    Since my last post about vertical lines, pine-script has been updated to include vline(), however, the issue here is getting the time right. Most servers (for FX) seem to be US based and the Trading view local time settings (shown on bottom left) seem totally independent of what is done in 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
    

    I couldn't get the above to work, but I got this to at least show the lines:

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

    However, the lines are totally wrong:

    How can I extend the above code, to plot a vertical line, every day at a certain time, and get the correct time placement?


    Possibly Useful links:


    UPDATE

    Thanks to PineCoders-LucF, I was able to get approximately what I wanted, with the following code:

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

    However, there are two issues wit this code.

    1. It doesn't use the nice timeframe.period functionality that allow you to specify certain days and time intervals in one place.
    2. It put the line in the "middle" of a candle so if you're on the 1H chart and want a line at 08.00 it will just place it in the middle. Thus not suitable as an alarm/signal, unless you use a <= 1 minute time frame.

    解决方案

    Version 1

    One version of the timestamp() function can use a timezone parameter:

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

    Chart is shown with UTC+1 times and indicator is set to "No Scale" not to disrupt price scale:

    Version 2

    With this version you can choose:

    • A from/to hour range
    • To show the line on weekdays only
    • Between bgcolor or vline mode

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

    这篇关于如何在Tradingview的pinescript中的某个时间画一条垂直线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆