How To Set Backtest Date Range
Oct 26, 2017

The How To Set Backtest Date Range tool provides a standardized framework for limiting strategy executions to a specific time window, enabling consistent performance comparisons across different timeframes.
Usage
The Usage section describes how the script can be used to filter backtesting results. By defining a specific start and end point, traders can ensure that strategy metrics are calculated over the exact same historical period regardless of the chart resolution (e.g., 5-minute vs. 15-minute charts).
To use this logic in a custom strategy:
- Define the "From" and "Thru" dates in the settings menu.
- The script uses a boolean function to check if the current bar's timestamp falls within the selected window.
- Execution commands (Long/Short) are only triggered if the date condition is met.
- Users can toggle the "Show Date Range" setting to visually highlight the active backtesting area on the chart with a background color.
Details
The script utilizes the input.time() function combined with the timestamp() built-in to create a user-definable range. The core logic relies on a custom function dateWithinRange(), which evaluates whether the time variable is greater than or equal to the start date and less than or equal to the end date.
A critical technical consideration in backtesting is the order limit (often 2000 orders). If a date range is too broad on lower timeframes, the strategy may hit this limit, potentially skewing the results and preventing an "apples-to-apples" comparison. Reducing the date range ensures that the strategy remains within platform limits while providing accurate comparative data.
Settings
Strategy Inputs
- Faster/Length: Determines the source and period for the fast Moving Average used in the crossover logic.
- Slower/Length: Determines the source and period for the slow Moving Average used in the crossover logic.
Date Range Inputs
- From: Sets the start date and time for the backtest period.
- Thru: Sets the end date and time for the backtest period.
- Show Date Range: Toggles a background highlight (bgcolor) to indicate the active backtesting window on the chart.
FAQ
How do I adjust the dates for my backtest? Open the script settings, navigate to the "Inputs" tab, and modify the "From" and "Thru" date fields to your desired window.
Why are my backtest results different on different timeframes even with the same date range? While the date range remains constant, different timeframes provide different data granularity, which may lead to different entry and exit prices based on the indicator calculations.
How can I access the How To Set Backtest Date Range tool? You can get access on the LuxAlgo Library for charting platforms like TradingView, MetaTrader (MT4/MT5), and NinjaTrader for free.
Free indicator
Get free access to this indicator on the platforms below.
Unlock the entire LuxAlgo Library
Every indicator, every strategy, full charts, and complete access to Quant — our AI agent.