Pine Script First X Minutes Filter
Blocks — or exclusively allows — entries during a configurable window right after the open, where spreads and slippage are usually at their worst.
Pine Script
//@version=6
strategy("Target Filled - First Minutes Filter Example", overlay=true, calc_on_every_tick=false)
// --- Inputs ---------------------------------------------------
windowMins = input.int(5, "Opening Window (minutes)", minval=1, maxval=120)
onlyWindow = input.bool(false, "Trade Only This Window",
tooltip="Off = skip the opening window. On = trade ONLY inside it.")
sessHour = input.int(9, "Session Open Hour (ET)", minval=0, maxval=23)
sessMin = input.int(30, "Session Open Minute", minval=0, maxval=59)
// --- Minutes elapsed since the session open (NY clock) --------------
minsNow = hour(time, "America/New_York") * 60 + minute(time, "America/New_York")
minsOpen = sessHour * 60 + sessMin
elapsed = minsNow - minsOpen
inWindow = elapsed >= 0 and elapsed < windowMins
afterWindow = elapsed >= windowMins
tradeOK = onlyWindow ? inWindow : afterWindow
// --- Sample signal — replace with your own entry logic ---------
fastMA = ta.ema(close, 9)
slowMA = ta.ema(close, 21)
canLong = ta.crossover(fastMA, slowMA) and tradeOK
canShort = ta.crossunder(fastMA, slowMA) and tradeOK
if canLong
strategy.entry("Long", strategy.long)
strategy.exit("Long Exit", "Long", profit=20, loss=10)
if canShort
strategy.entry("Short", strategy.short)
strategy.exit("Short Exit", "Short", profit=20, loss=10)
// Visual: yellow shading marks the opening window
bgcolor(inWindow ? color.new(color.yellow, 92) : na, title="Opening Window") Settings
| Input | Default | Purpose |
|---|---|---|
| windowMins | 5 | Length of the opening window, in minutes. |
| onlyWindow | off | Off skips the window; on trades exclusively inside it. |
| sessHour / sessMin | 9 / 30 | Session open time on the NY clock. |
How to use
- Paste into the Pine Editor and swap the EMA-crossover placeholder for your own entry conditions.
- In the default skip mode, no entry fires until
windowMinsminutes after the open — pair it with an end-of-window cutoff if the session also needs to be capped later in the day. - Yellow shading on the chart marks the opening window itself.
- In skip mode, entries stay live for the rest of the day once the window passes — combine with the time-of-day filter to add a hard cutoff too.
Frequently Asked Questions
How do I skip the first 5 minutes after the open in Pine Script?
Compute minutes elapsed since the session open on the NY clock and require that value to be at least 5 before any entry can fire. This snippet wires that comparison to a windowMins input, so the cutoff can be pushed to 15 or 30 minutes from the settings panel without touching the code.
Why avoid trading the open on a prop firm account?
The minutes right after 9:30 ET typically carry the widest spreads, the heaviest slippage, and the most erratic price action of the whole session. On an evaluation account, one bad fill during that opening burst can eat a meaningful chunk of the daily loss budget before a strategy's actual edge gets a chance to play out. Most systematic traders either skip the open entirely or run a dedicated opening-range approach rather than a generic one through it.