OPEN-SOURCE SCRIPT

Previous Day/Week/Month Rays

//version=5
indicator("Previous Day/Week/Month Rays", overlay=true)

// Fetch the high/low for the previous day, week, and month
prevDayHigh = request.security(syminfo.tickerid, "D", high[1], lookahead=barmerge.lookahead_on)
prevDayLow = request.security(syminfo.tickerid, "D", low[1], lookahead=barmerge.lookahead_on)

prevWeekHigh = request.security(syminfo.tickerid, "W", high[1], lookahead=barmerge.lookahead_on)
prevWeekLow = request.security(syminfo.tickerid, "W", low[1], lookahead=barmerge.lookahead_on)

prevMonthHigh = request.security(syminfo.tickerid, "M", high[1], lookahead=barmerge.lookahead_on)
prevMonthLow = request.security(syminfo.tickerid, "M", low[1], lookahead=barmerge.lookahead_on)

// Determine time points (UNIX timestamps) for anchoring the rays
prevDayTime = request.security(syminfo.tickerid, "D", time[1], lookahead=barmerge.lookahead_on)
prevWeekTime = request.security(syminfo.tickerid, "W", time[1], lookahead=barmerge.lookahead_on)
prevMonthTime = request.security(syminfo.tickerid, "M", time[1], lookahead=barmerge.lookahead_on)

// Create variables to store the rays
var line dayHighRay = na
var line dayLowRay = na
var line weekHighRay = na
var line weekLowRay = na
var line monthHighRay = na
var line monthLowRay = na

// Create or update the rays using time for x-coordinates
if na(dayHighRay)
dayHighRay := line.new(x1=prevDayTime, y1=prevDayHigh, x2=timestamp("2100-01-01 00:00"), y2=prevDayHigh, color=color.green, width=1, extend=extend.right, xloc=xloc.bar_time)

if na(dayLowRay)
dayLowRay := line.new(x1=prevDayTime, y1=prevDayLow, x2=timestamp("2100-01-01 00:00"), y2=prevDayLow, color=color.green, width=1, extend=extend.right, xloc=xloc.bar_time)

if na(weekHighRay)
weekHighRay := line.new(x1=prevWeekTime, y1=prevWeekHigh, x2=timestamp("2100-01-01 00:00"), y2=prevWeekHigh, color=color.white, width=1, extend=extend.right, xloc=xloc.bar_time)

if na(weekLowRay)
weekLowRay := line.new(x1=prevWeekTime, y1=prevWeekLow, x2=timestamp("2100-01-01 00:00"), y2=prevWeekLow, color=color.white, width=1, extend=extend.right, xloc=xloc.bar_time)

if na(monthHighRay)
monthHighRay := line.new(x1=prevMonthTime, y1=prevMonthHigh, x2=timestamp("2100-01-01 00:00"), y2=prevMonthHigh, color=color.red, width=1, extend=extend.right, xloc=xloc.bar_time)

if na(monthLowRay)
monthLowRay := line.new(x1=prevMonthTime, y1=prevMonthLow, x2=timestamp("2100-01-01 00:00"), y2=prevMonthLow, color=color.red, width=1, extend=extend.right, xloc=xloc.bar_time)

// Update the rays dynamically
line.set_xy1(dayHighRay, prevDayTime, prevDayHigh)
line.set_xy2(dayHighRay, timestamp("2100-01-01 00:00"), prevDayHigh)

line.set_xy1(dayLowRay, prevDayTime, prevDayLow)
line.set_xy2(dayLowRay, timestamp("2100-01-01 00:00"), prevDayLow)

line.set_xy1(weekHighRay, prevWeekTime, prevWeekHigh)
line.set_xy2(weekHighRay, timestamp("2100-01-01 00:00"), prevWeekHigh)

line.set_xy1(weekLowRay, prevWeekTime, prevWeekLow)
line.set_xy2(weekLowRay, timestamp("2100-01-01 00:00"), prevWeekLow)

line.set_xy1(monthHighRay, prevMonthTime, prevMonthHigh)
line.set_xy2(monthHighRay, timestamp("2100-01-01 00:00"), prevMonthHigh)

line.set_xy1(monthLowRay, prevMonthTime, prevMonthLow)
line.set_xy2(monthLowRay, timestamp("2100-01-01 00:00"), prevMonthLow)

// Handle timeframe-specific logic
isMonthTimeframe = timeframe.ismonthly
isWeekTimeframe = timeframe.isweekly

// Remove irrelevant rays for higher timeframes
if isMonthTimeframe
line.delete(dayHighRay)
line.delete(dayLowRay)
line.delete(weekHighRay)
line.delete(weekLowRay)

if isWeekTimeframe
line.delete(dayHighRay)
line.delete(dayLowRay)
Candlestick analysisChart patterns

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Disclaimer