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.

Pine Script v5 Free · TradingView free plan
Pine Script
//@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.

Entries are blocked, not just positions closed. This snippet also stops new entries once 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.

FirmEvaluationFunded / Live
TopstepNo overnight holdsNo overnight holds (most accounts)
Apex Trader FundingOvernight holds generally permittedOvernight holds permitted on funded accounts
FTMO (futures)Check current firm rulesCheck current firm rules
MyFundedFuturesCheck current firm rulesCheck current firm rules
TradeDayNo overnight holdsCheck current firm rules

Frequently Asked Questions

Why flatten positions before the market closes?
Holding into the close carries gap risk — the next session can open far away from where the last one settled, and there's no chance to react before that move is already on the books. Evaluation accounts typically forbid overnight holds outright, and even where a funded account allows it, the added exposure rarely pays for itself on a strategy that was built and tested on regular-hours data.
Which prop firms don't allow overnight holds?
Topstep prohibits overnight positions across its evaluation accounts and requires everything closed before the regular session ends. Most other major firms, including FTMO's futures offering and MyFundedFutures, apply similar restrictions during the evaluation. Rules shift, so it's worth confirming directly with a specific firm before assuming either way.
What time should the flatten trigger?
15:55 ET is a reasonable default — five minutes ahead of the 4:00 PM close of NY regular hours, which leaves enough time for a clean fill instead of getting caught in a thin, closing-minutes market. Strategies trading CME futures on the Globex session should adjust the time to whatever session-close rule their specific firm actually enforces.

Get a Pine Script tuned to your prop firm.

Invite-only on TradingView within 24 hours. From $19/mo, cancel anytime.