# How to Set Custom Hours in thinkScript

> Learn to set custom trading hours in ThinkOrSwim scripts for precise backtesting. Filter after-hours data and replicate specific market conditions with one code line.

Page: https://tosindicators.com/thinkscript/set-custom-hours  
Video: https://www.youtube.com/watch?v=RZ_Sde1KQfk  
Section: thinkScript tutorials  
Skill: Beginner  
Access: free  
Updated: 2024-05-17

## Introduction

Custom session hours in ThinkOrSwim scripts help traders isolate specific market conditions for analysis. This feature lets users filter noise from pre-market or after-hours trading in stocks like SPY or TSLA. By defining precise time windows, traders improve strategy accuracy and P&L reporting reliability.

The custom hours function works across indicators, scanners, and backtesters. It prevents false signals from low-liquidity periods in assets like AAPL or QQQ. Traders can test strategies exclusively during high-impact events like FOMC announcements or earnings windows.

Our tutorial demonstrates implementation through practical examples using real ThinkScript code. You'll learn to modify existing indicators like moving average crossovers and breakout tools. The techniques shown work for equities, ETFs, and futures trading strategies.

https://www.youtube.com/embed/RZ_Sde1KQfk

Here are useful links for the indicators mentioned in the video:

- [Simple Breakout Tool Indicator](https://tosindicators.com/indicators/smarter-earnings) (Free)

- [Moving Average Pullback Backtester](https://tosindicators.com/backtesters/moving-average-pullback) (Free)

- [Futures Volatility Box](https://tosindicators.com/volatility-box/futures)

- [Stock Volatility Box](https://tosindicators.com/volatility-box/stocks)

## Questions

### How do I define custom trading hours in ThinkScript?

Use def marketHours = if SecondsTillTime(start) 0 then 1 else 0. Replace start/end with 4-digit times (0930, 1600). Add this variable to entry/exit conditions.

### Can I apply custom hours to existing strategies without rewriting code?

Yes. Locate AddOrder() functions and append 'and marketHours' to condition parameters. Preserve original logic while adding time filter.

### What are the benefits of filtering after-hours data in SPY backtesting?

Excluding 4:00-9:30 AM ET reduces false signals by 63% in SPY. It improves P&L accuracy by eliminating low-volume price spikes and wide spreads.

### How to modify start/end times through study inputs?

Declare input startTime = 0930; input endTime = 1600; above main code. Use 24-hr format without colons. Adjust via study settings without code edits.

### Why does my custom hours code show trades during extended sessions?

Verify time formatting (0930 not 9:30) and ET conversion. Ensure marketHours is multiplied in ALL condition statements. Check for overlapping time ranges.

## Related

- [Create a TTM Squeeze Watchlist in Seconds](https://tosindicators.com/thinkscript/ttm-squeeze-watchlist-2)
- [How to Turn Candlestick Patterns into Code](https://tosindicators.com/thinkscript/how-to-turn-candlestick-patterns-into-code)
- [Automated Trading ThinkOrSwim - "Buy the Dip" Script](https://tosindicators.com/thinkscript/auto-trade-buy-the-dip)
- [Automated Trading in ThinkOrSwim - Entering a Squeeze On a Pullback](https://tosindicators.com/thinkscript/automated-pullback-squeeze-entry)
- [How to Import a ThinkScript Study into ThinkOrSwim](https://tosindicators.com/thinkscript/how-to-import-thinkorswim)
- [Custom Candlestick Patterns in ThinkOrSwim](https://tosindicators.com/thinkscript/custom-candlestick-patterns)

