r/pinescript • u/Omega_Grinder • 17d ago
process_orders_on_close: next-open fills, signal-bar close fills, and the session last-bar gap
Default strategy fill timing is easy to miss until live results diverge from the report.
On historical bars a Pine strategy runs once, at bar close. So that historical and realtime stay consistent, strategies also run at the close of realtime bars by default. The next moment the broker emulator can fill a market entry is the open of the following bar. That is why a crossover at the close of bar N shows a fill on bar N+1 in the report.
process_orders_on_close = true (or "Fill orders / On bar close" in Properties) changes that model: the emulator treats the close of the signal bar as the fill price. strategy.close(..., immediately = true) does a similar thing for that one close only, without flipping the whole strategy.
Where it bites
- Session last bar: if a signal fires at the final close of the session, a real broker can usually act only on the next session open. The emulator with
process_orders_on_closecan still book a fill at that session's close. That gap is classic repaint between report and live. - Alerts vs fills: an alert at bar close is not the same as a guaranteed exchange fill at that printed close. Bid/ask, latency and the next tradable price sit in between.
- Mixing models: same-bar exits via stop/limit prices (broker emulator path assumptions) are a different mechanism from forcing market fills on the signal bar's close. Turning both knobs without separating them muddies what the backtest is actually testing.
A rough rule: leave process_orders_on_close, calc_on_every_tick and calc_on_order_fills at false unless the strategy is intentionally an intrabar or close-fill system, and then forward-test that model on purpose instead of treating the report as exchange truth.
Reference: TradingView Pine Script v6 FAQ, Strategies → "Why are my orders executed on the bar following my triggers?" and the repainting notes on strategy properties.
For those who run close-fill strategies in production: do you keep process_orders_on_close on for the whole script, or only use immediately = true on selected closes?
