Skip to content
Pinescripter

Pine Script Strategy Limit Order Backtest

A limit order backtest depends on more than the limit price in your code. Learn how to place Pine Script limit entries, interpret fills, and test assumptions carefully.

A Pine Script strategy limit order backtest uses strategy.entry() with a limit price. The order waits until the emulator considers that price reached, but the fill depends on bar data, verification settings, and intrabar assumptions.

How do you place a limit entry in Pine Script?

Use strategy.entry() and pass the desired price with its limit argument. Unlike a market entry, the limit order does not mean “enter now”; it asks the broker emulator to wait for price to reach the specified level.

//@version=6
strategy("Limit pullback example", overlay = true)

length = input.int(20, "Moving average length", minval = 1)
pullbackTicks = input.int(10, "Limit offset in ticks", minval = 1)
ma = ta.sma(close, length)

longSignal = ta.crossover(close, ma)
limitPrice = close - pullbackTicks * syminfo.mintick

if longSignal
strategy.entry("Long", strategy.long, limit = limitPrice)

plot(ma, "Moving average", color = color.orange)

This example places a buy limit below the signal bar’s close. The offset is expressed in ticks and converted to a price using syminfo.mintick. It is only an example of order mechanics—not a claim that this entry rule is profitable or suitable for a particular market.

The signal condition and the order price are separate decisions. Here, a crossover creates the order, while the limit price is calculated from that bar’s close. If price never reaches the order level afterward, the entry may remain unfilled. To build a testable rule, decide when the order is created, how long it should remain active, and what event cancels or replaces it. For a related example of how Pine strategies issue and track orders, see our guide to turning an indicator into a backtestable strategy.

What does a limit-order fill mean in a backtest?

A backtest fill is the result of a simulation, not proof that a live order would have executed at that price. Historical bars typically provide open, high, low, and close values, not a complete record of every trade or the order book. The emulator must use the available data and its fill rules to decide whether a pending order was reached.

For a buy limit, the price must be available at or below the limit for the order to be eligible under the model. A bar’s low reaching the limit can therefore be enough for a simulated fill under the default assumptions. But a price touch in a chart does not tell you how much liquidity was available, how your order would have queued, or whether a real order would have been filled in full.

There is also a gap case. If the market opens beyond a pending order’s level, the emulator may fill at the open rather than the requested limit price. That can make the recorded fill better than the limit price for a buy, but it is still a modeled outcome. Bar-based results can also be ambiguous when an order is created and price moves through its level within the same bar.

We explain the broader assumptions behind fills, bar-close calculations, costs, and lookahead in how TradingView backtesting actually works. When reading a result, check the order’s creation bar, fill bar, and fill price in the trade list—not just the equity curve.

A buy limit level waits below the signal bar on a candlestick chart

What does limit verification change?

The backtest_fill_limits_assumption strategy setting adds a verification requirement for limit orders. Instead of treating a touch of the limit as sufficient, the emulator requires price to move beyond the limit by the configured number of ticks before it considers the order verified. The order is still filled at its limit price in the simulation; the extra movement is a test for whether the limit was reached with more than a bare touch.

In a strategy declaration, the setting is expressed as an integer number of ticks. For example:

//@version=6
strategy(
"Limit entry with verification",
overlay = true,
backtest_fill_limits_assumption = 1
)

The value is not a percentage or a raw price distance. It is a number of minimum price increments. If the instrument’s tick size changes, the price distance represented by the same tick count changes too. A stricter verification requirement may reduce the number of simulated fills, so compare trade counts and outcomes instead of assuming that one setting is automatically more realistic.

This is a useful way to check whether a strategy’s apparent performance relies on barely touched levels. It does not simulate queue priority, market depth, partial fills, or your brokerage’s execution. Consider comparing the default behavior with one or more verification settings that make sense for the instrument, then review which trades disappear and whether the strategy still behaves as intended.

Why can a limit order fill on the touch?

A bar chart compresses price movement into a handful of values. When its low reaches a buy limit, the default historical fill model can treat the order as reached even though the chart cannot show how long price stayed there, whether trades occurred at that level, or how much volume was available. That is why a chart that appears to show a quick touch can still have a fill marker.

This does not necessarily indicate a coding error. It can be the expected outcome of the emulator’s assumptions. Look at the order’s limit, the bar’s range, and the strategy’s limit-verification setting. If your result changes substantially when you require movement beyond the limit, the strategy may depend on fills that are sensitive to the model.

Also check whether the order existed before the bar began. If your script calculates a signal at the close and creates a limit order then, the order cannot generally be treated as though it had been waiting throughout that completed bar. The default timing of order creation matters. The process_orders_on_close setting changes when some orders can be processed, but it is not a shortcut for making every intrabar limit fill realistic. Our explanation of process_orders_on_close covers what that setting changes and what it does not.

For a practical debugging pass, mark the signal bar and limit level on the chart, then compare them with the trade list’s entry time and price. If those do not match your mental model, clarify the order’s creation and cancellation rules before adjusting the fill settings.

How do Bar Magnifier and verification differ?

Limit verification and Bar Magnifier address different parts of the simulation. Limit verification asks for price to move a specified number of ticks beyond a limit before the emulator accepts the limit order as reached. Bar Magnifier uses available lower-timeframe bars to provide more detail about the path price took inside a chart bar.

More granular bars can help with intrabar order sequencing, such as deciding which of two price levels was reached first. But they still do not provide a full record of individual trades, order-book depth, or queue position. Bar Magnifier also depends on lower-timeframe data being available for the historical period, so it cannot make every bar equally detailed.

A good comparison isolates the settings rather than changing several things at once:

  1. Run the strategy with the normal fill assumptions and record the trade list.

  2. Change only the limit-verification setting and compare fills and trade count.

  3. If available and relevant to your test, enable Bar Magnifier and compare again.

  4. Inspect trades that changed, especially entries or exits where the bar range crossed multiple levels.

Our guide to Pine Script Bar Magnifier walks through what lower-timeframe detail changes and where the emulator still has to make assumptions. Neither setting guarantees a live fill at the backtest’s displayed price.

Lower-timeframe bars add detail inside a larger chart bar

How can you make a limit-order test more cautious?

There is no single setting that makes a backtest “realistic” for every market and order size. Instead, test how sensitive the result is to assumptions you cannot fully observe in historical bars.

Start by checking the limit price and order lifetime. A limit order that stays active indefinitely can fill long after the signal that created it. If your rules require the order to expire, cancel it explicitly. If a new signal should replace the old order, define that behavior too. Otherwise, the code may test a different trading process from the one you intend.

Next, compare touch-based fills with a verification requirement. Then examine the impact of Bar Magnifier where available. Keep the settings and chart timeframe fixed while making each comparison. Review individual trades, not only total profit: changed fills can alter exits, position overlap, and the order of later signals.

Finally, model trading costs. Commission and slippage may affect results even when the entry is a limit order; a limit price does not remove every cost or guarantee execution without adverse effects. Set assumptions that fit the instrument and execution approach you are evaluating, and test the sensitivity of results to less favorable inputs. Our guide to commission and slippage settings explains how those costs are represented in Pine strategies.

A cautious backtest is not one that produces a preferred number. It is one where the rules, order timing, fill assumptions, and costs are visible enough that you can explain what changed when the result changed.

How should you read the finished backtest?

Treat the report as a description of what your coded rules did under the chosen historical simulation—not a forecast. A strategy can compile and produce a full trade list while still expressing the wrong order logic or relying on assumptions that do not hold in live markets.

Review the order details first. Confirm that the limit price is calculated from the intended reference, that an order is created only when the intended signal occurs, and that stale orders are canceled or replaced as your rules require. Check whether fills happen on the bars and at the prices you expect. Pay attention to trades whose outcome changes when you vary limit verification or intrabar detail.

Then evaluate the rest of the report. Consider trade count, drawdown, long and short behavior, and performance across different periods or markets—not only net profit. A small number of fills can make a strong-looking result especially fragile. Avoid repeatedly changing the rules until the same historical sample looks ideal; that tests your ability to fit past data rather than the robustness of the order logic.

If you are building the script with PineScripter, we can turn a request into a requirements checklist, ask clarifying questions when the order rules are ambiguous, and check the script for compile errors. Our strategy simulations use the starting capital, commission, and slippage you specify, and simulate historical data; they are not forecasts. You can also use our strategy report to inspect simulated results and individual trades. Whatever tool you use, the key is to verify the rules and assumptions before interpreting the headline metrics.

Frequently asked questions

Can a Pine Script backtest show an unfilled limit order?
Yes. If price does not reach the order level under the emulator’s fill rules before the order is canceled or replaced, it may remain unfilled. Define the order’s lifetime and cancellation conditions explicitly.
Does a limit order fill exactly at its limit price?
The simulated fill is determined by the emulator’s rules. A gap through the level can result in a fill at the bar open, and limit verification can require price to move beyond the limit while the recorded fill remains at the limit.
Does Bar Magnifier guarantee accurate limit fills?
No. It adds lower-timeframe bar detail where available, which can help resolve some intrabar sequencing. It does not model every trade, order-book depth, queue position, or live execution condition.
How do I make a limit-order backtest more conservative?
Compare results with limit verification enabled, use Bar Magnifier where appropriate and available, set commission and slippage, and inspect trades that change. These checks expose sensitivity; they cannot guarantee live fill behavior.

This article was drafted with AI assistance from Pinescripter's own information and current sources.

  • Illustration of a trading position being built through successive entries

    Pine Script Strategy Pyramiding: Entries and Limits

    Pyramiding lets a Pine Script strategy add entries in the same direction. Learn how to set the limit, what strategy.entry() does, and how to check the resulting trades and exposure.

    Siddhant Chaudhary ·