Just to clarify, that was the complete code for the subject of the question I asked, i.e., enter on the close and exit on the next day's open. Based on the response regarding sessions, I was able to produce a script that works on the 30-minute timeframe. This limits you to only a few years of backtesting.
Here is the script for anyone seeking an answer to this question. It includes a date filter and trend filter that you can optionally select. If anyone has a better way that will actually backtest 10 years or more, please share.
//@version=5
strategy("After-Hours Entry and Exit (Precise Timing)",
overlay=true,
initial_capital=10000,
default_qty_type=strategy.percent_of_equity,
default_qty_value=100)
//Input of Date Filters
i_dateFilter = input.bool(false, "Date Range Filtering On/Off")
i_fromYear = input.int(1900, "From Year", minval = 1900)
i_fromMonth = input.int(1, "From Month", minval = 1, maxval = 12)
i_fromDay = input.int(1, "From Day", minval = 1, maxval = 31)
i_toYear = input.int(2999, "To Year", minval = 1900)
i_toMonth = input.int(1, "To Month", minval = 1, maxval = 12)
i_toDay = input.int(1, "To Day", minval = 1, maxval = 31)
fromDate = timestamp(i_fromYear, i_fromMonth, i_fromDay, 00, 00)
toDate = timestamp(i_toYear, i_toMonth, i_toDay, 23, 59)
f_tradeDateIsAllowed() => not i_dateFilter or (time >= fromDate and time <= toDate)
//Long Trend Filter
trendFilter = input.bool(false, "Long Trend Filter", group= 'Long Trend Filter')
trendlength = input(title='Trend Lookback', defval=200, group='Long Trend Filter')
trend= ta.sma(close, trendlength)
f_trendFilterIsAllowed() => not trendFilter or (close >= trend)
// Define the timezone
inputTimezone = "GMT+0"
// Define the after-hours session (4:00 PM to 9:30 AM)
afterHoursStart = timestamp(inputTimezone, year, month, dayofmonth, 15, 30) // 4:00 PM
afterHoursEnd = timestamp(inputTimezone, year, month, dayofmonth + (hour >= 15 ? 1 : 0), 9, 00) // 9:30 AM next day
// Adjust the current bar's timestamp
currentBarTime = timestamp(inputTimezone, year, month, dayofmonth, hour, minute)
// Check if the current bar starts or ends within the after-hours session
entersAfterHours = (currentBarTime >= afterHoursStart and currentBarTime < afterHoursEnd) and strategy.position_size == 0
exitsAfterHours = (currentBarTime >= afterHoursEnd) and strategy.position_size > 0
// Entry logic
if entersAfterHours and f_tradeDateIsAllowed() and f_trendFilterIsAllowed()
strategy.entry("After-Hours Buy", strategy.long)
// Exit logic
if exitsAfterHours
strategy.close("After-Hours Buy")