0

Description

I would like to plot a horizontal ray from the end of each Anchored VWAP for the Daily, Weekly, Monthly time periods, and ending when price action returns to the level (i.e., each naked VWAP).

The sample script I have for this is here:

//@version=5
indicator("nVWAP(s)", overlay=true)

newWeek = ta.change(time("W")) != 0

vwap_w = ta.vwap(open, timeframe.change("1W"))
wvwapClose = ta.valuewhen(newWeek, vwap_w[1], 0)
plot(wvwapClose, color = color.blue)
plot(vwap_w, color = color.blue)

Problem

The problem is that the line only extends until the next time period and then prints vertical to the next one. I am not sure how to get the lines to print and end when price action revisits that value, and without printing vertical from period to period.

It needs to look like this (an example from a different indicator). Notice that only the lines that have not been revisited are showing. Although I would like to be able to toggle it like this, but that might be out of scope for this question.

Reference Material

  • VWAP Market Session Anchored which helped me understand how to plot intra-day anchored VWAPs and why I have use different logic for longer time periods.
  • Pine Scriptâ„¢ language reference manual for basic anchored VWAP plotting for Daily, Weekly, Monthly time periods.
  • I studied the Multi-Time Period Charts indicator script which eventually got me on the track of trying to use the ta.change() function but I ran into issues when trying to use the plot() function inside an if statement.
  • This stackoverflow question/answer showed me how to use the ta.change function for plotting the VWAP closing values.
  • I also figured out I can't simply index the last value of the series returned by ta.vwap() with this stackoverflow question/answer.

1 Answers1

0

Can be done with line function:

//@version=5
indicator("nVWAP(s)", overlay=true)

newWeek = ta.change(time("W")) != 0

vwap_w = ta.vwap(open, timeframe.change("1W"))
wvwapClose = ta.valuewhen(newWeek, vwap_w[1], 0)

numberOfBarsInWeek = ta.valuewhen(newWeek, bar_index, 0) - ta.valuewhen(newWeek, bar_index, 1)

if newWeek
    vwap_w_line = line.new(bar_index, vwap_w, bar_index + numberOfBarsInWeek, vwap_w, color = color.aqua)
    wvwapClose_line = line.new(bar_index, wvwapClose, bar_index + numberOfBarsInWeek, wvwapClose, color = color.orange)
mr_statler
  • 1,913
  • 2
  • 3
  • 14