Indicator: Last Daily Candle High, Low, and Close
This TradingView script plots horizontal lines on the chart representing the high, low, and close values of the previous day's candle. Here’s how it works:
indicator("Last Daily Candle High, Low, and Close", overlay=true)
This line defines the name of the indicator ("Last Daily Candle High, Low, and Close") and specifies that the indicator will be displayed on the main price chart (overlay=true).
2. Fetching Data from the Previous Day
pinescript
Copy
Edit
prev_day_high = ta.valuewhen(timeframe.change("D"), high[1], 0) // Fetch yesterday's high
prev_day_low = ta.valuewhen(timeframe.change("D"), low[1], 0) // Fetch yesterday's low
prev_day_close = ta.valuewhen(timeframe.change("D"), close[1], 0) // Fetch yesterday's close
Fetching the High, Low, and Close:
The script fetches the high, low, and close of the previous day’s completed candle.
timeframe.change("D"): Detects when the day changes.
high[1], low[1], close[1]: These references access the high, low, and close of the previous day's candle (using [1] to access the prior bar's values).
ta.valuewhen: This function ensures the values are only fetched when the daily candle changes (i.e., once per day).
3. Initializing Line Variables
pinescript
Copy
Edit
var line high_line = na
var line low_line = na
var line close_line = na
Initializing Line Variables:
Three variables are declared to store the line objects for the high, low, and close levels of the previous day.
These variables are initialized as na, which means "Not Available," until the lines are created.
4. Updating Lines at the Start of Each Day
pinescript
Copy
Edit
if timeframe.change("D")
// Remove old lines
line.delete(high_line)
line.delete(low_line)
line.delete(close_line)
Detecting New Day:
The if timeframe.change("D") block ensures the script detects when a new day starts.
When a new day starts, it deletes the previous day's lines to ensure no overlap.
5. Drawing New Lines
pinescript
Copy
Edit
// Create new lines extending fully across the chart
high_line := line.new(x1=bar_index - 500, y1=prev_day_high, x2=bar_index + 500, y2=prev_day_high, color=color.red, width=2, extend=extend.both)
low_line := line.new(x1=bar_index - 500, y1=prev_day_low, x2=bar_index + 500, y2=prev_day_low, color=color.black, width=2, extend=extend.both)
close_line := line.new(x1=bar_index - 500, y1=prev_day_close, x2=bar_index + 500, y2=prev_day_close, color=color.blue, width=2, extend=extend.both)
Creating Horizontal Lines:
High Line: Drawn at the previous day's high, using a red color.
Low Line: Drawn at the previous day's low, using a black color.
Close Line: Drawn at the previous day's close, using a blue color.
Line Extensions:
The lines are extended from x1=bar_index - 500 to x2=bar_index + 500, meaning they will span a wide section of the chart from far left to far right, ensuring visibility.
extend=extend.both ensures the lines extend beyond the current bar in both directions.
Summary:
This TradingView indicator draws horizontal lines representing the high, low, and close levels of the previous day's candle. The lines are updated daily, with each new day resetting the previous lines and drawing new ones at the appropriate levels for that day’s high, low, and close prices.
This tool helps traders quickly identify important price levels from the previous trading day, which can act as potential support or resistance levels.