Skip to main content

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

ParameterTypeRequiredDescription
strategynamestringYesName of the strategy under which the position exists
accountNamestringYesName of the account holding the position
tokenstringYesExchange:Token of the instrument (e.g., "NSE:13528")
positionTypestringYesType of position ("Intraday" or "Delivery")
at_limitbooleanYesWhether to place a limit order (true) or market order (false)
qtyintegerNoQuantity to square off. If not specified, entire position will be squared off

Example Usage

  1. 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
)
  1. 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

  1. Verify Position: Ensure the position exists before attempting to square it off
  2. Check Quantity: When specifying a quantity, ensure it doesn't exceed the position size
  3. Market Hours: Square off during market hours to ensure proper execution
  4. Position Type: Double-check the position type to avoid errors
  5. Limit Orders: When using at_limit=true, monitor the order status as it might not execute immediately

Common Error Scenarios

  1. Position not found
  2. Invalid token format
  3. Market closed
  4. Invalid position type
  5. Insufficient position quantity
  6. Account or strategy deactivated