Create an TradingView Indicator that highligh each day with starting and ending range…
- User
Create an TradingView Indicator that highligh each day with starting and ending range for week with Name Label
- Assistant
Create an TradingView Indicator that highligh each day with starting and ending range for week with Name Label
Certainly! Below is a TradingView indicator written in Pine Script that highlights each week's price range on your chart. It marks the starting and ending ranges of each week with labeled boxes and adds name labels indicating the start and end of the week.
This indicator will:
Here's the code for the indicator:
//@version=5
indicator("Weekly Range Highlighter", overlay=true)
// Get the current and previous week numbers
week_number = weekofyear(time)
prev_week_number = week_number[1]
// Detect the start of a new week
new_week = week_number != prev_week_number
// Initialize variables to store weekly high, low, and starting bar index
var int week_start_index = na
var float weekly_high = na
var float weekly_low = na
var string week_label = ""
// On the first bar, initialize the variables
if barstate.isfirst
week_start_index := bar_index
weekly_high := high
weekly_low := low
// Format the week label with the starting date
week_label := "Week of " + tostring(time, "MMM dd")
// Add a label at the start of the first week
label.new(bar_index, high, "Start of " + week_label, color=color.green, style=label.style_label_down, textcolor=color.white)
else
if new_week
// At the first bar of the new week, draw the box and label for the previous week
// Create a label at the end of the previous week
label.new(bar_index[1], low[1], "End of " + week_label[1], color=color.red, style=label.style_label_up, textcolor=color.white)
// Draw a box representing the weekly range
box.new(
left = week_start_index,
top = weekly_high,
right = bar_index[1],
bottom = weekly_low,
bgcolor = color.new(color.blue, 85),
border_color= color.blue
)
// Reset variables for the new week
week_start_index := bar_index
weekly_high := high
weekly_low := low
week_label := "Week of " + tostring(time, "MMM dd")
// Add a label at the start of the new week
label.new(bar_index, high, "Start of " + week_label, color=color.green, style=label.style_label_down, textcolor=color.white)
else
// Update the weekly high and low
weekly_high := math.max(weekly_high, high)
weekly_low := math.min(weekly_low, low)
Detecting New Weeks: The indicator uses the weekofyear function to determine the current week number. By comparing the current week number to the previous bar's week number, it detects when a new week starts.
Tracking Weekly Highs and Lows: It maintains variables for the weekly high and low prices, updating them with each bar within the week.
Drawing Boxes: When a new week is detected, the script draws a box (box.new) that spans from the start to the end of the previous week and from the weekly high to the weekly low. The box visually highlights the price range for that week.
Adding Labels: The indicator adds labels at the high of the first bar of the week ("Start of Week of MMM dd") and at the low of the last bar of the week ("End of Week of MMM dd"), where "MMM dd" is the formatted date (e.g., "Jan 01").
Add the Indicator to Your Chart:
Customize the Appearance (Optional):
color parameters in the code.color.new(color.blue, 85). Lower values are more transparent.Feel free to customize the indicator to better suit your needs, such as changing the label texts or adjusting how the weekly range is calculated. If you have any questions or need further assistance, don't hesitate to ask!
Not yet included in a published release.