Set a target position
Use setposition to reach a signed final volume on MT5 or cTrader. Check reusable tickets, sizing, stop and target rules, partial results, and recovery.
Last updated: 2026-10-19
setposition tells PineHook the final size you want to hold, rather than the size of an order to place. A positive target means long, a negative one means short and zero means flat, and PineHook compares that with your current position and trades only the difference. To copy a TradingView strategy you can't edit, see Mirror a TradingView strategy with setposition.
Send a target#
Use target= in place of a sizing parameter. These examples assume lot conversion is 1:1, so the target is in broker lots:
LICENSE_ID,setposition,EURUSD,target=0.30,magic=42,ticket=strategy-a
LICENSE_ID,setposition,EURUSD,target=-0.20,magic=42,ticket=strategy-a
LICENSE_ID,setposition,EURUSD,target=0,magic=42,ticket=strategy-a
{"license":"LICENSE_ID","action":"setposition","symbol":"EURUSD","target":0.30,"magic":42,"ticket":"strategy-a"}
| You hold | Target | What PineHook does |
|---|---|---|
| Nothing | 0.30 | Opens 0.30 long |
| 0.10 long | 0.30 | Adds 0.20 long |
| 0.30 long | 0.10 | Closes 0.20 |
| 0.30 long | -0.20 | Closes the long, then opens 0.20 short |
| Anything | 0 | Closes the selected positions, including both sides of a hedge |
| 0.30 long | 0.30 | Nothing |
A target is a one-time instruction. Once it's reached, a stop, a manual close or a later alert can change the position, and PineHook doesn't put it back. Sending the same target again asks for the same final size, so it never adds the volume a second time.
On MT5, setposition needs the updated EA, connected at the moment the alert arrives.
Choose the positions#
On a hedging account, ticket= names a reusable group of positions. An increase opens a new position for the difference, and a reduction closes the smallest positions first, partly closing at most one of the rest. The group keeps its ticket after you flatten or reverse it, so later alerts can reuse it, which ordinary buy and sell tickets can't. Without a ticket, the target covers every position on that symbol with the alert's magic number, or your Default Magic if you leave magic= out, and magic=ALL isn't accepted.
A group holding both longs and shorts can still need closes when its net total already matches the target, since PineHook unwinds the opposite side first. Commands such as closelong or modify sent with the group's ticket act on its positions.
On a netting or exchange account, the target applies to the symbol's one combined position, including anything added by hand or by other software, so leave out ticket= and run one strategy per account.
Volume and broker limits#
Your lot conversion applies to the target once, and PineHook then rounds its size down to the broker's volume step while keeping its direction. A nonzero target that ends up below the broker's minimum is refused, rather than rounded up or treated as flat. Some reductions can't reach the exact target under the broker's minimum sizes, and PineHook then reports a blocked or partial result instead of picking a different target.
A Trade Copier scales the target by each account's multiplier before that account's conversion, and each account reports its own result.
Stops, targets and other parameters#
You can add one stop loss and one take profit, in pips, as a price or as a percentage, and they apply to the whole group, with pip and percentage distances measured from its average entry price. Leaving them out keeps the stops and targets you already have. A zero distance doesn't remove a stop, so use modify for that.
A ticket group acts as one position#
On a hedging account, the positions of a ticket= group share one stop loss and one take profit:
- A position a target adds copies the group's stop loss and take profit when the alert leaves them out. If the group's positions don't share one level, for example when copying a change to one of them failed at your broker, the added position gets none for that side and the result is Partial.
modifywith pips or a percentage,breakevenandtrailsent with the group's ticket measure from the group's average entry price, so every position ends up at the same level and trails from the same point.- Changing the stop loss or take profit of one group position in Open Positions changes it on every position in the group.
- Changing the stop loss or take profit of one group position directly in MetaTrader or the cTrader app copies it to the rest of the group about 2 seconds later. If you change several, the last change wins.
Without a ticket, the stop loss and take profit in a target alert still cover every position it selects, but nothing else ties those positions together: an added position gets only the stops in its own alert, and modify, breakeven, trail and dashboard changes work position by position.
The command also accepts entry filters such as maxspread_pips, minequity and hedging=, along with magic, ticket, id, comment and auth. It rejects every size_ parameter, partial-close amounts, delay, pending-order and expiry fields, closetime, new_ticket, direction filters, symbol=ALL, SL/TP ladders, and inline trailing or breakeven settings. Send trail or breakeven separately once the target completes.
Entry rules#
Your entry rules, such as Active Hours, the news filter, spread limits and drawdown guards, only apply to the part of a target that opens a position, so a reduction can still go through while new entries are blocked. Your position limits work the same way, but a target names an exact size, so PineHook refuses an opening that would exceed them rather than shrinking it, even when When Limit Exceeded is set to Cap.
With Allow Hedging off, a reversal can close its own group, but its new side is still checked against every other position and pending order on the symbol. When a reversal's new side is refused for any of these reasons, the old side is already closed, so you end up flat with a Partial result.
target=0 follows the same subscription and alert-rate limits as an entry. For an urgent exit while your subscription has expired or your alert allowance is used up, send a close command instead.
Check the result#
Signal Logs shows each target's result as Complete, Partial, Blocked or Uncertain, along with the target and the size your account confirmed afterward. A partial result can include closes or opens that already happened, so read the reason before you send another target. A target refused before rounding, for example for being below the broker minimum, can show as zero, so check the reason before you read a zero as flat.
Uncertain means the broker's answer couldn't be confirmed. PineHook then holds further work on that symbol until it can check, and neither a new target nor a new id= gets around the hold. On MT5, the hold also covers commands without a symbol, while other symbols keep trading, and PineHook checks again on its own several times over the next minute or two, and again when the EA reconnects. Check your broker's trade history before retrying, and contact support if the result stays uncertain.
If a target is rejected, Alert error messages explains the message.