How To Set Backtest Time Ranges
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © allanster

//@version=5
strategy(title = "How To Set Time Ranges", shorttitle = "Timed", overlay = true, precision = 8, max_bars_back = 200, pyramiding = 0, initial_capital = 100000, 
     currency = currency.NONE, default_qty_type = strategy.cash, default_qty_value = 100000, commission_type = "percent", commission_value = 0.27)

// Revision: 2
// Author:   @allanster
// Credit:   Special thanks to @LucF and @a.tesla2018 for help with including ':1234567' for time ranges on weekends        

// === INPUT MA LENGTHS ===
fastMA     = input.int(defval = 14,      title = "FastMA Length",   minval = 1, step = 1)
slowMA     = input.int(defval = 28,      title = "SlowMA Length",   minval = 1, step = 1)

// === INPUT DATE RANGE ===
fromMonth  = input.int(defval = 1,       title = "From Month",      minval = 1, maxval = 12)
fromDay    = input.int(defval = 1,       title = "From Day",        minval = 1, maxval = 31)
fromYear   = input.int(defval = 2021,    title = "From Year",       minval = 1970)
thruMonth  = input.int(defval = 1,       title = "Thru Month",      minval = 1, maxval = 12)
thruDay    = input.int(defval = 1,       title = "Thru Day",        minval = 1, maxval = 31)
thruYear   = input.int(defval = 2112,    title = "Thru Year",       minval = 1970)

// === INPUT TIME RANGE ===
entryTime  = input.session('0000-0000',  title = "Entry Time")   // '0000-0000' is anytime to enter
exitTime   = input.session('0000-0000',  title = "Exit Time")    // '0000-0000' is anytime to exit

// === INPUT SHOW PLOTS ===
showDate   = input(true,                 title = "Show Date Range")
showTimeE  = input(true,                 title = "Show Time Entry")
showTimeX  = input(true,                 title = "Show Time Exit")

// === DATE & TIME RANGE FUNCTIONS ===
isDate() =>                                                               // create function "within window of dates"
    start  = timestamp(fromYear, fromMonth, fromDay, 00, 00)              // date start
    finish = timestamp(thruYear, thruMonth, thruDay, 23, 59)              // date finish
    isDate = time >= start and time <= finish                             // current date is "within window of dates"

isTime(_position) =>                                                      // create function "within window of time"
    isTime = time(timeframe.period, _position + ':1234567')               // current time is "within window of time"

// === LOGIC ===
enterLong  = ta.crossover (ta.sma(close, fastMA), ta.sma(close, slowMA))  // enter when fastMA crosses over  slowMA
exitLong   = ta.crossunder(ta.sma(close, fastMA), ta.sma(close, slowMA))  // exits when fastMA crosses under slowMA

// === EXECUTION ===
strategy.entry("L", strategy.long, when = isDate() and isTime(entryTime) and enterLong)  // enter "within window of dates and time" AND crossover
strategy.close("L", when = isDate() and isTime(exitTime) and exitLong)                   // exits "within window of dates and time" AND crossunder         

// === PLOTTING ===
bgcolor(color = showDate  and isDate()                       ? color.new(color.gray, 90)   : color(na))  // plot "within window of dates"
bgcolor(color = showTimeE and isDate() and isTime(entryTime) ? color.new(color.lime, 90)   : color(na))  // plot "within window of entry time"
bgcolor(color = showTimeX and isDate() and isTime(exitTime)  ? color.new(color.purple, 90) : color(na))  // plot "within window of exit time"

plot(ta.sma(close, fastMA), title = 'FastMA', color = color.new(color.yellow, 0), linewidth = 2, style = plot.style_line)  // plot FastMA
plot(ta.sma(close, slowMA), title = 'SlowMA', color = color.new(color.aqua, 0),   linewidth = 2, style = plot.style_line)  // plot SlowMA