Skip to content
All library documents

Restricting Backtests by Calendar Dates and Trading Sessions

Article Strategy library · Author: LucF

Summary

This example shows how to constrain a moving-average crossover strategy by both calendar dates and intraday sessions. User inputs define a start and end date, plus separate permitted sessions for entries and exits. The date predicate compares each bar’s timestamp with the selected range, while a session-time check gates the crossover signals. Optional chart shading marks the active date range and entry and exit sessions, making the filters visible during review.

The underlying signal uses a fast and slow simple moving average: an upward cross enters long and a downward cross closes the position, subject to the relevant filters. The document supplies code and default inputs, but no backtest results or comparison of filtered and unfiltered performance. Session interpretation depends on the chart’s time settings, and restricting signal times may also leave positions open beyond a session; users should check how their strategy handles exits when the allowed exit window is absent. The example illustrates implementation mechanics rather than evidence that a particular date or session filter improves returns.

Key ideas

  • Date inputs can define inclusive start and end timestamps for gating strategy orders.
  • Entry and exit sessions can be configured separately to filter when each order type is allowed.
  • The example applies both date and session checks to simple moving-average crossover signals.
  • Background shading helps show which bars satisfy the configured date and session conditions.
  • The document provides no evidence that its filters improve strategy performance.

Tags

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