cTrader

AlgoWay Blog

Guides, manuals and platform references.

Core
JSON & Alerts
MT5 / Expert Advisors
Errors
Integrations
Other Publications
AlgoWay Strategy Lab

Pine Script strategy.close vs strategy.exit Explained

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 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

IntentionPine commandID meaning
Exit matching entries at marketstrategy.close()id identifies the entry
Place stop or target exitsstrategy.exit()id names the exit; from_entry selects the entry
Exit the whole simulated position at marketstrategy.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.

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 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.

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