Pine Script EOD Flatten — Automatic Position Close at Session End
Calls strategy.close_all() at a configurable time each session and blocks new entries once that window opens, so nothing survives into the overnight.
//@version=5
strategy("Target Filled - EOD Flatten Example", overlay=true)
// --- Inputs ---------------------------------------------------
flatHour = input.int(15, "Flatten Hour (ET)", minval=0, maxval=23)
flatMin = input.int(55, "Flatten Minute", minval=0, maxval=59)
// --- Time check on the NY clock ------------------------------------
etHour = hour(time, "America/New_York")
etMin = minute(time, "America/New_York")
atFlatten = etHour == flatHour and etMin >= flatMin
// --- Force-close at the flatten window, block new entries inside it ----
if atFlatten and strategy.position_size != 0
strategy.close_all(comment="EOD Flatten")
if not atFlatten
if ta.crossover(ta.ema(close, 9), ta.ema(close, 21))
strategy.entry("Long", strategy.long)
strategy.exit("Exit", "Long", profit=20, loss=10)
// Visual: orange shading marks the flatten window
bgcolor(atFlatten ? color.new(color.orange, 92) : na, title="EOD Window") Recommended flatten times
15:55 ET — five minutes ahead of the 4:00 PM close of NY Regular Trading Hours — is a solid default, for a few reasons:
- Liquidity is still healthy at that point, so exits fill more cleanly than they would in the session's final seconds.
- It steps around the market-on-close order-imbalance window, where large institutional flow can move price unpredictably.
- A five-minute cushion means the position is confirmed flat well before the session actually ends, even with a little platform or webhook latency.
A strategy built around the last half hour of the session should probably move this earlier — 15:45 or even 15:30 — so the flatten logic doesn't interfere with legitimate late-session setups.
atFlatten is true — otherwise a trade could open right at the flatten time only to get force-closed a moment later at a loss from the spread alone.
Which firms require EOD flatten
Overnight-hold policy varies by firm and by account type. The table below reflects commonly published rules — always confirm the current policy with a specific firm before relying on it.
| Firm | Evaluation | Funded / Live |
|---|---|---|
| Topstep | No overnight holds | No overnight holds (most accounts) |
| Apex Trader Funding | Overnight holds generally permitted | Overnight holds permitted on funded accounts |
| FTMO (futures) | Check current firm rules | Check current firm rules |
| MyFundedFutures | Check current firm rules | Check current firm rules |
| TradeDay | No overnight holds | Check current firm rules |