Partial profit-taking is often implemented as several calls to strategy.exit(). A subtle Pine Script v6 behavior is that each exit call reserves part of the open position. A later exit call cannot close the portion already reserved by an earlier call, even when its requested quantity equals the entire position. This can leave the simulated position underprotected.
Worked example: six units at an illustrative entry of 100
Assume a long of six units, a shared stop at 98 and profit targets at 104 and 108. If a target-only call reserves three units at 104, a later independent stop call requesting six can reserve only the remaining three. If the stop is hit first, the first three units may remain open. Instead assign a stop and a limit to each three-unit bracket. Both brackets together reserve all six units. When target A fills, three units remain and bracket B retains the stop for those three units.
| Design | Target A | Target B | Stop coverage initially |
|---|---|---|---|
| Separate target first | 3 at 104 | None | Only 3 of 6 units reserved for the later stop |
| Two brackets | 3 at 104 | 3 at 108 | 3 + 3 at 98 |

Illustrative Pine Script v6 implementation
The following minimal example uses a 10/30 SMA crossover simply to demonstrate order placement. The prices are fixed hypothetical levels for a market trading near 100; they are not appropriate for arbitrary symbols. It submits both exit brackets on the same calculation as the entry request and links them to entry ID L. Each call has a unique exit ID and a quantity of three.
//@version=6
strategy("Two bracket example", overlay = true, pyramiding = 0)
fast = ta.sma(close, 10)
slow = ta.sma(close, 30)
signal = ta.crossover(fast, slow)
if signal and strategy.position_size == 0
strategy.entry("L", strategy.long, qty = 6)
strategy.exit("A", "L", qty = 3, stop = 98, limit = 104)
strategy.exit("B", "L", qty = 3, stop = 98, limit = 108)
strategy.exit() creates price-based orders in the broker emulator. When a bracket's stop or target executes, the sibling is canceled, and exit orders participate in the default reduction behavior. The calls reserve distinct portions of the position. Do not repeatedly submit an already-filled partial exit without a deliberate rearming rule. If designing adaptive prices, calculate and freeze the intended levels for each trade and verify whether they are based on signal price or actual fill price.
Test cases before relying on the results
- Stop first: with six open units and no previous target execution, test that both three-unit brackets close, leaving zero units.
- Target A then stop: verify three units exit at the first target and the remaining three exit at the stop.
- Both targets: verify total exit quantity equals six and the strategy never reverses unexpectedly.
- Same-bar stop and target: inspect the broker emulator assumptions and compare with Bar Magnifier when available; neither guarantees real fill sequencing.
- Gap, fees, slippage and minimum lot size: validate the actual quantities and prices rather than assuming every theoretical order is fillable.
Broker emulator versus live orders
Pine strategies simulate orders; their order-fill alerts can trigger a webhook or integration but are not confirmation of broker execution. A bridge should reconcile real fills, attach or resize actual protective orders to the broker's remaining position, deduplicate alerts, and handle partial fills, rejected orders and reconnection. Never assume emulator OCA behavior automatically exists at the live broker.
Official references: Pine Script strategies and strategy FAQ.
This is an illustrative, uncompiled and unbacktested teaching example. Trading involves risk of loss; hypothetical performance does not predict future returns.
If you would like to adapt this free strategy idea to your own trading rules, contact us about Custom Tradingview strategy Development Services.