//@version=5 indicator("Market Structure Break & Order Block", "MSB-OB", overlay=true, max_lines_count=500, max_bars_back=4900, max_boxes_count=500) settings = "Settings" zigzag_len = input.int(9, "ZigZag Length", group=settings) show_zigzag = input.bool(true, "Show Zigzag", group=settings) fib_factor = input.float(0.33, "Fib Factor for breakout confirmation", 0, 1, 0.01, group=settings) tooltip_text = "Some timeframes may not be displayed in current timeframe. Zigzag lines only shows in current timeframe." time_frame= input.string("Chart", "Timeframe", ["Chart", "5m", "15m", "30m", "1h", "2h", "4h", "D"], tooltip=tooltip_text) tf = switch time_frame "5m" => "5" "15m" => "15" "30m" => "30" "1h" => "60" "2h" => "120" "4h" => "240" "D" => "D" => timeframe.period text_size = input.string(size.tiny, "Text Size", [size.tiny, size.small, size.normal, size.large, size.huge]) bu_ob_inline_color = "Bu-OB Colors" be_ob_inline_color = "Be-OB Colors" bu_bb_inline_color = "Bu-BB Colors" be_bb_inline_color = "Be-BB Colors" bu_ob_display_settings = "Bu-OB Display Settings" bu_ob_color = input.color(color.new(color.green, 70), "Color", group=bu_ob_display_settings, inline=bu_ob_inline_color) bu_ob_border_color = input.color(color.green, "Border Color", group=bu_ob_display_settings, inline=bu_ob_inline_color) bu_ob_text_color = input.color(color.green, "Text Color", group=bu_ob_display_settings, inline=bu_ob_inline_color) be_ob_display_settings = "Be-OB Display Settings" be_ob_color = input.color(color.new(color.red, 70), "Color", group=be_ob_display_settings, inline=be_ob_inline_color) be_ob_border_color = input.color(color.red, "Border Color", group=be_ob_display_settings, inline=be_ob_inline_color) be_ob_text_color = input.color(color.red, "Text Color", group=be_ob_display_settings, inline=be_ob_inline_color) bu_bb_display_settings = "Bu-BB & Bu-MB Display Settings" bu_bb_color = input.color(color.new(color.green, 70), "Color", group=bu_bb_display_settings, inline=bu_bb_inline_color) bu_bb_border_color = input.color(color.green, "Border Color", group=bu_bb_display_settings, inline=bu_bb_inline_color) bu_bb_text_color = input.color(color.green, "Text Color", group=bu_bb_display_settings, inline=bu_bb_inline_color) be_bb_display_settings = "Be-BB & Be-MB Display Settings" be_bb_color = input.color(color.new(color.red, 70), "Color", group=be_bb_display_settings, inline=be_bb_inline_color) be_bb_border_color = input.color(color.red, "Border Color", group=be_bb_display_settings, inline=be_bb_inline_color) be_bb_text_color = input.color(color.red, "Text Color", group=be_bb_display_settings, inline=be_bb_inline_color) var float[] high_points_arr = array.new_float(5) var int[] high_index_arr = array.new_int(5) var float[] low_points_arr = array.new_float(5) var int[] low_index_arr = array.new_int(5) var box[] bu_ob_boxes = array.new_box(5) var box[] be_ob_boxes = array.new_box(5) var box[] bu_bb_boxes = array.new_box(5) var box[] be_bb_boxes = array.new_box(5) f_get_high(ind) => [array.get(high_points_arr, array.size(high_points_arr) - 1 - ind), array.get(high_index_arr, array.size(high_index_arr) - 1 - ind)] f_get_low(ind) => [array.get(low_points_arr, array.size(low_points_arr) - 1 - ind), array.get(low_index_arr, array.size(low_index_arr) - 1 - ind)] f_main1() => to_up = high >= ta.highest(zigzag_len) to_down = low <= ta.lowest(zigzag_len) trend = 1 trend := nz(trend[1], 1) trend := trend == 1 and to_down ? -1 : trend == -1 and to_up ? 1 : trend last_trend_up_since = ta.barssince(to_up[1]) low_val = ta.lowest(nz(last_trend_up_since > 0 ? last_trend_up_since : 1, 1)) low_index = bar_index - ta.barssince(low_val == low) last_trend_down_since = ta.barssince(to_down[1]) high_val = ta.highest(nz(last_trend_down_since > 0 ? last_trend_down_since : 1, 1)) high_index = bar_index - ta.barssince(high_val == high) if ta.change(trend) != 0 if trend == 1 array.push(low_points_arr, low_val) array.push(low_index_arr, low_index) if trend == -1 array.push(high_points_arr, high_val) array.push(high_index_arr, high_index) [h0, h0i] = f_get_high(0) [h1, h1i] = f_get_high(1) [l0, l0i] = f_get_low(0) [l1, l1i] = f_get_low(1) market = 1 market := nz(market[1], 1) last_l0 = ta.valuewhen(ta.change(market) != 0, l0, 0) last_h0 = ta.valuewhen(ta.change(market) != 0, h0, 0) market := last_l0 == l0 or last_h0 == h0 ? market : market == 1 and l0 < l1 and l0 < l1 - math.abs(h0 - l1) * fib_factor ? -1 : market == -1 and h0 > h1 and h0 > h1 + math.abs(h1 - l0) * fib_factor ? 1 : market // For alert alert_market = 1 alert_market := nz(alert_market[1], 1) alert_market := last_l0 == l0 or last_h0 == h0 ? alert_market : alert_market == 1 and trend == -1 and close < l0 and close < l0 - math.abs(h0 - l0) * fib_factor ? -1 : alert_market == -1 and trend == 1 and close > h0 and close > h0 + math.abs(h0 - l0) * fib_factor ? 1 : alert_market bu_ob_index = bar_index bu_ob_index := nz(bu_ob_index[1], bar_index) for i=h1i to l0i[zigzag_len] index = bar_index - i if open[index] > close[index] bu_ob_index := bar_index[index] bu_ob_since = bar_index - bu_ob_index be_ob_index = bar_index be_ob_index := nz(be_ob_index[1], bar_index) for i=l1i to h0i[zigzag_len] index = bar_index - i if open[index] < close[index] be_ob_index := bar_index[index] be_ob_since = bar_index - be_ob_index be_bb_index = bar_index be_bb_index := nz(be_bb_index[1], bar_index) for i=h1i - zigzag_len to l1i index = bar_index - i if open[index] > close[index] be_bb_index := bar_index[index] be_bb_since = bar_index - be_bb_index bu_bb_index = bar_index bu_bb_index := nz(bu_bb_index[1], bar_index) for i=l1i - zigzag_len to h1i index = bar_index - i if open[index] < close[index] bu_bb_index := bar_index[index] bu_bb_since = bar_index - bu_bb_index bu_ob_since_high = high[bu_ob_since] bu_ob_since_low = low[bu_ob_since] be_ob_since_high = high[be_ob_since] be_ob_since_low = low[be_ob_since] be_bb_since_high = high[be_bb_since] be_bb_since_low = low[be_bb_since] bu_bb_since_high = high[bu_bb_since] bu_bb_since_low = low[bu_bb_since] [trend, h0i, h0, l0i, l0, market, h1i, h1, l1i, l1, bu_ob_since_high, bu_ob_since_low, be_ob_since_high, be_ob_since_low] f_main2() => to_up = high >= ta.highest(zigzag_len) to_down = low <= ta.lowest(zigzag_len) trend = 1 trend := nz(trend[1], 1) trend := trend == 1 and to_down ? -1 : trend == -1 and to_up ? 1 : trend last_trend_up_since = ta.barssince(to_up[1]) low_val = ta.lowest(nz(last_trend_up_since > 0 ? last_trend_up_since : 1, 1)) low_index = bar_index - ta.barssince(low_val == low) last_trend_down_since = ta.barssince(to_down[1]) high_val = ta.highest(nz(last_trend_down_since > 0 ? last_trend_down_since : 1, 1)) high_index = bar_index - ta.barssince(high_val == high) if ta.change(trend) != 0 if trend == 1 array.push(low_points_arr, low_val) array.push(low_index_arr, low_index) if trend == -1 array.push(high_points_arr, high_val) array.push(high_index_arr, high_index) [h0, h0i] = f_get_high(0) [h1, h1i] = f_get_high(1) [l0, l0i] = f_get_low(0) [l1, l1i] = f_get_low(1) market = 1 market := nz(market[1], 1) last_l0 = ta.valuewhen(ta.change(market) != 0, l0, 0) last_h0 = ta.valuewhen(ta.change(market) != 0, h0, 0) market := last_l0 == l0 or last_h0 == h0 ? market : market == 1 and l0 < l1 and l0 < l1 - math.abs(h0 - l1) * fib_factor ? -1 : market == -1 and h0 > h1 and h0 > h1 + math.abs(h1 - l0) * fib_factor ? 1 : market // For alert alert_market = 1 alert_market := nz(alert_market[1], 1) alert_market := last_l0 == l0 or last_h0 == h0 ? alert_market : alert_market == 1 and trend == -1 and close < l0 and close < l0 - math.abs(h0 - l0) * fib_factor ? -1 : alert_market == -1 and trend == 1 and close > h0 and close > h0 + math.abs(h0 - l0) * fib_factor ? 1 : alert_market bu_ob_index = bar_index bu_ob_index := nz(bu_ob_index[1], bar_index) for i=h1i to l0i[zigzag_len] index = bar_index - i if open[index] > close[index] bu_ob_index := bar_index[index] bu_ob_since = bar_index - bu_ob_index be_ob_index = bar_index be_ob_index := nz(be_ob_index[1], bar_index) for i=l1i to h0i[zigzag_len] index = bar_index - i if open[index] < close[index] be_ob_index := bar_index[index] be_ob_since = bar_index - be_ob_index be_bb_index = bar_index be_bb_index := nz(be_bb_index[1], bar_index) for i=h1i - zigzag_len to l1i index = bar_index - i if open[index] > close[index] be_bb_index := bar_index[index] be_bb_since = bar_index - be_bb_index bu_bb_index = bar_index bu_bb_index := nz(bu_bb_index[1], bar_index) for i=l1i - zigzag_len to h1i index = bar_index - i if open[index] < close[index] bu_bb_index := bar_index[index] bu_bb_since = bar_index - bu_bb_index bu_ob_since_high = high[bu_ob_since] bu_ob_since_low = low[bu_ob_since] be_ob_since_high = high[be_ob_since] be_ob_since_low = low[be_ob_since] be_bb_since_high = high[be_bb_since] be_bb_since_low = low[be_bb_since] bu_bb_since_high = high[bu_bb_since] bu_bb_since_low = low[bu_bb_since] [alert_market, be_bb_since_high, be_bb_since_low, bu_bb_since_high, bu_bb_since_low, bu_ob_index, bu_bb_index, be_ob_index, be_bb_index] [trend, h0i, h0, l0i, l0, market, h1i, h1, l1i, l1, bu_ob_since_high, bu_ob_since_low, be_ob_since_high, be_ob_since_low] = request.security(syminfo.tickerid, tf, f_main1()) [alert_market, be_bb_since_high, be_bb_since_low, bu_bb_since_high, bu_bb_since_low, bu_ob_index, bu_bb_index, be_ob_index, be_bb_index] = request.security(syminfo.tickerid, tf, f_main2()) // Be_bb_since olanlar değişecek be_ob_index ler eklenecek if ta.change(trend) != 0 and show_zigzag if trend == 1 line.new(h0i, h0, l0i, l0) if trend == -1 line.new(l0i, l0, h0i, h0) if ta.change(market) != 0 if market == 1 line.new(h1i, h1, h0i, h1, color=color.green, width=2) label.new(int(math.avg(h1i, l0i)), h1, "MSB", color=color.new(color.black, 100), style=label.style_label_down, textcolor=color.green, size=size.small) bu_ob = box.new(bu_ob_index, bu_ob_since_high, bar_index + 10, bu_ob_since_low, bgcolor=bu_ob_color, border_color=bu_ob_border_color, text="Bu-OB", text_color=bu_ob_text_color, text_halign=text.align_right, text_size=text_size) bu_bb = box.new(bu_bb_index, bu_bb_since_high, bar_index + 10, bu_bb_since_low, bgcolor=bu_bb_color, border_color=bu_bb_border_color, text=l0 < l1 ? "Bu-BB" : "Bu-MB", text_color=bu_bb_text_color, text_halign=text.align_right, text_size=text_size) array.push(bu_ob_boxes, bu_ob) array.push(bu_bb_boxes, bu_bb) if market == -1 line.new(l1i, l1, l0i, l1, color=color.red, width=2) label.new(int(math.avg(l1i, h0i)), l1, "MSB", color=color.new(color.black, 100), style=label.style_label_up, textcolor=color.red, size=size.small) be_ob = box.new(be_ob_index, be_ob_since_high, bar_index + 10, be_ob_since_low, bgcolor=be_ob_color, border_color=be_ob_border_color, text="Be-OB", text_color=be_ob_text_color, text_halign=text.align_right, text_size=text_size) be_bb = box.new(be_bb_index, be_bb_since_high, bar_index + 10, be_bb_since_low, bgcolor=be_bb_color, border_color=be_bb_border_color, text=h0 > h1 ? "Be-BB" : "Be-MB", text_color=be_bb_text_color, text_halign=text.align_right, text_size=text_size) array.push(be_ob_boxes, be_ob) array.push(be_bb_boxes, be_bb) for bull_ob in bu_ob_boxes bottom = box.get_bottom(bull_ob) if close < bottom box.delete(bull_ob) else if array.size(bu_ob_boxes) == 5 box.delete(array.shift(bu_ob_boxes)) else box.set_right(bull_ob, bar_index + 10) for bear_ob in be_ob_boxes top = box.get_top(bear_ob) if close > top box.delete(bear_ob) else if array.size(be_ob_boxes) == 5 box.delete(array.shift(be_ob_boxes)) else box.set_right(bear_ob, bar_index + 10) for bear_bb in be_bb_boxes top = box.get_top(bear_bb) if close > top box.delete(bear_bb) else if array.size(be_bb_boxes) == 5 box.delete(array.shift(be_bb_boxes)) else box.set_right(bear_bb, bar_index + 10) for bull_bb in bu_bb_boxes bottom = box.get_bottom(bull_bb) if close < bottom box.delete(bull_bb) else if array.size(bu_bb_boxes) == 5 box.delete(array.shift(bu_bb_boxes)) else box.set_right(bull_bb, bar_index + 10) if ta.change(alert_market) != 0 alert("MSB", alert.freq_once_per_bar)