API function to square-off a position. The system will validate the position to make sure it is OPEN and proceed with square-off. This command can square-off all types of positions (MIS, NRML, CNC, BO, CO). APIs are available in AmiBroker, MetaTrader, Excel, Java, C#, Python, HTTP REST.
Note: A user should ideally square-off a position with a reverse order. This function is used for special use-cases when the user wants to square-off and does not want to enter a new position for the given symbol. For example, an intraday trader may want to square-off position before auto-square-off time set by the broker.
Api function will square-off a position on Supports leading Indian Stock Brokers.
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)
Examples
// Square-off intraday NSE-SBIN position for account
// defined in chart parameters
squareOffPosition(AT_ACCOUNT, "NET", "MIS",
"NSE", "SBIN");
// Square-off a bracket order NSE-SBIN
// position for account defined in chart parameters
squareOffPosition(AT_ACCOUNT, "NET", "BO",
"NSE", "SBIN");
// Square-off delivery BSE-DMART position for account
// defined in chart parameters
squareOffPosition(AT_ACCOUNT, "NET", "MIS",
"BSE", "DMART");
// Square-off NSE-BANKNIFTY future position for account ABC123
squareOffPosition("ABC123", "NET", "MIS",
"NSE", "BANKNIFTY_29-OCT-2020_FUT");
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)
Examples
// Square-off intraday NSE-SBIN position for account
// defined in chart parameters
squareOffPosition(AT_ACCOUNT, "NET", "MIS",
NSE, "SBIN");
// Square-off delivery BSE-DMART position for account
// defined in chart parameters
squareOffPosition(AT_ACCOUNT, "NET", "MIS",
BSE, "DMART");
// Square-off NSE-BANKNIFTY future position for account ABC123
squareOffPosition("ABC123", "NET", "MIS",
NSE, "BANKNIFTY_29-OCT-2020_FUT");
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)
* @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);
Examples
IOperationResponse<Boolean> response =
autotrader.squareOffPosition(ABC123,
PositionCategory.NET,
PositionType.MIS, "NSE", "SBIN");
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, "NSE", "SBIN");
if(response.Success()) {
Console.WriteLine("Result: {0}", response.Result);
}
else {
Console.WriteLine("Message: {0}", response.Message);
}
Signature
def square_off_position(self, pseudo_account, \
position_category, position_type, exchange, symbol):
Example
response = autotrader.square_off_position( \
'XX1234', 'NET', 'MIS', 'NSE', 'WIPRO')
if response.success():
print("Result: {0}".format(response.result))
else:
print("Message: {0}".format(response.message))
Examples
curl https://api.stocksdeveloper.in/trading/squareOffPosition \
-H "api-key: <your_api_key>" \
-d "pseudoAccount=ACC_NAME" \
-d "category=NET" \
-d "type=MIS" \
-d "exchange=NSE" \
-d "symbol=SBIN"
curl https://api.stocksdeveloper.in/trading/squareOffPosition \
-H "api-key: <your_api_key>" \
-d "pseudoAccount=ACC_NAME" \
-d "category=NET" \
-d "type=BO" \
-d "exchange=NSE" \
-d "symbol=DMART"
curl https://api.stocksdeveloper.in/trading/squareOffPosition \
-H "api-key: <your_api_key>" \
-d "pseudoAccount=ACC_NAME" \
-d "category=NET" \
-d "type=NRML" \
-d "exchange=NSE" \
-d "symbol=BANKNIFTY_29-OCT-2020_FUT"
Response
{
"result":true,
"error":null,
"message":null,
"status":true,
"commandId":"6d4b5738-c1aa-1543-b0c5-8df779e0f44d"
}
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
Examples
' Square-off intraday NSE-SBIN position for account
' defined in chart parameters
SquareOffPosition(AT_ACCOUNT, "NET", "MIS", _
"NSE", "SBIN")
' Square-off delivery BSE-DMART position for account
' defined in chart parameters
SquareOffPosition(AT_ACCOUNT, "NET", "MIS", _
"BSE", "DMART")
' Square-off NSE-BANKNIFTY future position for account ABC123
SquareOffPosition("ABC123", "NET", "MIS", _
"NSE", "BANKNIFTY_29-OCT-2020_FUT");
Postman is widely used tool for API testing. We have provided below a collection of all of our APIs in postman collection format. Click postman collection to know more about how to use it.
Parameters
- account – pseudo account
- category – position category
- type – position type
- exchange – instrument (stock/derivative) exchange
- symbol – instrument (stock/derivative) symbol
Internal Working
It is extremely important that users understand how AutoTrader Web’s square-off works. The details are given on Square-off process.
Admin
The internal working is explained in detail on .
Warning
This feature is fairly stable, but it might not work in rare scenarios. See details below:
Admin
There are 60 different scenarios which needs to be tested in live market.
- 3 Exchanges (NSE, BSE, MCX)
- 4 Brokers (Zerodha, Upstox, Finvasia, AliceBlue)
- 5 Position types (MIS, NRML, CNC, BO, CO)
Which makes a total of (5 * 4 * 3) = 60 scenarios.
Unfortunately, it is not possible to test all of these scenarios in such a short span of time with the limited resources that we have.
We have built this feature with fail-safe. It will stop subsequent executions after first failure. This is extremely important to safeguard our clients. See below scenario:
- Clients strategy sends square-off request
- AutoTrader places square-off order
- Square-off fails (most likely due to multiplier issue, as some broker give net quantity not in multiples of lot size)
- Clients strategy sends square-off requests again as it sees the position is still open
- But AutoTrader will not place another square-off order. Because it will result in a loop and there will be one square-off order for each function call by client’s strategy.
As you can understand, the results could be disastrous. Hence we have added a check which will stop square-off execution for a given position after first failure.
We will keep an eye on all failures and make necessary changes to the system.