Another feature is added from previous script which is the Day Filter. You can now choose which days to open trade. This is to improve your probability of winning trade.
Pine scriptcode as below:
//This Pine script strategy was inspired and modified from MSB-OB by EmreKb. Credit goes to EmreKb.
//@version=5
strategy("MSB strategy[kriptos.my]", overlay=true, max_lines_count=500, max_bars_back=4900, max_boxes_count=500, initial_capital=100, default_qty_type=strategy.percent_of_equity, default_qty_value=99, pyramiding=1, commission_type=strategy.commission.percent, commission_value=0.22)
//In general, most recommended TF is 15min. Alternatives are 30min & 1hour
//code266
// --- STRATEGY SETTINGS ---
strat_group = "Strategy Settings"
zigzag_len = input.int(18, "ZigZag Length [Recommended 3-30]", group=strat_group)
fib_factor = input.float(0.87, "Fib Factor [Recommended 0.3-0.99]", 0, 1, 0.01, group=strat_group)
tp_perc = input.float(9.9, title="Take Profit % [Recommended 4-14%]", step=0.1, group=strat_group)
rr_ratio = input.float(2.6, title="Reward/Risk Ratio [Recommended 1.0-3.0]", step=0.1, group=strat_group)
sl_perc = tp_perc / rr_ratio // Auto-calculates SL based on RR
// --- NEW TRAILING TAKE PROFIT SETTINGS ---
use_trail = input.bool(true, title="Enable Trailing Take Profit", group=strat_group)
trail_offset_perc = input.float(0.2, title="Trail Offset %", step=0.1, group=strat_group)
time_group = "Time Filter [DO NOT FORGET to SET A DAY AFTER]"
start_time = input.time(timestamp("2025-10-01 00:00"), title="Start Time", group=time_group)
end_time = input.time(timestamp("2027-12-31 23:59"), title="End Time", group=time_group)
in_time_frame = time >= start_time and time <= end_time
// --- DAY FILTER SETTINGS ---
day_group = "Day Filter Settings"
trade_mon = input.bool(true, title="Monday", group=day_group)
trade_tue = input.bool(false, title="Tuesday", group=day_group)
trade_wed = input.bool(true, title="Wednesday", group=day_group)
trade_thu = input.bool(false, title="Thursday", group=day_group)
trade_fri = input.bool(true, title="Friday", group=day_group)
trade_sat = input.bool(false, title="Saturday", group=day_group)
trade_sun = input.bool(true, title="Sunday", group=day_group)
is_trading_day = (dayofweek == dayofweek.monday and trade_mon) or
(dayofweek == dayofweek.tuesday and trade_tue) or
(dayofweek == dayofweek.wednesday and trade_wed) or
(dayofweek == dayofweek.thursday and trade_thu) or
(dayofweek == dayofweek.friday and trade_fri) or
(dayofweek == dayofweek.saturday and trade_sat) or
(dayofweek == dayofweek.sunday and trade_sun)
// --- ORIGINAL INDICATOR SETTINGS ---
settings = "Settings"
show_zigzag = input.bool(true, "Show Zigzag", group=settings)
text_size = input.string(size.tiny, "Text Size", [size.tiny, size.small, size.normal, size.large, size.huge], group=settings)
delete_boxes = input.bool(false, "Delete Old/Broken Boxes", group=settings)
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)
var box[] bu_mb_boxes = array.new_box(5)
var box[] be_mb_boxes = array.new_box(5)
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)
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_delete_box(box_arr) =>
if delete_boxes
box.delete(array.shift(box_arr))
else
array.shift(box_arr)
0
[h0, h0i] = f_get_high(0)
[h1, h1i] = f_get_high(1)
[l0, l0i] = f_get_low(0)
[l1, l1i] = f_get_low(1)
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)
market = 1
market := nz(market[1], 1)
market := market == 1 and close < l0 and low < l0 - math.abs(h0 - l0) * fib_factor ? -1 : market == -1 and close > h0 and high > h0 + math.abs(h0 - l0) * fib_factor ? 1 : market
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
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
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, high[bu_ob_since], bar_index + 10, low[bu_ob_since], 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)
array.push(bu_ob_boxes, bu_ob)
is_bu_bb = l0 < l1
bu_bb = box.new(bu_bb_index, high[bu_bb_since], bar_index + 10, low[bu_bb_since], bgcolor=bu_bb_color, border_color=bu_bb_border_color, text=is_bu_bb ? "Bu-BB" : "Bu-MB", text_color=bu_bb_text_color, text_halign=text.align_right, text_size=text_size)
if is_bu_bb
array.push(bu_bb_boxes, bu_bb)
else
array.push(bu_mb_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, high[be_ob_since], bar_index + 10, low[be_ob_since], 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)
array.push(be_ob_boxes, be_ob)
is_be_bb = h0 > h1
be_bb = box.new(be_bb_index, high[be_bb_since], bar_index + 10, low[be_bb_since], bgcolor=be_bb_color, border_color=be_bb_border_color, text=is_be_bb ? "Be-BB" : "Be-MB", text_color=be_bb_text_color, text_halign=text.align_right, text_size=text_size)
if is_be_bb
array.push(be_bb_boxes, be_bb)
else
array.push(be_mb_boxes, be_bb)
// --- MODIFIED ALERTS, BOX EXTENSION LOOPS & STRATEGY TRIGGER ---
long_signal = false
for bull_ob in bu_ob_boxes
if not na(bull_ob)
bottom = box.get_bottom(bull_ob)
top = box.get_top(bull_ob)
if close < bottom
f_delete_box(bu_ob_boxes)
else if close < top
continue
else
box.set_right(bull_ob, bar_index + 10)
if close[1] <= top and barstate.isconfirmed
alert("Price closed above BU-OB green box")
long_signal := true
for bull_bb in bu_bb_boxes
if not na(bull_bb)
bottom = box.get_bottom(bull_bb)
top = box.get_top(bull_bb)
if close < bottom
f_delete_box(bu_bb_boxes)
else if close < top
continue
else
box.set_right(bull_bb, bar_index + 10)
if close[1] <= top and barstate.isconfirmed
alert("Price closed above Bu-BB green box")
long_signal := true
for bull_mb in bu_mb_boxes
if not na(bull_mb)
bottom = box.get_bottom(bull_mb)
top = box.get_top(bull_mb)
if close < bottom
f_delete_box(bu_mb_boxes)
else if close < top
continue
else
box.set_right(bull_mb, bar_index + 10)
for bear_ob in be_ob_boxes
if not na(bear_ob)
top = box.get_top(bear_ob)
bottom = box.get_bottom(bear_ob)
if close > top
f_delete_box(be_ob_boxes)
else if close > bottom
continue
else
box.set_right(bear_ob, bar_index + 10)
if close[1] >= bottom and barstate.isconfirmed
alert("Price closed below BE-OB red box")
for bear_bb in be_bb_boxes
if not na(bear_bb)
top = box.get_top(bear_bb)
bottom = box.get_bottom(bear_bb)
if close > top
f_delete_box(be_bb_boxes)
else if close > bottom
continue
else
box.set_right(bear_bb, bar_index + 10)
if close[1] >= bottom and barstate.isconfirmed
alert("Price closed below Be-BB red box")
for bear_mb in be_mb_boxes
if not na(bear_mb)
top = box.get_top(bear_mb)
bottom = box.get_bottom(bear_mb)
if close > top
f_delete_box(be_mb_boxes)
else if close > bottom
continue
else
box.set_right(bear_mb, bar_index + 10)
alertcondition(ta.change(market) != 0, "MSB", "MSB")
// --- STRATEGY EXECUTION ---
if long_signal and in_time_frame and is_trading_day
strategy.entry("Long", strategy.long)
if strategy.position_size > 0
limit_price = strategy.position_avg_price * (1 + (tp_perc / 100))
stop_price = strategy.position_avg_price * (1 - (sl_perc / 100))
if use_trail
trail_offset_ticks = (limit_price * (trail_offset_perc / 100)) / syminfo.mintick
strategy.exit("Exit Long", "Long", stop=stop_price, trail_price=limit_price, trail_offset=trail_offset_ticks)
else
strategy.exit("Exit Long", "Long", limit=limit_price, stop=stop_price)
Setting suggestions
Pair: XMRUSDT
Exchanger: Kucoin
TF: 15min
Zigzag length 18
Fib Factor: 0.87
RR Ratio 2.6
Enable Trailing TP: checked
Trail Offset 0.2%
Days to trade to open trade: Mon, Wed, Fri, Sun
**code266

No Comments