f_get_period (_session, _start, _lookback) => result = math.max(_start, 1) for i = result to _lookback if na(_session[i+1]) and _session result := i+1 break result
else if session label.set_y(my_label, v_position_y) label.set_text(my_label, array.join(v_label, icon_separator))
data
// ------------------------ // Drawing Fibonacci levels // ------------------------ f_render_fibonacci (_show, data, _is_started, _is_ended, _x1, _x2, _color, _top, _bottom, _level, _width, _style) => var line my_line = na session = data.session()
if _show y = (_top - _bottom) * _level + _bottom
if _is_started my_line := line.new(_x1, y, _x2, y, width=_width, color=color.new(_color, 30), style=_style) array.push(data.fib, my_line)
if data.is_extended line.set_extend(my_line, extend.right)
else if session line.set_y1(my_line, y) line.set_y2(my_line, y)
f_set_line_x2(my_line, _x2) else if _is_ended f_set_line_x2(my_line, _x2-session_end_offset) data
// ------------------------ // Drawing Opening range // ------------------------ f_render_oprange (_show, Session data, _is_started, _is_ended, _x1, _x2, _color, _max) => var int start_time = na var box my_box = na var line my_line1 = na var line my_line2 = na var label my_label1 = na var label my_label2 = na var bool is_opened = false var float R1 = na, var float R2 = na var float S1 = na, var float S2 = na
var is_reached_R1 = false var is_reached_R2 = false var is_reached_S1 = false var is_reached_S2 = false var color_gray = ac.tradingview('gray') var color_orange = ac.tradingview('orange') var target_line_width = 2
if data.is_extended box.set_extend(my_box, extend.right)
alert('Opening range is fixed.', alert.freq_once_per_bar) else if is_opened if ta.crossover(high, R1) and (not is_reached_R1) is_reached_R1 := true
if i_o_breakout_icon label.new(bar_index, high, '×', yloc=yloc.price, style=label.style_label_center, color=TRANSPARENT, textcolor=ac.tradingview('blue'), size=size.large)
if ta.crossover(high, R2) and (not is_reached_R2) is_reached_R2 := true
if i_o_breakout_icon label.new(bar_index, high, '×', yloc=yloc.price, style=label.style_label_center, color=TRANSPARENT, textcolor=ac.tradingview('blue'), size=size.large)
if ta.crossunder(low, S1) and (not is_reached_S1) is_reached_S1 := true
if i_o_breakout_icon label.new(bar_index, low, '×', yloc=yloc.price, style=label.style_label_center, color=TRANSPARENT, textcolor=ac.tradingview('red'), size=size.large)
if ta.crossunder(low, S2) and (not is_reached_S2) is_reached_S2 := true
if i_o_breakout_icon label.new(bar_index, low, '×', yloc=yloc.price, style=label.style_label_center, color=TRANSPARENT, textcolor=ac.tradingview('red'), size=size.large) true else if is_crossover alert('Price crossed over the opening range', alert.freq_once_per_bar)
if i_alert2_show label.new(bar_index, box.get_top(my_box), "×", color=color.blue, textcolor=ac.tradingview('blue'), style=label.style_none, size=size.large)
if is_crossunder alert('Price crossed under the opening range', alert.freq_once_per_bar)
if i_alert2_show label.new(bar_index, box.get_bottom(my_box), "×", color=color.red, textcolor=ac.tradingview('red'), style=label.style_none, size=size.large)
else if _is_ended
if i_o_nowonly util.clear_lines(data.opr_lines, 0) util.clear_labels(data.opr_labels, 0) util.clear_boxes(data.opr_boxes, 0)
else if array.size(data.opr_lines) > 0 for i = 0 to 5 the_line = array.get(data.opr_lines, array.size(data.opr_lines) - (i + 1)) line.set_x2(the_line, _x2-session_end_offset)
for i = 0 to 1 the_label = array.get(data.opr_labels, array.size(data.opr_labels) - (i + 1)) label.set_text(the_label, '●') label.set_style(the_label, label.style_label_center) label.set_textcolor(the_label, i_o_color == option_candle_color2 ? label.get_y(the_label) >= saved_top ? ac.tradingview('blue') : ac.tradingview('red') : _color) label.set_color(the_label, TRANSPARENT) label.set_size(the_label, size.tiny) label.set_x(the_label,_x1)
// ------------------------ // Drawing candle // ------------------------ f_render_candle (_show, Session data, _is_started, _is_ended, _color, _top, _bottom, _open, _x1, _x2) => var box body = na var line wick1 = na var line wick2 = na session = data.session()
if data.is_extended box.set_extend(my_box, extend.right)
else if session box.set_top(my_box, _top) box.set_bottom(my_box, _bottom) f_set_box_right(my_box, _x2)
else if _is_ended box.set_right(my_box, bar_index-session_end_offset) data.add(_top[1] - _bottom[1], i_info_period)
data
f_render_dots (_show, Session data, _is_started, _is_ended, _color, _top, _bottom, _x1, _x2) => var float _open = na var box oc_box = na var line oc_line_u = na var line oc_line_l = na session = data.session()
if _show and i_sess_box_dots if _is_started oc_line_u := line.new(_x1, open , _x2, open , color=color.new(_color, 70), style=line.style_dotted) oc_line_l := line.new(_x1, close, _x2, close, color=color.new(_color, 70), style=line.style_dotted) linefill.new(oc_line_u, oc_line_l, color.new(_color, i_sess_bgopacity2))
if (i_sess1_chart != option_chart_x) and is_sess1 c_barcolor := i_sess1_barcolor ? c_sess1_barcolor : na c_plotcandle := i_sess1_plotcandle ? c_sess1_barcolor : na
if (i_sess2_chart != option_chart_x) and is_sess2 c_barcolor := i_sess2_barcolor ? c_sess2_barcolor : na c_plotcandle := i_sess2_plotcandle ? c_sess2_barcolor : na
if (i_sess3_chart != option_chart_x) and is_sess3 c_barcolor := i_sess3_barcolor ? c_sess3_barcolor : na c_plotcandle := i_sess3_plotcandle ? c_sess3_barcolor : na
if (i_sess4_chart != option_chart_x) and is_sess4 c_barcolor := i_sess4_barcolor ? c_sess4_barcolor : na c_plotcandle := i_sess4_plotcandle ? c_sess4_barcolor : na
if (i_sess5_chart != option_chart_x) and is_sess5 c_barcolor := i_sess5_barcolor ? c_sess5_barcolor : na c_plotcandle := i_sess5_plotcandle ? c_sess5_barcolor : na
//////////////////// // Alerts //////////////////// // Session alerts sess1_started = is_sess1 and not is_sess1[1], sess1_ended = not is_sess1 and is_sess1[1] sess2_started = is_sess2 and not is_sess2[1], sess2_ended = not is_sess2 and is_sess2[1] sess3_started = is_sess3 and not is_sess3[1], sess3_ended = not is_sess3 and is_sess3[1] sess4_started = is_sess4 and not is_sess4[1], sess4_ended = not is_sess4 and is_sess4[1] sess5_started = is_sess5 and not is_sess5[1], sess5_ended = not is_sess5 and is_sess5[1]
plotshape(i_alert3_show and (not is_sess1) and ta.crossover (close, sess1_ohlc.high) ? sess1_ohlc.high : na, 'cross sess1_high', color=i_sess1_color, style=shape.triangleup , location=location.absolute, size=size.tiny) plotshape(i_alert3_show and (not is_sess1) and ta.crossunder(close, sess1_ohlc.low) ? sess1_ohlc.low : na, 'cross sess1_low' , color=i_sess1_color, style=shape.triangledown, location=location.absolute, size=size.tiny) plotshape(i_alert3_show and (not is_sess2) and ta.crossover (close, sess2_ohlc.high) ? sess2_ohlc.high : na, 'cross sess2_high', color=i_sess2_color, style=shape.triangleup , location=location.absolute, size=size.tiny) plotshape(i_alert3_show and (not is_sess2) and ta.crossunder(close, sess2_ohlc.low) ? sess2_ohlc.low : na, 'cross sess2_low' , color=i_sess2_color, style=shape.triangledown, location=location.absolute, size=size.tiny) plotshape(i_alert3_show and (not is_sess3) and ta.crossover (close, sess3_ohlc.high) ? sess3_ohlc.high : na, 'cross sess3_high', color=i_sess3_color, style=shape.triangleup , location=location.absolute, size=size.tiny) plotshape(i_alert3_show and (not is_sess3) and ta.crossunder(close, sess3_ohlc.low) ? sess3_ohlc.low : na, 'cross sess3_low' , color=i_sess3_color, style=shape.triangledown, location=location.absolute, size=size.tiny) plotshape(i_alert3_show and (not is_sess4) and ta.crossover (close, sess4_ohlc.high) ? sess4_ohlc.high : na, 'cross sess4_high', color=i_sess4_color, style=shape.triangleup , location=location.absolute, size=size.tiny) plotshape(i_alert3_show and (not is_sess4) and ta.crossunder(close, sess4_ohlc.low) ? sess4_ohlc.low : na, 'cross sess4_low' , color=i_sess4_color, style=shape.triangledown, location=location.absolute, size=size.tiny)
////////////// // Analysis // var text_color = #e1e2e4 var bgcolor = #1a1c20 var color_green = ac.monokai('green') var color_orange = ac.monokai('orange') var color_red = ac.monokai('red') var color_ind_green = ac.tradingview('green') var color_ind_gray = color.new(ac.tradingview('gray'), 30) var color_white = #e1e2e4
i_show_op_analysis = false//input.bool(true, 'Opening range table') i_show_analysis2 = false//input.bool(false)
if i_show_info and barstate.islast data = array.new<Session>()
if array.size(sess1_data.price_ranges) > 0 data.push(sess1_data) if array.size(sess2_data.price_ranges) > 0 data.push(sess2_data) if array.size(sess3_data.price_ranges) > 0 data.push(sess3_data) if array.size(sess4_data.price_ranges) > 0 data.push(sess4_data)
if array.size(sess1_data.price_ranges) > 0 and array.size(sess3_data.price_ranges) > 0 for i = 0 to array.size(sess1_data.price_ranges) - 1 s1 = array.get(sess1_data.price_ranges, i) s3 = array.get(sess3_data.price_ranges, i)
timezone_offset_hours = input.int(0, "Timezone UTC Offset Hours", group="Time Zone Offset", minval=-12, maxval=14) timezone_offset_minutes = input.int(0, "Timezone UTC Offset Minutes", group="Time Zone Offset", minval=-59, maxval=59)
count = input(3, "Count") colorTop = input(color.green, "Top Color", group="STYLING") colorBetween = input(color.new(color.gray, 50), "Between Color", group="STYLING") colorBottom = input(color.red, "Bottom Color", group="STYLING") colorRange = input(color.blue, "Range Color", group="STYLING") rangeIsDotted = input(true, "Dash Line Style For The Range", group="STYLING") plot_mid = input(true, "Plot Mid Lines") close_to_close = input(false, "Close To Close (instead of low to high distance)")
// --- Variable Initialization --- var float range_high = na var float range_low = na var float distance = na var int startIDX = na
// Function to calculate adjusted time based on timezone offset getAdjustedTime(hour, minute, offset_hours, offset_minutes) => total_minutes = hour * 60 + minute + offset_hours * 60 + offset_minutes [total_minutes / 60 % 24, total_minutes % 60]
// Function to normalize time to minutes from the start of the week normalizeTimeToWeek(hour, minute) => (dayofweek - 1) * 24 * 60 + hour * 60 + minute
// Normalize times to minutes from the start of the week start_time_minutes = normalizeTimeToWeek(adjusted_start_hour, adjusted_start_minute) end_time_minutes = normalizeTimeToWeek(adjusted_end_hour, adjusted_end_minute)
// Adjust the end time if the range spans over midnight if adjusted_start_hour > adjusted_end_hour or (adjusted_start_hour == adjusted_end_hour and adjusted_start_minute > adjusted_end_minute) end_time_minutes := end_time_minutes + 7 * 24 * 60 // Add 7 days worth of minutes
// Current time normalized to minutes from the start of the week current_time_minutes = normalizeTimeToWeek(hour, minute)
// Detect if the current time is before the start of the week normalized time if current_time_minutes < start_time_minutes current_time_minutes := current_time_minutes + 7 * 24 * 60 // Adjust for new week
// Determine if we are in the plotting range isInRange = current_time_minutes >= start_time_minutes and current_time_minutes <= end_time_minutes
// Main Logic if isInRange if na(range_high) // If this is the first bar to plot startIDX := bar_index range_high := close_to_close ? close : high range_low := close_to_close ? close : low else range_high := math.max(range_high, close_to_close ? close : high) range_low := math.min(range_low, close_to_close ? close : low) distance := range_high - range_low else if not na(range_high) // If we have completed the range // Draw the range lines line.new(startIDX, range_high, bar_index[1], range_high, xloc=xloc.bar_index, style=rangeIsDotted ? line.style_dashed : line.style_solid, color=colorRange) line.new(startIDX, range_low, bar_index[1], range_low, xloc=xloc.bar_index, style=rangeIsDotted ? line.style_dashed : line.style_solid, color=colorRange)
// Draw additional lines based on the count input for i = 1 to count line.new(startIDX, range_high + i * distance, bar_index[1], range_high + i * distance, xloc=xloc.bar_index, style=line.style_solid, color=colorTop) line.new(startIDX, range_low - i * distance, bar_index[1], range_low - i * distance, xloc=xloc.bar_index, style=line.style_solid, color=colorBottom) if plot_mid line.new(startIDX, range_high + (i - 0.5) * distance, bar_index[1], range_high + (i - 0.5) * distance, xloc=xloc.bar_index, style=line.style_dashed, color=colorBetween) line.new(startIDX, range_low - (i - 0.5) * distance, bar_index[1], range_low - (i - 0.5) * distance, xloc=xloc.bar_index, style=line.style_dashed, color=colorBetween)
// Reset the variables after plotting range_high := na range_low := na distance := na startIDX := na