How To Auto Set Date Range
// 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 Auto Set Date Range", shorttitle = "AutoDates", 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: 3
// Author:   @allanster

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

// === INPUT BACKTEST RANGE ===
useRange     = input.string(defval = "WEEKS",                              title = "Date Range",        confirm = false, options = ["ALL", "DAYS", "WEEKS", "MANUAL"])
nDaysOrWeeks = input.int   (defval = 52,                                   title = "# Days or Weeks",   minval = 1)
fromTime     = input.time  (defval = timestamp("01 Jan 2021 00:00 +0000"), title = "From Date & Time")
thruTime     = input.time  (defval = timestamp("01 Jan 2112 00:00 +0000"), title = "Thru Date & Time")

// === INPUT SHOW PLOT ===
showDate     = input       (defval = true,                                 title = "Show Date Range")

// === FUNCTION EXAMPLE ===
window() =>                                                               // create function "within window of time"
    oneDay   = 24 * 60 * 60 * 1000
    oneWeek  = 7 * oneDay
    present  = timenow
    start    = useRange == "DAYS" ? present - nDaysOrWeeks * oneDay : 
      useRange == "WEEKS" ? present - nDaysOrWeeks * oneWeek : fromTime
    finish   = useRange == "DAYS" or useRange == "WEEKS" ? timestamp(2112, 1, 1, 23, 59) : thruTime
    useRange != "ALL" ? time >= start and time <= finish : true

// === LOGIC ===
crossOv      = ta.crossover (ta.ema(hlc3, fastMA), ta.ema(hlc3, slowMA))  // true when fastMA crosses over  slowMA
crossUn      = ta.crossunder(ta.ema(hlc3, fastMA), ta.ema(hlc3, slowMA))  // true when fastMA crosses under slowMA

// === EXECUTION ===
strategy.entry("L", strategy.long, when=window() and crossOv)             // enter long when "within window of time" AND crossover
strategy.close("L", when=window() and crossUn)                            // exit  long when "within window of time" AND crossunder         

// === PLOTTING ===
bgcolor(color = showDate and window() ? color.rgb(120,123,134, 90) : na)                                             // plot "within window of dates"
plot(ta.ema(hlc3, fastMA), title = 'FastMA', color = color.rgb(0,188,212),  linewidth = 2, style = plot.style_line)  // plot FastMA
plot(ta.ema(hlc3, slowMA), title = 'SlowMA', color = color.rgb(255,235,59), linewidth = 2, style = plot.style_line)  // plot SlowMA