SuperCharting
Sign in Get started Account Open charts

Learn / Guides

"Pine Script OCA groups: one-cancels-all orders in strategies"

How oca_name and oca_type work in Pine Script strategies, with a working breakout example where one fill cancels the other order, and the difference between cancel and reduce.

An OCA group ("one cancels all") links pending orders so that when one fills, the others in the group are cancelled or reduced. In Pine Script you set it with oca_name and oca_type on strategy.entry() and strategy.order(). SuperCharting's strategy tester runs OCA groups as tradingview.com does.

The three OCA types

oca_typeWhen one order of the group fills
strategy.oca.cancelEvery other order of the group is cancelled.
strategy.oca.reduceThe other orders are reduced by the filled quantity, and cancelled when nothing is left.
strategy.oca.noneNothing happens to the others (the default).

A working example: a breakout in either direction

//@version=6
strategy("Range breakout OCA", overlay = true)

hi = ta.highest(high, 20)[1]
lo = ta.lowest(low, 20)[1]

if strategy.position_size == 0
    strategy.entry("Long",  strategy.long,  stop = hi, oca_name = "breakout", oca_type = strategy.oca.cancel)
    strategy.entry("Short", strategy.short, stop = lo, oca_name = "breakout", oca_type = strategy.oca.cancel)

strategy.exit("Long exit",  "Long",  profit = 400, loss = 200)
strategy.exit("Short exit", "Short", profit = 400, loss = 200)

Two stop orders wait above and below the 20-bar range. When the price breaks one side, that order fills and the other is cancelled, so the strategy never ends up long and short from the same range.

How it behaves

How to try it

  1. Open a chart, for example NSE:NIFTY on the 15m interval.
  2. Open the Pine Editor, paste the example and click Add to chart.
  3. Open the Strategy Tester to see the trades. No trade opens on both sides of the same range.

For limits on drawdown, losses and position size, see the strategy risk guide.

Frequently asked questions

What is the difference between oca.cancel and oca.reduce?

cancel removes the other orders when one fills. reduce lowers their quantity by the filled amount, which suits scaling out of a position with several targets.

Does OCA work with strategy.close?

No. strategy.close() is a market order and takes no OCA group. Use OCA on strategy.entry() and strategy.order().

Try it on a live chart

SuperCharting is an AI-enabled charting and coding platform in your browser: charts, Pine-compatible scripts with AI Auto-fix, the Strategy Tester, alerts and the screener.

Create a free account More guides