Square-off Position

Close one open position for an account, for all position types.

POST https://apix.stocksdeveloper.in/trading/squareOffPosition
WriteReturns true on success7 languagesBroker independent
In short

The Square-off Position call closes one open position for an account. It first checks that the position is OPEN, then places the square-off. It works for all position types (MIS, NRML, CNC, BO, CO). The same function is available in AmiBroker, MetaTrader, Excel, Java, C#, Python and HTTP REST, and returns true on success. Set `cancelOpenOrders=true` to cancel any open orders for that position before square-off.

This call closes a single open position. The system validates that the position is OPEN, then proceeds with the square-off. It can square off all position types: MIS, NRML, CNC, BO and CO. It works across leading Indian stock brokers. In most cases you should square off a position with a reverse order. Use this call for special cases where you want to close a position but do not want to enter a new position for the same symbol. It can also cancel any open orders for the given position before starting square-off, via the cancelOpenOrders parameter.

Code samples

Samples

HTTP

Example

curl https://apix.stocksdeveloper.in/trading/squareOffPosition \
   -H "api-key: <your-api-key>" \
   -d "pseudoAccount=ACC_NAME" \
   -d "category=NET" \
   -d "type=MIS" \
   -d "exchange=<exchange>" \
   -d "symbol=SBIN" \
   -d "cancelOpenOrders=true"

Response

{
	"result":true,
	"error":null,
	"message":null,
	"status":true,
	"commandId":"6d4b5738-c1aa-1543-b0c5-8df779e0f44d"
}

Python

Signature

def square_off_position(self, pseudo_account, \
	position_category, position_type, exchange, symbol):

Example

response = autotrader.square_off_position( \
	'XX1234', 'NET', 'MIS', '<exchange>', 'WIPRO')

if response.success():
    print("Result: {0}".format(response.result))
else:
    print("Message: {0}".format(response.message))

Java

Signature

/**
 * Submits a square-off position request.
 *
 * @param pseudoAccount pseudo account
 * @param category      position category
 * @param type          position type
 * @param exchange      position exchange (broker independent exchange)
 * @param symbol        position symbol (broker independent symbol)
 * @param cancelOpenOrders        cancel all open orders for the given position 
 * @return true on successful acceptance of square-off request, false otherwise
 */
IOperationResponse<Boolean> squareOffPosition(
		final String pseudoAccount, 
		final PositionCategory category,
		final PositionType type, 
		final String exchange, final String symbol, 
        final boolean cancelOpenOrders);

Example

IOperationResponse<Boolean> response = 
	autotrader.squareOffPosition(ABC123, 
		PositionCategory.NET,
		PositionType.MIS, "<exchange>", "SBIN", true);

C#

Signature

/// <summary>
/// Submits a square-off position request.
/// </summary>
/// <param name="pseudoAccount"> pseudo account </param>
/// <param name="category">      position category </param>
/// <param name="type">          position type </param>
/// <param name="exchange">      position exchange (broker independent exchange) </param>
/// <param name="symbol">        position symbol (broker independent symbol) </param>
/// <returns> true on successful acceptance of square-off request, false otherwise </returns>
IOperationResponse<bool?> SquareOffPosition(
	string pseudoAccount, PositionCategory category, 
	PositionType type, string exchange, string symbol);

Example

IOperationResponse<bool?> response = 
	autoTrader.SquareOffPosition(
	"XX1234", PositionCategory.NET,
	PositionType.CO, "<exchange>", "SBIN");

if(response.Success()) {
    Console.WriteLine("Result: {0}", response.Result);
}
else {
    Console.WriteLine("Message: {0}", response.Message);
}

Excel

Signature

' Submits a square-off request for the given position.
'
' pseudoAccount - account to which the position belongs
' category - position category (DAY, NET). Pass NET if you are not sure.
' type - position type (MIS, NRML, CNC, BO, CO) 
' independentExchange - broker independent exchange
' independentSymbol - broker independent symbol
Public Function SquareOffPosition(pseudoAccount As String, _ 
	category As String, posType As String, independentExchange As String, _
	independentSymbol As String) As Boolean

Example 1

' Square-off intraday position for account 
' defined in chart parameters
SquareOffPosition(AT_ACCOUNT, "NET", "MIS", _
	"<exchange>", "SBIN")

Square-off an intraday position using the account defined in chart parameters.

Example 2

' Square-off a future position for account ABC123
SquareOffPosition("ABC123", "NET", "MIS", _
	"<exchange>", "BANKNIFTY_29-OCT-2020_FUT");

Square-off a derivative position for a named account.

AmiBroker

Signature

/**
* Submits a square-off request for the given position.
*
* pseudoAccount - account to which the position belongs
* category - position category (DAY, NET). Pass NET if you are not sure.
* type - position type (MIS, NRML, CNC, BO, CO) 
* independentExchange - broker independent exchange
* independentSymbol - broker independent symbol
*/
function squareOffPosition(pseudoAccount, category, type, 
	independentExchange, independentSymbol)

Example 1

// Square-off intraday SBIN position for account 
// defined in chart parameters
squareOffPosition(AT_ACCOUNT, "NET", "MIS", 
		"<exchange>", "SBIN");

Square-off an intraday position using the account defined in chart parameters.

Example 2

// Square-off a bracket order SBIN 
// position for account defined in chart parameters
squareOffPosition(AT_ACCOUNT, "NET", "BO", 
		"<exchange>", "SBIN");

Square-off a bracket order position using the account defined in chart parameters.

MetaTrader

Signature

/**
* Submits a square-off request for the given position.
*
* pseudoAccount - account to which the position belongs
* category - position category (DAY, NET). Pass NET if you are not sure.
* type - position type (MIS, NRML, CNC, BO, CO) 
* exchange - broker independent exchange (Enum)
* independentSymbol - broker independent symbol
*/
bool squareOffPosition(string pseudoAccount, string category, 
	string type, Exchange exchange, string independentSymbol)

Example 1

// Square-off intraday SBIN position for account 
// defined in chart parameters
squareOffPosition(AT_ACCOUNT, "NET", "MIS", 
		AT_EXCHANGE, "SBIN");

Square-off an intraday position using the account defined in chart parameters.

Example 2

// Square-off a future position for account ABC123
squareOffPosition("ABC123", "NET", "MIS", 
		AT_EXCHANGE, "BANKNIFTY_29-OCT-2020_FUT");

Square-off a derivative position for a named account.

Postman

Postman is a widely used tool for API testing. We provide a collection of all our APIs in Postman collection format. See the Postman collection guide to learn how to use it.

Parameters

ParameterDescription
accountnickname of the broker account (also known as pseudo account)
categoryposition category
typeposition type
exchangeinstrument (stock/derivative) exchange
symbolinstrument (stock/derivative) symbol

For broker-independent exchange and symbol values, see instruments and symbols.

Return value

DirectJava · C# · Python · HTTP

The call returns the order id given by your trading platform.

BridgeExcel · AmiBroker · MetaTrader

The call returns the library order id. The Desktop Client passes the request on to your broker.

Notes

It is important to understand how AutoTrader Web’s square-off works. The full details are on the Square-off process page.

This feature is fairly stable, but it might not work in rare scenarios. There are many combinations to test across Indian stock exchanges, brokers and position types (MIS, NRML, CNC, BO, CO), and testing all of them in a live market is hard with the time and resources we have.

To stay safe, this feature has a fail-safe built in. It stops further executions after the first failure. This protects clients. Consider this scenario:

  • The client’s strategy sends a square-off request.
  • AutoTrader places a square-off order.
  • The square-off fails (most likely a multiplier issue, as some brokers return net quantity that is not in multiples of lot size).
  • The client’s strategy sees the position is still open, so it sends the square-off request again.
    • But AutoTrader will not place another square-off order. Doing so would create a loop, with one square-off order for every function call by the client’s strategy.

The result of such a loop could be disastrous. So we added a check that stops square-off execution for a position after the first failure. We watch all failures and make changes to the system as needed.

To exit the whole account instead, use Square-off Portfolio.

Was this page helpful?

Last updated 21 June 2026