Square Off Single Position
The Square Off Single Position API allows you to close a specific position in a particular account under a strategy. This functionality is useful when you need to exit individual positions rather than squaring off all positions.
API Details
- Method: POST
- Endpoint:
/SquareOffSingle
Request Parameters
Parameter | Type | Required | Description |
---|---|---|---|
strategyname | string | Yes | Name of the strategy under which the position exists |
accountName | string | Yes | Name of the account holding the position |
token | string | Yes | Exchange:Token of the instrument (e.g., "NSE:13528") |
positionType | string | Yes | Type of position ("Intraday" or "Delivery") |
at_limit | boolean | Yes | Whether to place a limit order (true) or market order (false) |
qty | integer | No | Quantity to square off. If not specified, entire position will be squared off |
Example Usage
- Square off using market order:
from maticalgos.sparkLib import SparkLib
spk = SparkLib(apikeys="apikeys")
spk.generate_token()
# Square off an intraday position at market price
status = spk.squareOffSingle(
strategyName='test_1',
accountName='account_name',
token='NSE:13528',
positionType='Intraday',
at_limit=False
)
- Square off using limit order:
# Square off a delivery position with limit price
status = spk.squareOffSingle(
strategyName='test_1',
accountName='account_name',
token='NSE:13528',
positionType='Delivery',
at_limit=True,
qty=100 # Optional: specify quantity to partially square off
)
Response Structure
On successful execution:
{
"status": true,
"error": false,
"data": [{
"strefID": "123"
}],
"message": "Order Placed, Position Squared off"
}
On error:
{
"status": false,
"error": true,
"data": [],
"message": "Bad request"
}
Important Notes
- The function creates an opposing order to close the position (Buy order for short positions, Sell order for long positions)
- When
at_limit
is true, the order is placed at the current market price as a limit order - When
at_limit
is false, a market order is placed to ensure immediate execution - If quantity is not specified, the entire position will be squared off
- Position type (Intraday/Delivery) must match the actual position type to square off correctly
Best Practices
- Verify Position: Ensure the position exists before attempting to square it off
- Check Quantity: When specifying a quantity, ensure it doesn't exceed the position size
- Market Hours: Square off during market hours to ensure proper execution
- Position Type: Double-check the position type to avoid errors
- Limit Orders: When using
at_limit=true
, monitor the order status as it might not execute immediately
Common Error Scenarios
- Position not found
- Invalid token format
- Market closed
- Invalid position type
- Insufficient position quantity
- Account or strategy deactivated