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 the EOD window opens. No open positions survive to the overnight session.
//@version=5
strategy("EOD Flatten", overlay=true)
// ── Inputs ──────────────────────────────────────────────
flattenHour = input.int(15, "Flatten Hour (ET)", minval=0, maxval=23)
flattenMin = input.int(55, "Flatten Minute", minval=0, maxval=59)
// ── EOD close logic ──────────────────────────────────────
nyHour = hour(time, "America/New_York")
nyMin = minute(time, "America/New_York")
isEOD = nyHour == flattenHour and nyMin >= flattenMin
if isEOD and strategy.position_size != 0
strategy.close_all(comment="EOD Flatten")
// ── Entry logic (only when not at EOD) ───────────────────
if not isEOD
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
bgcolor(isEOD ? color.new(color.orange, 92) : na, title="EOD Window")
Recommended flatten times
The default flatten time is 15:55 ET — five minutes before the 4:00 PM close of NY Regular Trading Hours. This is the standard recommendation for several reasons:
- Liquidity is still reasonable at 15:55, so fills are cleaner than in the final 60 seconds of the session.
- You avoid the MOC (market-on-close) order imbalance period where large institutional orders can spike prices unpredictably.
- A 5-minute buffer ensures the position is fully confirmed closed before the session end, even with platform latency or webhook delays.
If your strategy targets the last 30 minutes of the session, consider moving the flatten time earlier — 15:45 or even 15:30 — to prevent the EOD logic from interfering with legitimate late-day setups.
isEOD is true. This prevents a trade from opening right at the flatten time and being immediately closed at a loss due to spread on the forced exit.Which firms require EOD flatten
Overnight hold restrictions vary by firm and account type. The table below reflects commonly published rules, but always verify with your specific firm before trading — rules change.
| Firm | Eval Account | Funded / PA Account |
|---|---|---|
| Topstep | No overnight holds | No overnight holds (most accounts) |
| Apex Trader Funding | Overnight holds generally permitted | Overnight holds permitted on PA |
| FTMO (futures) | Check firm rules | Check firm rules |
| MyFundedFutures | Check firm rules | Check firm rules |
| TradeDay | No overnight holds | Check firm rules |
Frequently Asked Questions
Why should I flatten positions before market close?
Holding positions overnight exposes you to gap risk — the market can open significantly higher or lower than where it closed, which can produce large unexpected losses before you can react. For prop firm eval accounts, overnight holds are typically prohibited outright. For funded accounts they may be allowed, but the added risk rarely justifies it for systematic strategies that are optimized on RTH data.
Which prop firms prohibit overnight holds?
Topstep prohibits overnight holds on all evaluation accounts and requires positions to be closed before the end of the regular session. Most other major firms — including FTMO futures accounts and MyFundedFutures — have similar restrictions on evaluation accounts. Always verify the current rules with your specific firm before trading, as policies change.
What time should I set my EOD flatten?
The recommended default is 15:55 ET — five minutes before the 4:00 PM close of NY RTH. This gives the market enough time to fill your exit at a reasonable price rather than hitting a thin market in the final minutes. If you trade CME futures on the globex session, adjust to match your firm's specific session close requirement.
Related
Want a complete strategy with EOD flatten pre-wired?
Our scripts include EOD flatten, kill switch, session filter, and bar-close entries — tested on 3 years of MES/NQ data and ready for TradersPost automation. Instant delivery from $50.
View Plans — From $50