Last updated 4 September 2026
How do I square off a position from a TradingView alert?
- An alert places an order. There is no
squareofforclosesetting in an Alert Automation message. - If you know the size: send the same alert with the opposite
tradetype. Entered withlots=1, close withlots=1. - If you do not know the size: set the exit on the entry — add
target=orstoploss=and the position is closed for you, with no quantity to work out. forceexit=does not close anything yet. It is in beta and only emails you. Usetarget=orstoploss=.- Need a true square-off from an alert? Only the older classic setup has one, and it needs its own webhook link.
- From your own program: call square off a position — it takes no quantity.
When this happens
- 1 Expecting a square-off command in the Alert Automation message format. It places orders only, so every alert needs a side and a size.
- 2 Writing an exit alert for a strategy that cannot know its own position size, instead of setting the exit on the entry alert.
- 3 Working the quantity out by hand from a lot size, when `lots=` does that for you.
Why there is no square-off setting
An Alert Automation message describes an order to place. Every order needs a side and a size, so there is no setting that means “close whatever I am holding”. That is why the field list has tradetype and lots, and nothing like squareoff.
Everything below is a way to get the same result. Which one fits you depends on a single question: does your strategy know the size it is holding when the exit signal fires?
| Your situation | What to use |
|---|---|
| The strategy knows the size | Send the opposite tradetype, same size |
| The strategy does not know the size | target= / stoploss= on the entry alert |
| You must close an unknown size from the alert itself | The older classic setup |
| Your strategy runs in your own program | Square off a position |
1. You know the size — send the opposite order
Send the same alert again with the opposite tradetype. A position you bought is closed with a SELL, and one you sold short is closed with a BUY.
account=MY_ACCOUNT
symbol=NIFTY
exchange=<exchange>
producttype=NORMAL
optiontype=CE
strike=24150
expiry=01-SEP-2026
tradetype=SELL
ordertype=MARKET
lots=1
Write the size the same way you wrote it on the entry. If you entered with lots=1, close with lots=1.
Do not work the quantity out by hand. lots=1 means one lot, and AutoTrader Web looks up the contract’s lot size for you. A lot size is set by the exchange and changes from time to time, so a number you hard-code into your strategy today can be wrong later. quantity= is an exact quantity and is what shares need; lots= is the easier way for options and futures. Use one or the other, never both. See Order size.
2. You do not know the size — set the exit when you enter
This is the usual answer for a strategy that cannot know its own position size. Instead of sending a closing alert later, put the exit on the entry alert with target= or stoploss=. Once your order fills, AutoTrader Web watches the live price and closes the position the moment your level is reached.
account=MY_ACCOUNT
symbol=NIFTY
exchange=<exchange>
producttype=NORMAL
optiontype=CE
strike=24150
expiry=01-SEP-2026
tradetype=BUY
ordertype=MARKET
lots=1
target=2%
stoploss=1%
No second alert is sent, and no quantity is worked out by you. You can set a target only, a stop-loss only, or both, and write each as a percent, in points, or as an exact price. See Set a target and stop-loss.
If you would rather the levels belonged to the order than to the position, place a bracket or cover order from the alert with variety=AT_BO or variety=AT_CO.
forceexit=is not an exit yet. It is listed under risk limits and looks like one, but risk limits are in beta and only email you today — they do not close a position. Usetarget=orstoploss=for an exit that acts.
3. A true square-off from an alert
If you genuinely must close a position of unknown size from the alert itself, the older classic setup has a square-off message that does exactly this. You name the account and the symbol, and it closes the whole open position, whatever the quantity — you never send a size.
This is the one thing the classic setup can do that Alert Automation cannot. It uses the older JSON message and its own webhook link, so it is not a line you can add to an Alert Automation alert — the alert would be set up as a classic one instead. For everything else, Alert Automation is the format we recommend.
4. From your own program
If your strategy runs in your own code rather than as an alert, call square off a position. It closes the whole position for the account and symbol you name, so you never pass a quantity. It is available in every one of our client libraries, and the exact function name and arguments for your language are on that page.
Check the result
Whichever method you use, the Alert Trail records what happened and why. It shows that the order was placed; to confirm the position is actually closed, check your positions on the trading screen as usual.