Tutorial

Pine Script process_orders_on_close: Fill on the Signal Bar

See how process_orders_on_close moves strategy fills from the next bar open to the signal bar close, when to use it, and what it hides.

•10 min read

You write a strategy that enters when a moving average crosses. You look at the chart, see the cross happen on a candle, and then notice that the Strategy Tester's entry marker sits one bar to the right, at a different price. Nothing is broken. The default fill model in Pine Script is doing exactly what it is documented to do, and one parameter in the strategy() declaration, process_orders_on_close, changes it. This post explains what the setting does, when it is the right choice, and the few ways it can mislead you if you switch it on without thinking.

What the default fill model does

A Pine Script strategy runs once per bar. On historical bars it runs after the bar has closed, so by the time your if ta.crossover(...) condition is true, the bar it was true on is already over. At that point the market is on the next bar, so the broker emulator has nowhere to fill the order except the next bar's open. That is the default: an order placed on the close of bar N is filled at the open of bar N+1.

This is a sensible rule. It matches what would happen if you looked at a closed candle, decided, and clicked buy. You cannot buy at a price that has already printed and gone. The cost is that the fill price you see in the Strategy Tester is the next open, which can be quite different from the close you were looking at when the condition became true, especially after a gap or on a fast market.

The same rule applies to every order type. Market orders from strategy.entry and strategy.order fill at the next open. A strategy.close call fills at the next open too. Limit and stop orders sit on the broker emulator's books and are tested against price movement from the next bar onward.

What process_orders_on_close changes

Setting process_orders_on_close = true in the strategy() call asks the broker emulator for an extra attempt to execute orders after a bar closes and your strategy calculations are complete. In TradingView's words, market orders are executed before the next bar's open, which in practice means at the close of the bar that produced the condition. Orders that depend on price, such as limit and stop orders, only fill if their price conditions are met at that point.

So with the setting on, the signal bar and the fill bar are the same bar. The marker lands on the candle where the condition became true, and the fill price is that candle's close.

The parameter is a constant, so it must be a literal true or false in the strategy() call. You cannot drive it from input.bool(). If you want to compare the two behaviours, you keep two copies of the script, which is what the next section does.

See the difference on one script

Here is a plain moving average cross with the default fill model. It enters long on the cross up and closes on the cross down, using the whole account equity per trade and a percentage commission.

pine
//@version=6
strategy("Cross, next-bar fills", overlay = true, initial_capital = 10000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, commission_value = 0.1)

fastLen = input.int(10, "Fast length", minval = 1)
slowLen = input.int(30, "Slow length", minval = 2)

fast = ta.sma(close, fastLen)
slow = ta.sma(close, slowLen)

if ta.crossover(fast, slow)
    strategy.entry("Long", strategy.long)

if ta.crossunder(fast, slow)
    strategy.close("Long")

plot(fast, "Fast", color = color.teal)
plot(slow, "Slow", color = color.orange)

Now the same logic with the setting switched on. Only the strategy() line and the title change.

pine
//@version=6
strategy("Cross, same-bar fills", overlay = true, initial_capital = 10000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, commission_value = 0.1, process_orders_on_close = true)

fastLen = input.int(10, "Fast length", minval = 1)
slowLen = input.int(30, "Slow length", minval = 2)

fast = ta.sma(close, fastLen)
slow = ta.sma(close, slowLen)

if ta.crossover(fast, slow)
    strategy.entry("Long", strategy.long)

if ta.crossunder(fast, slow)
    strategy.close("Long")

plot(fast, "Fast", color = color.teal)
plot(slow, "Slow", color = color.orange)

Add both to the same chart and open the List of Trades tab for each. In the first, each entry time is one bar after the candle where the averages crossed, and the entry price equals that later candle's open. In the second, the entry time is the crossing candle itself and the price equals its close. Every trade in the list moves in the same way, so the totals in the Performance Summary shift too, in a direction that depends entirely on the market you test and not on anything about the setting.

A hypothetical shows why. Say the crossing candle closes at 100.00 and the next candle opens at 101.50 after a small gap up. With the default model the entry is at 101.50. With the setting on, the entry is at 100.00. The second number looks better for a long entry, and that is the problem covered below: the second number is also the one you were less likely to actually get.

Label the fills so you can check them

Staring at the trade list works, but a label on the chart makes the timing obvious at a glance. This version prints the actual entry price of the open trade on the bar where the position first appears. Run it with and without the setting and watch the label move.

pine
//@version=6
strategy("Fill price labels", overlay = true, process_orders_on_close = true, default_qty_type = strategy.fixed, default_qty_value = 1)

fast = ta.sma(close, 10)
slow = ta.sma(close, 30)

if ta.crossover(fast, slow)
    strategy.entry("Long", strategy.long)

if ta.crossunder(fast, slow)
    strategy.close("Long")

openedNow = strategy.opentrades > 0 and strategy.opentrades[1] == 0

if openedNow
    label.new(bar_index, high, "Entry " + str.tostring(strategy.opentrades.entry_price(0), format.mintick), style = label.style_label_down, color = color.new(color.teal, 20), textcolor = color.white)

With process_orders_on_close = true, the label appears on the crossing candle and shows that candle's close. Remove the parameter and the label appears one candle later, showing the open. This is a useful habit for any strategy: if you do not know which bar a fill belongs to, print it.

When this setting is the right choice

The honest reason to enable it is that your rule is genuinely defined at the close and you intend to act at the close. Some examples follow.

End-of-day strategies on daily bars are the classic case. If your rule is "exit at the close if the day closes below the 20-day average", you may be able to place the order in the closing auction or in the last minutes of the session. The default model would instead fill you at the next day's open, which is a different trade with overnight risk in between. Matching the model to the rule you really intend to run is the whole point.

Session-close rules on intraday charts follow the same logic. A rule such as "flatten at the last bar of the session" is meant to leave you flat when the session ends. Next-bar fills would carry the trade into the following session's first bar.

Signals built from the close of a higher timeframe candle also fit. If you evaluate a condition on a daily close and want the order recorded on that daily bar, the setting keeps the marker and the decision together.

A final reason is readability. When you debug a strategy, having the entry marker on the candle that triggered it removes a source of confusion. Plenty of people turn the setting on only to check that their condition fires where they expect, then compare against the default.

The pitfall: the close is not a price you can always get

The default model is conservative about information. By the time the next open arrives, the close that triggered your condition is a known, finished number. With the setting on, your script sees the final close and also gets filled at it. On a historical chart, that is a perfect result: you act on the close with full knowledge of the close.

In live conditions a candle's close is only known at the instant it closes. The order has to be sent and filled in that instant or just after, and the price you get can differ from the printed close. How far it differs depends on the market, the session, and your broker or exchange. The Strategy Tester has no way of knowing this, so it fills at exactly the close.

Treat the same-bar fill as a best-case assumption, not a measurement. Two sensible responses help. First, add slippage so the emulator charges you something for the gap between the close you saw and the price you could realistically get. Second, add commission, which you should already be doing.

pine
//@version=6
strategy("Cross, same-bar fills with costs", overlay = true, initial_capital = 10000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, commission_value = 0.1, slippage = 2, process_orders_on_close = true)

fast = ta.sma(close, 10)
slow = ta.sma(close, 30)

if ta.crossover(fast, slow)
    strategy.entry("Long", strategy.long)

if ta.crossunder(fast, slow)
    strategy.close("Long")

plot(fast, "Fast", color = color.teal)
plot(slow, "Slow", color = color.orange)

The slippage parameter is measured in ticks and applies to market and stop orders, so two ticks here means each such fill is worse than the reference price by two minimum price increments. The right number depends on the symbol. For the full picture of how these costs add up, see how slippage and commission eat your returns and the commission and slippage settings in the strategy declaration.

Interaction with stops, limits and strategy.exit

Many strategies pair an entry with strategy.exit for a stop loss and take profit. Those are price-conditional orders, so they do not need the closing attempt in the same way. They are tested against price movement bar by bar. What changes is the starting point: if your entry fills at the close of the signal bar, the exit orders are placed right after it and can be tested from the next bar on, with the entry price at the close instead of the next open.

That shifts your stop and target distances in price terms when they are computed from the entry price. If you set the stop with strategy.position_avg_price, the reference price is the fill price, so it moves with the setting. Re-check any exit logic after changing the fill model, because the numbers you tuned for next-open fills do not carry over. For a complete walkthrough of exits, read stop loss and take profit in Pine Script.

Limit entries deserve care. Because a price-conditional order only fills if its price condition is met when the extra attempt happens, a limit order placed at close may or may not execute at that moment. If it does not, it remains working and follows the ordinary rules afterward. Do not assume that turning the setting on makes every order fill on the signal bar.

Calculations, alerts and the live bar

Two neighbouring settings are often confused with this one, so it helps to separate them.

calc_on_every_tick controls whether the script recalculates on every price update of the live bar rather than once at close. It affects realtime behaviour, not how orders are filled on historical bars. If you use both, read what calc_on_every_tick changes in a backtest first, because the combination is where most surprises come from.

use_bar_magnifier makes the emulator look at lower timeframe data inside each bar to decide the order of events, such as whether a stop or a target was hit first. It is about intrabar accuracy, not about when market orders fill. See the bar magnifier explained.

Alerts add a third angle. An alert on a strategy fires when an order is generated, and the timing of a live alert is tied to when your script executes, not to how the historical fill is drawn. A strategy that looks perfect with same-bar fills on history can still deliver its alert at a time that makes the same-bar price unreachable. That gap is one of the main reasons backtest results differ from live results.

A short checklist before you rely on the setting

Decide first whether your rule is actually a close rule. If the logic only needs a condition to be true, and you would act whenever you noticed, next-bar fills are the more honest model.

Then compare both versions on the same symbol, timeframe and date range, and read the trade list rather than just the headline number. If the same-bar version looks much better, the difference is mostly the gap between the close and the next open, and that is exactly the part you may not be able to capture.

Add realistic commission and slippage to whichever version you keep. And write down which fill model a result was produced with, because a number without its fill model is hard to reproduce a month later. The broader mechanics are in how TradingView backtesting works and the Pine Script execution model.

Switching the setting without rewriting the script

Once a strategy is long, changing the fill model is a small edit to the strategy() line, but it often leads to follow-up changes: moving a stop that was tuned for next-open fills, adding slippage, or adding a label so you can see where fills land. In PineScripter you can describe that change in plain English, for example "turn on same-bar fills, add two ticks of slippage and label each entry with its fill price". The AI code editor edits only the lines that change, shows them in the Code Changes view, and checks its own output for compile errors before you paste anything into TradingView. The fill model itself is still tested in TradingView's Strategy Tester, since PineScripter does not simulate trades. Plans start at $9.99 per month on the pricing page.

Disclaimer: PineScripter is a coding tool for Pine Script development. It does not provide financial advice and does not guarantee trading profits. Always backtest strategies thoroughly and understand the risks before live trading.

Back to Blog

Frequently asked questions

What does process_orders_on_close do in Pine Script?

It asks the broker emulator for an extra attempt to execute orders after a bar closes. Market orders then fill at the close of the signal bar instead of the next bar's open.

Can I turn process_orders_on_close on with an input?

No. It is a constant in the strategy() declaration, so it must be a literal true or false. To compare both behaviours, keep two copies of the script.

Why do my backtest results look better with it on?

The close that triggered the condition is often better than the next open, and the Strategy Tester fills at exactly that close. In live conditions you may not get that price, so add slippage and commission.

Is it the same as calc_on_every_tick?

No. calc_on_every_tick controls recalculation on the live bar, while process_orders_on_close controls when market orders fill relative to the signal bar.

Does it change stop loss and take profit orders?

Those are price-conditional orders and are still tested against price movement. Their reference price changes if your entry fills at the close, so recheck distances computed from the entry price.

Describe your indicator in plain English and get Pine Script that compiles →


Disclaimer: PineScripter is a coding tool for Pine Script development. It does not provide financial advice and does not guarantee trading profits. Always backtest strategies thoroughly and understand the risks before live trading.