Skip to content
All library documents

Restricting Strategy Backtests by Date and Session

Article TradingView scripts

Summary

This example shows how to limit a Pine Script strategy's entries and exits to configurable calendar dates and intraday sessions. It builds start and end timestamps, checks whether each bar falls inside that date window, and uses a session-time function that includes all weekdays. Separate entry and exit session inputs allow the two actions to be constrained differently; chart shading makes the active date and time windows visible.

The demonstration strategy uses a simple moving-average crossover to enter long and a crossunder to close, but that signal logic serves only to show where the filters attach. The author presents the script as a research example, not as a trading system. It supplies no performance evidence, and users should account for chart timezone, instrument trading hours, and the chosen bar interval when interpreting session and date boundaries.

Key ideas

  • Date filters compare each bar's timestamp with configurable start and end dates.
  • Entry and exit sessions can be configured separately and applied independently.
  • The example includes all weekdays in its session-time check.
  • Chart background colors visualize the active date, entry, and exit windows.
  • The moving-average crossover is illustrative, and the script provides no strategy performance evidence.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.