[Canonical HTML article](https://algoway.trade/blog/pine-script-close-vs-exit.html)

Published: 2026-10-21T10:00:00+04:00

Updated: 2026-10-21T10:00:00+04:00

# **Pine Script strategy.close vs strategy.exit Explained**

<img src="https://algoway.trade/static/img/blog-img/pine-script-close-vs-exit.jpg" style="display:block; max-width:800px; width:100%; height:auto; margin:0 auto 1.5rem auto;" data-fetchpriority="high" decoding="async" alt="Pine Script strategy.close vs strategy.exit Explained" />

**Use `strategy.close()` when a condition should request a market exit. Use `strategy.exit()` when the exit depends on a stop, target or trailing price rule.** Both can reduce a position, but they give the broker emulator different instructions.

That choice also matters for TradingView automation. An order-fill alert follows the simulated fill, not merely the line of code that created the order. For an AlgoWay workflow, use the [strategy alert timing guide](https://algoway.trade/blog/tradingview-strategy-alert-timing.html) to separate the script decision, emulator fill and connected-account execution. Decide what the exit means before deciding what its webhook should say.

## Choose the instruction before choosing the function

| Intention                                   | Pine command           | ID meaning                                          |
|---------------------------------------------|------------------------|-----------------------------------------------------|
| Exit matching entries at market             | `strategy.close()`     | `id` identifies the entry                           |
| Place stop or target exits                  | `strategy.exit()`      | `id` names the exit; `from_entry` selects the entry |
| Exit the whole simulated position at market | `strategy.close_all()` | No entry ID required                                |

In a price-based exit, `stop` and `limit` are absolute prices. The `loss` and `profit` alternatives are tick distances from entry. Supplying both `stop` and `limit` creates stop-loss and take-profit orders, not a single stop-limit order. These distinctions come from TradingView's [strategy order documentation](https://www.tradingview.com/pine-script-docs/concepts/strategies/).

## A matching entry ID is not an exit-order name

Consider an illustrative entry named `LongSetup`. These fragments show two different exit intentions; they are not a complete strategy or a tested trading system:

    // A condition elsewhere decides when to request this market close.
    strategy.close("LongSetup")

    // A separate illustration: price-based protection for that entry.
    strategy.exit("LongProtection", from_entry = "LongSetup",
                  stop = 98.0, limit = 104.0)

The numbers are hypothetical prices. They are not percentages and are not suitable defaults for an arbitrary symbol. Calling `strategy.close("LongProtection")` would address an entry with that name, not the protective exit. Likewise, a `from_entry` value with no matching entry produces no exit orders. Omitting `from_entry` broadens the exit to all entries and can affect subsequent entries until the position closes.

## “Close” does not promise the candle's closing price

By default, a newly created market order waits for the next available tick. With ordinary bar-close calculation, that commonly means the next bar's open. The name `strategy.close()` describes closing a trade, not a promise to execute at the chart's `close` value.

TradingView's [strategy FAQ](https://www.tradingview.com/pine-script-docs/faq/strategies/) explains the `immediately` option for a particular market close and the broader `process_orders_on_close` setting. These alter simulated execution timing. They do not guarantee the same price in a connected account, particularly when a signal arrives as a market session ends.

## Check the alert that leaves the strategy

A price exit can be created well before it fills. If automation uses order-fill alerts, creating that simulated stop does not itself send a stop-placement instruction to the destination. Broker-side protection needs its own supported configuration; it should not be inferred from a line drawn by the strategy.

TradingView supports custom `alert_message` values on order-generating commands. When using them, include `{{strategy.order.alert_message}}` in the alert message field and select the intended fill events. An adjacent `alert()` call is a separate event mechanism. See the [official alert documentation](https://www.tradingview.com/pine-script-docs/concepts/alerts/).

Before connecting an exit, write down three things: which entry it addresses, whether it requests a market close or establishes a price condition, and which event sends the execution message. Then inspect one example in the Strategy Tester's trade list and a separate demo-account workflow. A label that says “exit” is a useful beginning; it is not an execution specification.

Last updated: October 21, 2026
