//@version=5 strategy(title="Custom Session Strategy", overlay=true, default_qty_value=100, initial_capital=100000, max_lines_count=500, default_qty_type=strategy.percent_of_equity, pyramiding=0, process_orders_on_close=true) tz = input.string("UTC+2", title='TimeZone') // Trading Session in_session(sess) => t = time(timeframe.period, sess, tz) r = na(t) ? false : true r session = input.session("1415-1500", title="Trading Session") + ":1234567" sessionx = input.session("1415-1550", title="Exit Trades after this") + ":1234567" inSess = in_session(session) inSessx = in_session(sessionx) bgcolor(inSess ? color.new(color.orange, 90) : na) start = inSess and not inSess[1] h = ta.valuewhen(start, high[0], 0) l = ta.valuewhen(start, low[0], 0) r = h - l e = input(2.0, title='Entry Points') sl = input(2.0, title='SL Points') plot(inSess ? h + e : na, color=color.orange, style=plot.style_linebr) plot(inSess ? l - e : na, color=color.orange, style=plot.style_linebr) sl_long = (h + e) - (r + sl) sl_short = (l - e) + (r + sl) tp = input.float(2.0, title="Take Profit R:R", minval=0, step=0.1) tp_long = (h + e) + math.abs((h + e) - sl_long) * tp tp_short = (l - e) - math.abs((l - e) - sl_short) * tp // Calculate the 1% profit threshold initial_risk = math.abs(h - (h + e)) // Initial risk for a long trade profit_threshold = initial_risk * 1.01 // 1% profit threshold // NEWDAY is_newbar(res) => t = time(res) not na(t) and (na(t[1]) or t > t[1]) newDay = is_newbar("D") newTrade = ta.change(strategy.closedtrades) > 0 newWin = ta.change(strategy.wintrades) > 0 trades_per_day = 0 trades_per_day := newDay ? 0 : newTrade ? trades_per_day[1] + 1 : trades_per_day[1] wins_per_day = 0 wins_per_day := newDay ? 0 : newWin ? wins_per_day[1] + 1 : wins_per_day[1] // Strategy Backtest Limiting Algorithm i_startTime = input.time(defval=timestamp("01 Sep 2002 13:30 +0000"), title="Backtesting Start Time") i_endTime = input.time(defval=timestamp("30 Sep 2099 19:30 +0000"), title="Backtesting End Time") timeCond = (time > i_startTime) and (time < i_endTime) equity = strategy.initial_capital + strategy.netprofit if inSess and equity > 0 and timeCond and strategy.opentrades == 0 and trades_per_day < 4 strategy.entry("long", strategy.long, comment='Long', stop=h + e) strategy.exit("SL/TPL", from_entry="long", stop=sl_long, limit=tp_long, comment_profit='TP', comment_loss='SL') if (close >= (h + e) + profit_threshold) strategy.exit("Move SL to BE", from_entry="long", stop=h + e) if strategy.position_size > 0 and strategy.position_size[1] <= 0 line.new(bar_index - 1, sl_long, bar_index + 2, sl_long, color=color.red) if inSess and equity > 0 and timeCond and strategy.opentrades == 0 and wins_per_day == 0 strategy.entry("short", strategy.short, comment='Short', stop=l - e) strategy.exit("SL/TPS", from_entry="short", stop=sl_short, limit=tp_short, comment_profit='TP', comment_loss='SL') if (close <= (l - e) - profit_threshold) strategy.exit("Move SL to BE", from_entry="short", stop=l - e) if not inSess or trades_per_day == 4 or wins_per_day > 0 or equity < 0 strategy.cancel("long") strategy.cancel("short") if not inSessx strategy.close_all(comment='Exit')