institutional-levels-v7.pine
Copy
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0
// https://mozilla.org/MPL/2.0/
// © Afthab - Institutional Levels Dashboard v7
//@version=6
indicator("Institutional Levels Dashboard v7 [AFTHAB]", overlay=true, max_lines_count=500, max_labels_count=500, max_boxes_count=500)
// ─── INPUTS ───────────────────────────────────────────────
grp_dash = "Dashboard Settings"
show_dash = input.bool(true, "Show Dashboard Table", group=grp_dash)
dash_pos = input.string("Top Right", "Dashboard Position", options=["Top Right", "Top Left", "Bottom Right", "Bottom Left", "Middle Right", "Middle Left"], group=grp_dash)
dash_size = input.string("Normal", "Text Size", options=["Tiny", "Small", "Normal", "Large"], group=grp_dash)
grp_lines = "Line Settings"
show_pdhl = input.bool(true, "Show Prev Day H/L Lines", group=grp_lines)
show_pwhl = input.bool(true, "Show Prev Week H/L Lines", group=grp_lines)
show_pmhl = input.bool(true, "Show Prev Month H/L Lines", group=grp_lines)
show_pqhl = input.bool(true, "Show Prev Quarter H/L Lines", group=grp_lines)
show_round = input.bool(true, "Show Round Number Lines", group=grp_lines)
show_imp = input.bool(true, "Show Important Levels Lines", group=grp_lines)
line_ext = input.int(40, "Line Extension (bars)", minval=10, maxval=100, group=grp_lines, tooltip="How far left/right lines extend from current bar")
grp_pday = "Previous Same-Day Settings"
show_prev_same_day = input.bool(true, "Show Previous Same-Day H/L", group=grp_pday)
prev_day_line_style = input.string("Solid", "Line Style", options=["Solid", "Dashed", "Dotted"], group=grp_pday)
prev_day_h_color = input.color(color.new(#00E5FF, 0), "Prev Same-Day High Color", group=grp_pday)
prev_day_l_color = input.color(color.new(#FF6D00, 0), "Prev Same-Day Low Color", group=grp_pday)
prev_day_linewidth = input.int(2, "Line Width", minval=1, maxval=4, group=grp_pday)
grp_round = "Round Number Settings"
round_step = input.float(20.0, "Round Number Step", minval=1.0, group=grp_round)
round_count = input.int(3, "Round Levels Above", minval=1, maxval=5, group=grp_round)
round_count_below = input.int(3, "Round Levels Below", minval=1, maxval=5, group=grp_round)
grp_trend = "Trend Settings"
trend_ema_fast = input.int(20, "Fast EMA", minval=5, group=grp_trend)
trend_ema_slow = input.int(50, "Slow EMA", minval=10, group=grp_trend)
// ─── HELPER: POSITION / SIZE / STYLE ─────────────────────
f_pos(p) =>
switch p
"Top Right" => position.top_right
"Top Left" => position.top_left
"Bottom Right" => position.bottom_right
"Bottom Left" => position.bottom_left
"Middle Right" => position.middle_right
"Middle Left" => position.middle_left
f_size(s) =>
switch s
"Tiny" => size.tiny
"Small" => size.small
"Normal" => size.normal
"Large" => size.large
f_linestyle(s) =>
switch s
"Solid" => line.style_solid
"Dashed" => line.style_dashed
"Dotted" => line.style_dotted
f_dow_name(d) =>
switch d
1 => "Sunday"
2 => "Monday"
3 => "Tuesday"
4 => "Wednesday"
5 => "Thursday"
6 => "Friday"
7 => "Saturday"
=> "Unknown"
// ─── HTF DATA ─────────────────────────────────────────────
[pd_h, pd_l] = request.security(syminfo.tickerid, "D", [high[1], low[1]], lookahead=barmerge.lookahead_on)
[cd_h, cd_l] = request.security(syminfo.tickerid, "D", [high, low], lookahead=barmerge.lookahead_on)
[pw_h, pw_l] = request.security(syminfo.tickerid, "W", [high[1], low[1]], lookahead=barmerge.lookahead_on)
[pm_h, pm_l] = request.security(syminfo.tickerid, "M", [high[1], low[1]], lookahead=barmerge.lookahead_on)
[pq_h, pq_l] = request.security(syminfo.tickerid, "3M", [high[1], low[1]], lookahead=barmerge.lookahead_on)
// ─── TREND DETECTION ──────────────────────────────────────
ema_f = ta.ema(close, trend_ema_fast)
ema_s = ta.ema(close, trend_ema_slow)
trend_bull = ema_f > ema_s
trend_str = trend_bull ? "▲ Bullish" : "▼ Bearish"
trend_col = trend_bull ? color.green : color.red
// ─── IMPORTANT LEVELS (Midpoints) ────────────────────────
imp_level_1 = math.avg(pd_h, pd_l)
imp_level_2 = math.avg(pw_h, pw_l)
// ─── ROUND NUMBERS ────────────────────────────────────────
cur_price = close
base_round = math.floor(cur_price / round_step) * round_step
// ─── PREVIOUS SAME-DAY-OF-WEEK HIGH/LOW ──────────────────
var float prev_same_day_high = na
var float prev_same_day_low = na
var string prev_same_day_name = ""
cur_dow_raw = request.security(syminfo.tickerid, "D", dayofweek(time, syminfo.timezone), lookahead=barmerge.lookahead_on)
cur_dow = (cur_dow_raw == 1 or cur_dow_raw == 7) ? dayofweek(time, syminfo.timezone) : cur_dow_raw
[d_high_prev, d_low_prev, d_dow_prev] = request.security(syminfo.tickerid, "D", [high[1], low[1], dayofweek(time[1], syminfo.timezone)], lookahead=barmerge.lookahead_on)
var float[] daily_highs = array.new_float(0)
var float[] daily_lows = array.new_float(0)
var int[] daily_dows = array.new_int(0)
is_new_day = ta.change(time("D")) != 0
if is_new_day
if not na(d_high_prev) and not na(d_low_prev) and not na(d_dow_prev)
array.push(daily_highs, d_high_prev)
array.push(daily_lows, d_low_prev)
array.push(daily_dows, d_dow_prev)
if array.size(daily_highs) > 60
array.shift(daily_highs)
array.shift(daily_lows)
array.shift(daily_dows)
f_find_prev_same_dow() =>
float found_h = na
float found_l = na
int sz = array.size(daily_dows)
if sz >= 1
for i = sz - 1 to 0
if array.get(daily_dows, i) == cur_dow
found_h := array.get(daily_highs, i)
found_l := array.get(daily_lows, i)
break
[found_h, found_l]
[psd_h, psd_l] = f_find_prev_same_dow()
prev_same_day_high := psd_h
prev_same_day_low := psd_l
prev_same_day_name := f_dow_name(cur_dow)
// ─── PROXIMITY DETECTION ─────────────────────────────────
prox_pct = 0.0015
f_near(price, level) =>
not na(level) and math.abs(price - level) / level < prox_pct
near_pdh = f_near(close, pd_h)
near_pdl = f_near(close, pd_l)
near_pwh = f_near(close, pw_h)
near_pwl = f_near(close, pw_l)
near_pmh = f_near(close, pm_h)
near_pml = f_near(close, pm_l)
near_pqh = f_near(close, pq_h)
near_pql = f_near(close, pq_l)
// ─── DRAW LINES ───────────────────────────────────────────
var line l_pdh = na, var line l_pdl = na
var line l_pwh = na, var line l_pwl = na
var line l_pmh = na, var line l_pml = na
var line l_pqh = na, var line l_pql = na
var line l_imp1 = na, var line l_imp2 = na
var line l_psd_h = na, var line l_psd_l = na
var line[] round_lines_above = array.new_line(0)
var line[] round_lines_below = array.new_line(0)
var label[] round_labels_above = array.new_label(0)
var label[] round_labels_below = array.new_label(0)
var label lb_pdh = na, var label lb_pdl = na
var label lb_pwh = na, var label lb_pwl = na
var label lb_pmh = na, var label lb_pml = na
var label lb_pqh = na, var label lb_pql = na
var label lb_imp1 = na, var label lb_imp2 = na
var label lb_psd_h = na, var label lb_psd_l = na
f_update_line(ln, lvl, col, w, sty) =>
result_line = line(na)
if not na(lvl)
line.delete(ln)
result_line := line.new(bar_index - line_ext, lvl, bar_index + line_ext, lvl, color=col, width=w, style=sty)
result_line
f_update_label(lb, lvl, txt, col) =>
result_label = label(na)
if not na(lvl)
label.delete(lb)
result_label := label.new(bar_index + line_ext + 2, lvl, txt, style=label.style_label_left, color=color.new(color.black, 70), textcolor=col, size=size.small)
result_label
if barstate.islast
if show_pdhl
l_pdh := f_update_line(l_pdh, pd_h, color.red, 2, line.style_solid)
l_pdl := f_update_line(l_pdl, pd_l, color.green, 2, line.style_solid)
lb_pdh := f_update_label(lb_pdh, pd_h, "PDH " + str.tostring(pd_h, format.mintick), color.red)
lb_pdl := f_update_label(lb_pdl, pd_l, "PDL " + str.tostring(pd_l, format.mintick), color.green)
if show_pwhl
l_pwh := f_update_line(l_pwh, pw_h, color.yellow, 2, line.style_solid)
l_pwl := f_update_line(l_pwl, pw_l, color.teal, 2, line.style_solid)
lb_pwh := f_update_label(lb_pwh, pw_h, "PWH " + str.tostring(pw_h, format.mintick), color.yellow)
lb_pwl := f_update_label(lb_pwl, pw_l, "PWL " + str.tostring(pw_l, format.mintick), color.teal)
if show_pmhl
l_pmh := f_update_line(l_pmh, pm_h, #FF00FF, 2, line.style_solid)
l_pml := f_update_line(l_pml, pm_l, #FF00FF, 2, line.style_solid)
lb_pmh := f_update_label(lb_pmh, pm_h, "PMH " + str.tostring(pm_h, format.mintick), #FF00FF)
lb_pml := f_update_label(lb_pml, pm_l, "PML " + str.tostring(pm_l, format.mintick), #FF00FF)
if show_pqhl
l_pqh := f_update_line(l_pqh, pq_h, color.white, 2, line.style_solid)
l_pql := f_update_line(l_pql, pq_l, color.white, 2, line.style_solid)
lb_pqh := f_update_label(lb_pqh, pq_h, "PQH " + str.tostring(pq_h, format.mintick), color.white)
lb_pql := f_update_label(lb_pql, pq_l, "PQL " + str.tostring(pq_l, format.mintick), color.white)
if show_imp
l_imp1 := f_update_line(l_imp1, imp_level_1, color.blue, 1, line.style_solid)
l_imp2 := f_update_line(l_imp2, imp_level_2, color.purple, 1, line.style_solid)
lb_imp1 := f_update_label(lb_imp1, imp_level_1, "PD Mid " + str.tostring(imp_level_1, format.mintick), color.blue)
lb_imp2 := f_update_label(lb_imp2, imp_level_2, "PW Mid " + str.tostring(imp_level_2, format.mintick), color.purple)
if show_prev_same_day and not na(prev_same_day_high)
psd_sty = f_linestyle(prev_day_line_style)
l_psd_h := f_update_line(l_psd_h, prev_same_day_high, prev_day_h_color, prev_day_linewidth, psd_sty)
l_psd_l := f_update_line(l_psd_l, prev_same_day_low, prev_day_l_color, prev_day_linewidth, psd_sty)
lb_psd_h := f_update_label(lb_psd_h, prev_same_day_high, "Prev " + prev_same_day_name + " H " + str.tostring(prev_same_day_high, format.mintick), prev_day_h_color)
lb_psd_l := f_update_label(lb_psd_l, prev_same_day_low, "Prev " + prev_same_day_name + " L " + str.tostring(prev_same_day_low, format.mintick), prev_day_l_color)
// ── Round Numbers with Labels ──
if show_round
if array.size(round_lines_above) > 0
for i = 0 to array.size(round_lines_above) - 1
line.delete(array.get(round_lines_above, i))
array.clear(round_lines_above)
if array.size(round_lines_below) > 0
for i = 0 to array.size(round_lines_below) - 1
line.delete(array.get(round_lines_below, i))
array.clear(round_lines_below)
if array.size(round_labels_above) > 0
for i = 0 to array.size(round_labels_above) - 1
label.delete(array.get(round_labels_above, i))
array.clear(round_labels_above)
if array.size(round_labels_below) > 0
for i = 0 to array.size(round_labels_below) - 1
label.delete(array.get(round_labels_below, i))
array.clear(round_labels_below)
for i = 1 to round_count
r_lvl = base_round + (round_step * i)
r_ln = line.new(bar_index - line_ext, r_lvl, bar_index + line_ext, r_lvl, color=color.new(color.gray, 40), width=1, style=line.style_solid)
array.push(round_lines_above, r_ln)
r_lb = label.new(bar_index + line_ext + 2, r_lvl, str.tostring(r_lvl, format.mintick), style=label.style_label_left, color=color.new(color.black, 85), textcolor=color.new(color.gray, 30), size=size.tiny)
array.push(round_labels_above, r_lb)
for i = 0 to round_count_below - 1
r_lvl = base_round - (round_step * i)
r_ln = line.new(bar_index - line_ext, r_lvl, bar_index + line_ext, r_lvl, color=color.new(color.gray, 40), width=1, style=line.style_solid)
array.push(round_lines_below, r_ln)
r_lb = label.new(bar_index + line_ext + 2, r_lvl, str.tostring(r_lvl, format.mintick), style=label.style_label_left, color=color.new(color.black, 85), textcolor=color.new(color.gray, 30), size=size.tiny)
array.push(round_labels_below, r_lb)
// ─── DASHBOARD TABLE ──────────────────────────────────────
if show_dash and barstate.islast
tbl_pos = f_pos(dash_pos)
tbl_size = f_size(dash_size)
total_rows = 22 + round_count + round_count_below
var table dash = table.new(tbl_pos, 2, total_rows, bgcolor=color.new(#1a1a2e, 10), border_color=color.new(color.gray, 60), border_width=1, frame_color=color.new(#16213e, 0), frame_width=2)
row = 0
table.cell(dash, 0, row, "⚡ Institutional Levels v7", text_color=color.white, text_size=tbl_size, bgcolor=color.new(#16213e, 0), text_halign=text.align_center)
table.cell(dash, 1, row, "", bgcolor=color.new(#16213e, 0))
table.merge_cells(dash, 0, row, 1, row)
row += 1
table.cell(dash, 0, row, "Trend (" + str.tostring(trend_ema_fast) + "/" + str.tostring(trend_ema_slow) + ")", text_color=color.white, text_size=tbl_size, bgcolor=color.new(#0d1117, 0), text_halign=text.align_center)
table.cell(dash, 1, row, trend_str, text_color=trend_col, text_size=tbl_size, bgcolor=color.new(#0d1117, 0), text_halign=text.align_center)
row += 1
table.cell(dash, 0, row, "Today High", text_color=color.green, text_size=tbl_size, bgcolor=color.new(#0d1117, 0), text_halign=text.align_center)
table.cell(dash, 1, row, str.tostring(cd_h, format.mintick), text_color=color.green, text_size=tbl_size, bgcolor=color.new(#0d1117, 0), text_halign=text.align_center)
row += 1
table.cell(dash, 0, row, "Today Low", text_color=color.red, text_size=tbl_size, bgcolor=color.new(#0d1117, 0), text_halign=text.align_center)
table.cell(dash, 1, row, str.tostring(cd_l, format.mintick), text_color=color.red, text_size=tbl_size, bgcolor=color.new(#0d1117, 0), text_halign=text.align_center)
row += 1
table.cell(dash, 0, row, "Midpoint Levels", text_color=color.white, text_size=tbl_size, bgcolor=color.new(#16213e, 0), text_halign=text.align_center)
table.cell(dash, 1, row, "", bgcolor=color.new(#16213e, 0))
table.merge_cells(dash, 0, row, 1, row)
row += 1
table.cell(dash, 0, row, "PD Mid", text_color=color.blue, text_size=tbl_size, bgcolor=color.new(#0d1117, 0), text_halign=text.align_center)
table.cell(dash, 1, row, str.tostring(imp_level_1, format.mintick), text_color=color.blue, text_size=tbl_size, bgcolor=color.new(#0d1117, 0), text_halign=text.align_center)
row += 1
table.cell(dash, 0, row, "PW Mid", text_color=color.purple, text_size=tbl_size, bgcolor=color.new(#0d1117, 0), text_halign=text.align_center)
table.cell(dash, 1, row, str.tostring(imp_level_2, format.mintick), text_color=color.purple, text_size=tbl_size, bgcolor=color.new(#0d1117, 0), text_halign=text.align_center)
row += 1
table.cell(dash, 0, row, "Key Timeframe Levels", text_color=color.white, text_size=tbl_size, bgcolor=color.new(#16213e, 0), text_halign=text.align_center)
table.cell(dash, 1, row, "", bgcolor=color.new(#16213e, 0))
table.merge_cells(dash, 0, row, 1, row)
row += 1
pdh_bg = near_pdh ? color.new(color.red, 70) : color.new(#0d1117, 0)
pdl_bg = near_pdl ? color.new(color.green, 70) : color.new(#0d1117, 0)
table.cell(dash, 0, row, "PDH", text_color=color.red, text_size=tbl_size, bgcolor=pdh_bg, text_halign=text.align_center)
table.cell(dash, 1, row, str.tostring(pd_h, format.mintick), text_color=color.red, text_size=tbl_size, bgcolor=pdh_bg, text_halign=text.align_center)
row += 1
table.cell(dash, 0, row, "PDL", text_color=color.green, text_size=tbl_size, bgcolor=pdl_bg, text_halign=text.align_center)
table.cell(dash, 1, row, str.tostring(pd_l, format.mintick), text_color=color.green, text_size=tbl_size, bgcolor=pdl_bg, text_halign=text.align_center)
row += 1
pwh_bg = near_pwh ? color.new(color.yellow, 70) : color.new(#0d1117, 0)
pwl_bg = near_pwl ? color.new(color.teal, 70) : color.new(#0d1117, 0)
table.cell(dash, 0, row, "PWH", text_color=color.yellow, text_size=tbl_size, bgcolor=pwh_bg, text_halign=text.align_center)
table.cell(dash, 1, row, str.tostring(pw_h, format.mintick), text_color=color.yellow, text_size=tbl_size, bgcolor=pwh_bg, text_halign=text.align_center)
row += 1
table.cell(dash, 0, row, "PWL", text_color=color.teal, text_size=tbl_size, bgcolor=pwl_bg, text_halign=text.align_center)
table.cell(dash, 1, row, str.tostring(pw_l, format.mintick), text_color=color.teal, text_size=tbl_size, bgcolor=pwl_bg, text_halign=text.align_center)
row += 1
pmh_bg = near_pmh ? color.new(#FF00FF, 70) : color.new(#0d1117, 0)
pml_bg = near_pml ? color.new(#FF00FF, 70) : color.new(#0d1117, 0)
table.cell(dash, 0, row, "PMH", text_color=#FF00FF, text_size=tbl_size, bgcolor=pmh_bg, text_halign=text.align_center)
table.cell(dash, 1, row, str.tostring(pm_h, format.mintick), text_color=#FF00FF, text_size=tbl_size, bgcolor=pmh_bg, text_halign=text.align_center)
row += 1
table.cell(dash, 0, row, "PML", text_color=#FF00FF, text_size=tbl_size, bgcolor=pml_bg, text_halign=text.align_center)
table.cell(dash, 1, row, str.tostring(pm_l, format.mintick), text_color=#FF00FF, text_size=tbl_size, bgcolor=pml_bg, text_halign=text.align_center)
row += 1
pqh_bg = near_pqh ? color.new(color.white, 70) : color.new(#0d1117, 0)
pql_bg = near_pql ? color.new(color.white, 70) : color.new(#0d1117, 0)
table.cell(dash, 0, row, "PQH", text_color=color.white, text_size=tbl_size, bgcolor=pqh_bg, text_halign=text.align_center)
table.cell(dash, 1, row, str.tostring(pq_h, format.mintick), text_color=color.white, text_size=tbl_size, bgcolor=pqh_bg, text_halign=text.align_center)
row += 1
table.cell(dash, 0, row, "PQL", text_color=color.white, text_size=tbl_size, bgcolor=pql_bg, text_halign=text.align_center)
table.cell(dash, 1, row, str.tostring(pq_l, format.mintick), text_color=color.white, text_size=tbl_size, bgcolor=pql_bg, text_halign=text.align_center)
row += 1
table.cell(dash, 0, row, "Prev " + prev_same_day_name, text_color=#00E5FF, text_size=tbl_size, bgcolor=color.new(#16213e, 0), text_halign=text.align_center)
table.cell(dash, 1, row, "", bgcolor=color.new(#16213e, 0))
table.merge_cells(dash, 0, row, 1, row)
row += 1
psd_h_str = not na(prev_same_day_high) ? str.tostring(prev_same_day_high, format.mintick) : "N/A"
psd_l_str = not na(prev_same_day_low) ? str.tostring(prev_same_day_low, format.mintick) : "N/A"
table.cell(dash, 0, row, "Prev " + prev_same_day_name + " H", text_color=prev_day_h_color, text_size=tbl_size, bgcolor=color.new(#0d1117, 0), text_halign=text.align_center)
table.cell(dash, 1, row, psd_h_str, text_color=prev_day_h_color, text_size=tbl_size, bgcolor=color.new(#0d1117, 0), text_halign=text.align_center)
row += 1
table.cell(dash, 0, row, "Prev " + prev_same_day_name + " L", text_color=prev_day_l_color, text_size=tbl_size, bgcolor=color.new(#0d1117, 0), text_halign=text.align_center)
table.cell(dash, 1, row, psd_l_str, text_color=prev_day_l_color, text_size=tbl_size, bgcolor=color.new(#0d1117, 0), text_halign=text.align_center)
row += 1
table.cell(dash, 0, row, "Round Numbers (" + str.tostring(round_step) + ")", text_color=color.white, text_size=tbl_size, bgcolor=color.new(#16213e, 0), text_halign=text.align_center)
table.cell(dash, 1, row, "", bgcolor=color.new(#16213e, 0))
table.merge_cells(dash, 0, row, 1, row)
row += 1
for i = round_count to 1
r_val = base_round + (round_step * i)
dist = r_val - close
dist_str = str.tostring(r_val, format.mintick) + " (+" + str.tostring(dist, "#.##") + ")"
table.cell(dash, 0, row, "▲ " + str.tostring(i), text_color=color.gray, text_size=tbl_size, bgcolor=color.new(#0d1117, 0), text_halign=text.align_center)
table.cell(dash, 1, row, dist_str, text_color=color.gray, text_size=tbl_size, bgcolor=color.new(#0d1117, 0), text_halign=text.align_center)
row += 1
for i = 0 to round_count_below - 1
r_val = base_round - (round_step * i)
dist = close - r_val
dist_str = str.tostring(r_val, format.mintick) + " (-" + str.tostring(dist, "#.##") + ")"
table.cell(dash, 0, row, "▼ " + str.tostring(i + 1), text_color=color.gray, text_size=tbl_size, bgcolor=color.new(#0d1117, 0), text_halign=text.align_center)
table.cell(dash, 1, row, dist_str, text_color=color.gray, text_size=tbl_size, bgcolor=color.new(#0d1117, 0), text_halign=text.align_center)
row += 1
// ─── ALERTS ───────────────────────────────────────────────
alertcondition(ta.cross(close, pd_h), "Price crossed Prev Day High", "Price crossed Previous Day High")
alertcondition(ta.cross(close, pd_l), "Price crossed Prev Day Low", "Price crossed Previous Day Low")
alertcondition(ta.cross(close, pw_h), "Price crossed Prev Week High", "Price crossed Previous Week High")
alertcondition(ta.cross(close, pw_l), "Price crossed Prev Week Low", "Price crossed Previous Week Low")
alertcondition(ta.cross(close, pm_h), "Price crossed Prev Month High", "Price crossed Previous Month High")
alertcondition(ta.cross(close, pm_l), "Price crossed Prev Month Low", "Price crossed Previous Month Low")
alertcondition(ta.cross(close, pq_h), "Price crossed Prev Quarter High", "Price crossed Previous Quarter High")
alertcondition(ta.cross(close, pq_l), "Price crossed Prev Quarter Low", "Price crossed Previous Quarter Low")
alertcondition(ta.cross(close, prev_same_day_high), "Price crossed Prev Same-Day High", "Price crossed Previous Same-Day High")
alertcondition(ta.cross(close, prev_same_day_low), "Price crossed Prev Same-Day Low", "Price crossed Previous Same-Day Low")
Show all 382 lines