Skip to main content

Close Position

This command will close a position and requires an account name value and an instrument name value. The instrument name value is the name of the NinjaTrader instrument including the exchange name. For equities, the symbol is sufficient.

A long position with a protective stop working, and a flat account afterwards. A full close cancels the working orders on that one instrument, then liquidates the position. Supplying a quantity or a percent closes only part of it and leaves the rest, with its working orders untouched.

A full close cancels the instrument's working orders first, then closes the position.

A full close first cancels the working orders on that instrument (not the whole account), then sends the closing market order, if the position exists. On NinjaTrader add-on v1.13.13 and later the cancellations are confirmed before the close is sent, so a stop or target from the entry cannot fill against the closing order.

It is important to note that open or pending orders will not be canceled unless a position already exists that can be closed.

Advanced Usage - Scaling out​

The generic CLOSEPOSITION command will close the entire position. It is also possible to scale out of a position by specifying the quantity or percent of contracts that should be closed.

The percentage of contracts to be closed is specific as a decimal number between 0 and 1. For example, percent=0.25; would be used to close 25% of the remaining contracts.

Percentages are always rounded up. So 10% of 1 remaining contract will close 1 contract. Quantities will close up to the remaining contract quantity. So if quantity=4; with 1 remaining contract, the 1 contract will be closed.

A partial close is sent as an opposing market order for that quantity and leaves working orders in place. A stop or target placed with the entry is not resized to the smaller position; it keeps its original quantity, and if it later fills it can open a position in the opposite direction. To bring a stop or target back in line after a partial close, send CANCELANDBRACKET once the partial has filled, using an absolute price for each leg.

A quantity or percent that covers the whole remaining position is treated as a full close and cancels the instrument's working orders. Send each close only after the previous partial has filled: a full close that arrives while a partial's market order is still pending is refused and cancels nothing.

warning

If both quantity and percent appear in the alert payload, quantity will take priority and percent will be ignored.

Quantity and percent must always be greater than zero.

Resending a close​

CLOSEPOSITION closes whatever position is open on that account and instrument at the moment it runs. If your sender timed out or received a 202 pending response, check Alert History or the position before sending the close again: if a new position opened in the meantime, a late resend closes that one too. Adding require_market_position (for example require_market_position=long;) drops the close once the account is flat, but it cannot tell a new position from the old one when both are on the same side.

Example Payloads​

Close the entire ES 12-26 positions in account Sim101.

key=your-secret-key;
command=closeposition;
account=sim101;
instrument=ES 12-26;

Close 2 contracts of however many are remaining in account Sim101:

key=your-secret-key;
command=closeposition;
account=sim101;
instrument=ES 12-26;
quantity=2;

Close 50% of the remaining contracts in account Sim101:

key=your-secret-key;
command=closeposition;
account=sim101;
instrument=ES 12-26;
percent=0.5;
info

The instrument name value is the name of the NinjaTrader instrument, including the exchange name. For equities, the symbol is sufficient. A full close cancels the instrument's working orders and flattens the position; a partial close leaves working orders in place.

On Tradovate

CLOSEPOSITION has full parity with destination=tradovate;, including scaling out: quantity closes up to the remaining size and percent (decimal between 0 and 1, always rounded up) closes that fraction, with quantity taking priority when both are present. A full close uses Tradovate's liquidate endpoint, which also cancels the contract's working orders; partial closes are sent as an opposing market order and leave working orders in place.