Search code examples
plotlinedrawpine-script

Draw vertical line at the first bar of the month in tradingview's pine script


I'd like to draw a vertical dashed line at every first bar of a month (for daily and weekly charts) or a year (for monthly charts). How can I do that? I don't know how to get those bar indexes.

Addition: I read in this post that "line.new" has a restriction to 54 lines max per indicator. But when using the plot function in histogram style (=plot only single vertical histogram bars), I cannot plot dashed lines... and I prefer a solution with dashed lines...


Solution

  • This is an adaptation of midtownsk8rguy's code here:

    //@version=4
    study("Periodic Vline", overlay=true)
    p = timeframe.ismonthly ? "12M" : "M"
    vline(BarIndex, Color, LineStyle, LineWidth) =>
        return = line.new(BarIndex, low - tr, BarIndex, high + tr, xloc.bar_index, extend.both, Color, LineStyle, LineWidth)
    if change(time(p))
        vline(bar_index, #FF800080, line.style_dashed, 1)