The script gates entry signals by bar-open time in a half-open UTC interval: start included, end excluded. Its weekday filter also uses UTC. This is not an exact fill-time boundary: under default strategy settings, orders normally fill on a later tick, and an end-of-window close is requested only when the script next calculates on a bar outside the interval. Disabling Friday entries does not close an existing position for the weekend.
Pine Script v6: UTC entry-date and weekday filterpinescript
//@version=6
strategy("Date Range & Session Filter [v6]",
overlay = true,
initial_capital = 10000,
default_qty_type = strategy.percent_of_equity,
default_qty_value = 10)
// Explicit UTC dates; end is exclusive.
int startTimestamp = input.time(timestamp("01 Jan 2024 00:00 +0000"), "Start (inclusive)")
int endTimestamp = input.time(timestamp("01 Jan 2027 00:00 +0000"), "End (exclusive)")
if startTimestamp >= endTimestamp
runtime.error("Start must precede end")
// Day of week filters
bool tradeMonday = input.bool(true, "Trade Monday")
bool tradeFriday = input.bool(false, "Allow Friday entries (UTC)")
// Gate signals by bar-open time, not order-fill time.
bool inDateRange = time >= startTimestamp and time < endTimestamp
int utcDay = dayofweek(time, "UTC")
bool isMonday = utcDay == dayofweek.monday
bool isFriday = utcDay == dayofweek.friday
bool validDay = (not isMonday or tradeMonday) and (not isFriday or tradeFriday)
// Technical signals
float fastEma = ta.ema(close, 20)
float slowEma = ta.ema(close, 50)
bool buySignal = ta.crossover(fastEma, slowEma)
bool exitSignal = ta.crossunder(fastEma, slowEma)
// Execution inside allowed window
if inDateRange and validDay
if buySignal
strategy.entry("Long", strategy.long)
if exitSignal or (time >= endTimestamp and strategy.position_size > 0)
strategy.close("Long", comment = "Exit Trigger / Window End")
// Plot moving averages
plot(fastEma, "Fast EMA", color = color.blue)
plot(slowEma, "Slow EMA", color = color.orange)
// Background highlight for active backtest date window
bgcolor(inDateRange ? color.new(color.blue, 95) : na)