Expectation: Plotting previous day high is an easy task. But the following code is written to get previous day high and if todays close is crossed the previous day high line it should break high line at that point.
Issue: The yellow line is the high line. It starts from the previous bar index, not from previous day high bar index or from previous day start.
What I need: The high line should start from previous day high point bar index or from previous day start. Also please correct if there is any unnecessay codes written or any thing that can be simplified.
Picture:
Code:
//@version=5
indicator(title='HL', shorttitle='HL', overlay=true)
i_htf = 'D'
i_show_last = 1
i_ln_h_offset = 20
i_hide_crossed = true
[high_htf] = request.security(symbol=syminfo.tickerid, timeframe=i_htf, expression=[high[1]], lookahead=barmerge.lookahead_on)
// Check if the current bar breaks a price level
break_line(price) =>
(close[1] < price and close >= price) or (close[1] > price and close <= price)
var float last_high = na
var int last_index = na
var line[] keep_lines = array.new_line()
var line[] all_lines = array.new_line()
if barstate.isconfirmed
_line = line.new(x1=last_index, y1=last_high, x2=bar_index + 10, y2=last_high, color=#ffff00)
array.push(all_lines, _line)
if array.size(all_lines) > i_show_last
removed_line = array.shift(all_lines)
if array.includes(keep_lines, removed_line)
index_of_removed_line = array.indexof(keep_lines, removed_line)
array.remove(keep_lines, index_of_removed_line)
line.delete(removed_line)
last_high := high_htf
last_index := bar_index
// Check if the current bar breaks the line
for current_line in all_lines
if array.includes(keep_lines, current_line)
continue
line.set_x2(current_line, bar_index + i_ln_h_offset)
line_price = line.get_y1(current_line)
if break_line(line_price)
line.set_x2(current_line, bar_index)
array.push(keep_lines, current_line)
// Remove crossed lines
for keep_line in keep_lines
line_price = line.get_y1(keep_line)
if i_hide_crossed and break_line(line_price)
line.delete(keep_line)
int index = array.indexof(all_lines, keep_line)
if not na(index)
array.remove(all_lines, index)
The following code is team effort by Sharad Gaikwad and me, we received this question about two days ago in the Telegram PineCoders Q&A section. Sharad did build the structure and I added the repeating part.
//@version=5
indicator("My script", overlay = true, max_lines_count = 500)
type UDT_lines
line upper
bool upper_extend = true
line lower
bool lower_extend = true
var historical_lines = array.new<UDT_lines>()
[prevDayHigh, prevDayLow, _time] = request.security(syminfo.tickerid, "D", [high[1], low[1], time[1]], lookahead = barmerge.lookahead_on)
var hi_today = 0.
var lo_today = 0.
int lines_back = input.int(3, "Max Historical Lines Back", 1, 250)
if(timeframe.change('D'))
my_lines = UDT_lines.new()
my_lines.upper := line.new(_time, prevDayHigh, time, prevDayHigh, xloc = xloc.bar_time, color = color.green)
my_lines.lower := line.new(_time, prevDayLow , time, prevDayLow , xloc = xloc.bar_time, color = color.red)
historical_lines.unshift(my_lines)
hi_today := high
lo_today := low
if historical_lines.size() > lines_back
old_lines = historical_lines.pop()
old_lines.upper.delete()
old_lines.lower.delete()
hi_today := math.max(hi_today, high)
lo_today := math.min(lo_today, low)
for old_lines in historical_lines
if hi_today < old_lines.upper.get_y1() and old_lines.upper_extend
old_lines.upper.set_x2(time)
else
old_lines.upper_extend := false
if lo_today > old_lines.lower.get_y1() and old_lines.lower_extend
old_lines.lower.set_x2(time)
else
old_lines.lower_extend := false